You are here

class GoogleApiServiceClientService in Google API PHP Client 8.3

Same name and namespace in other branches
  1. 8.4 src/Service/GoogleApiServiceClientService.php \Drupal\google_api_client\Service\GoogleApiServiceClientService

Class Google API Client Service.

@package Drupal\google_api_client\Service

Hierarchy

Expanded class hierarchy of GoogleApiServiceClientService

1 string reference to 'GoogleApiServiceClientService'
google_api_client.services.yml in ./google_api_client.services.yml
google_api_client.services.yml
1 service uses GoogleApiServiceClientService
google_api_service_client.client in ./google_api_client.services.yml
Drupal\google_api_client\Service\GoogleApiServiceClientService

File

src/Service/GoogleApiServiceClientService.php, line 20

Namespace

Drupal\google_api_client\Service
View source
class GoogleApiServiceClientService {
  use StringTranslationTrait;

  /**
   * The GoogleClient object.
   *
   * @var \Google_Client
   */
  public $googleClient;

  /**
   * The GoogleApiClient Entity Object.
   *
   * @var \Drupal\google_api_client\GoogleApiServiceClientInterface
   */
  public $googleApiServiceClient;

  /**
   * The logger factory.
   *
   * @var \Drupal\Core\Logger\LoggerChannelFactoryInterface
   */
  protected $loggerFactory;

  /**
   * Cache.
   *
   * @var \Drupal\Core\Cache\CacheBackendInterface
   */
  private $cacheBackend;

  /**
   * The messenger.
   *
   * @var \Drupal\Core\Messenger\MessengerInterface
   */
  protected $messenger;

  /**
   * The system theme config object.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * Callback Controller constructor.
   *
   * @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $loggerFactory
   *   LoggerChannelFactoryInterface.
   * @param \Drupal\Core\Cache\CacheBackendInterface $cacheBackend
   *   Cache Backend.
   * @param \Drupal\Core\Messenger\MessengerInterface $messenger
   *   The messenger.
   * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
   *   The string translation.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   */
  public function __construct(LoggerChannelFactoryInterface $loggerFactory, CacheBackendInterface $cacheBackend, MessengerInterface $messenger, TranslationInterface $string_translation, ConfigFactoryInterface $config_factory) {
    $this->loggerFactory = $loggerFactory;
    $this->cacheBackend = $cacheBackend;
    $this->messenger = $messenger;
    $this->stringTranslation = $string_translation;
    $this->configFactory = $config_factory;
  }

  /**
   * Function to set the GoogleApiClient account for the service.
   *
   * @param \Drupal\google_api_client\GoogleApiServiceClientInterface $google_api_client
   *   Pass completely loaded GoogleApiClient object.
   * @param \Google_Client|null $googleClient
   *   Optionally parameter for developers who want to set initial
   *   google client object.
   *
   * @throws \Google_Exception
   */
  public function setGoogleApiClient(GoogleApiServiceClientInterface $google_api_client, \Google_Client $googleClient = NULL) {
    $this->googleApiServiceClient = $google_api_client;

    // Add the client.
    $this
      ->getClient($googleClient);
  }

  /**
   * Function to retrieve the google client for different operations.
   *
   * Developers can pass the google_api_client object to setGoogleApiClient
   * and get the api client ready for operations.
   *
   * @param \Google_Client|null $client
   *   Optionally parameter for developers who want to set initial
   *   google client object.
   *
   * @return \Google_Client|bool
   *   Google_Client object with all params from the account or false.
   *
   * @throws \Google_Exception|\Drupal\Core\Entity\EntityStorageException
   *    Google Exception if any api function fails and
   *    EntityStorage Exception if entity save fails.
   */
  private function getClient(\Google_Client $client = NULL) {
    if (!google_api_client_load_library()) {

      // We don't have library installed notify admin and abort.
      $status_report_link = Link::createFromRoute($this
        ->t('Status Report'), 'system.status')
        ->toString();
      $this->messenger
        ->addError($this
        ->t("Can't get the google client as library is missing check %status_report for more details. Report this to site administrator.", [
        '%status_report' => $status_report_link,
      ]));
      $response = new RedirectResponse('<front>');
      $response
        ->send();
      return FALSE;
    }
    if ($client == NULL) {
      $client = new \Google_Client();
    }
    $client
      ->setAuthConfig($this->googleApiServiceClient
      ->getAuthConfig());
    $client
      ->setScopes($this->googleApiServiceClient
      ->getScopes(TRUE));
    if ($access_token = $this->googleApiServiceClient
      ->getAccessToken()) {
      $client
        ->setAccessToken($access_token);
      if ($client
        ->isAccessTokenExpired()) {
        $access_token = $client
          ->fetchAccessTokenWithAssertion();
        if ($access_token) {
          $this->googleApiServiceClient
            ->setAccessToken($access_token);
          $this->googleApiServiceClient
            ->save();
        }
      }
    }
    else {
      $access_token = $client
        ->fetchAccessTokenWithAssertion();
      if ($access_token) {
        $this->googleApiServiceClient
          ->setAccessToken($access_token);
        $this->googleApiServiceClient
          ->save();
      }
    }
    $this->googleClient = $client;
    return $client;
  }

  /**
   * This function is designed to return objects of services classes.
   *
   * So if the account is authenticated for say Google calendar then
   * this function will return Google_Service_Calendar class object.
   *
   * @return array
   *   Array of Google_Service classes with servicename as index.
   */
  public function getServiceObjects() {
    $google_api_service_client = $this->googleApiServiceClient;
    $services = $google_api_service_client
      ->getServices();
    if (!is_array($services)) {
      $services = [
        $services,
      ];
    }
    $classes = $this->configFactory
      ->get('google_api_client.google_api_classes')
      ->get('google_api_client_google_api_classes');
    $return = [];
    foreach ($services as $service) {
      $return[$service] = new $classes[$service]($this->googleClient);
    }
    return $return;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
GoogleApiServiceClientService::$cacheBackend private property Cache.
GoogleApiServiceClientService::$configFactory protected property The system theme config object.
GoogleApiServiceClientService::$googleApiServiceClient public property The GoogleApiClient Entity Object.
GoogleApiServiceClientService::$googleClient public property The GoogleClient object.
GoogleApiServiceClientService::$loggerFactory protected property The logger factory.
GoogleApiServiceClientService::$messenger protected property The messenger.
GoogleApiServiceClientService::getClient private function Function to retrieve the google client for different operations.
GoogleApiServiceClientService::getServiceObjects public function This function is designed to return objects of services classes.
GoogleApiServiceClientService::setGoogleApiClient public function Function to set the GoogleApiClient account for the service.
GoogleApiServiceClientService::__construct public function Callback Controller constructor.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.