class GoogleApiServiceClientService in Google API PHP Client 8.4
Same name and namespace in other branches
- 8.3 src/Service/GoogleApiServiceClientService.php \Drupal\google_api_client\Service\GoogleApiServiceClientService
Class Google API Client Service.
@package Drupal\google_api_client\Service
Hierarchy
- class \Drupal\google_api_client\Service\GoogleApiServiceClientService uses StringTranslationTrait
Expanded class hierarchy of GoogleApiServiceClientService
1 string reference to 'GoogleApiServiceClientService'
1 service uses GoogleApiServiceClientService
File
- src/
Service/ GoogleApiServiceClientService.php, line 20
Namespace
Drupal\google_api_client\ServiceView 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
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
GoogleApiServiceClientService:: |
private | property | Cache. | |
GoogleApiServiceClientService:: |
protected | property | The system theme config object. | |
GoogleApiServiceClientService:: |
public | property | The GoogleApiClient Entity Object. | |
GoogleApiServiceClientService:: |
public | property | The GoogleClient object. | |
GoogleApiServiceClientService:: |
protected | property | The logger factory. | |
GoogleApiServiceClientService:: |
protected | property | The messenger. | |
GoogleApiServiceClientService:: |
private | function | Function to retrieve the google client for different operations. | |
GoogleApiServiceClientService:: |
public | function | This function is designed to return objects of services classes. | |
GoogleApiServiceClientService:: |
public | function | Function to set the GoogleApiClient account for the service. | |
GoogleApiServiceClientService:: |
public | function | Callback Controller constructor. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |