Skip to content
Snippets Groups Projects
Commit 84572674 authored by Sadashiv Dalvi's avatar Sadashiv Dalvi
Browse files

using dependency injection for services

parent ae69c438
No related branches found
No related tags found
No related merge requests found
services:
google_api_client.client:
class: Drupal\google_api_client\Service\GoogleApiClientService
arguments: ['@logger.factory', '@cache.default']
arguments: ['@logger.factory', '@cache.default', '@messenger', '@string_translation', '@config.factory', '@module_handler']
google_api_service_client.client:
class: Drupal\google_api_client\Service\GoogleApiServiceClientService
arguments: ['@logger.factory', '@cache.default']
arguments: ['@logger.factory', '@cache.default', '@messenger', '@string_translation']
cache.google_api_client_scopes:
class: Drupal\Core\Cache\CacheBackendInterface
......
......@@ -3,11 +3,16 @@
namespace Drupal\google_api_client\Service;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Link;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\google_api_client\GoogleApiClientInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Google_Client;
use \Drupal\Core\StringTranslation\StringTranslationTrait;
/**
* Class Google API Client Service.
......@@ -16,6 +21,8 @@ use Google_Client;
*/
class GoogleApiClientService {
use StringTranslationTrait;
/**
* The GoogleClient object.
*
......@@ -44,6 +51,27 @@ class GoogleApiClientService {
*/
private $cacheBackend;
/**
* The messenger.
*
* @var \Drupal\Core\Messenger\MessengerInterface
*/
protected $messenger;
/**
* The system theme config object.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* The module handler service.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* Callback Controller constructor.
*
......@@ -51,11 +79,22 @@ class GoogleApiClientService {
* LoggerChannelFactoryInterface.
* @param \Drupal\Core\Cache\CacheBackendInterface $cacheBackend
* Cache Backend.
* @param MessengerInterface $messenger
* The messenger.
* @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
* The string translation.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler
*/
public function __construct(LoggerChannelFactoryInterface $loggerFactory,
CacheBackendInterface $cacheBackend) {
public function __construct(LoggerChannelFactoryInterface $loggerFactory, CacheBackendInterface $cacheBackend, MessengerInterface $messenger, TranslationInterface $string_translation, ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler) {
$this->loggerFactory = $loggerFactory;
$this->cacheBackend = $cacheBackend;
$this->messenger = $messenger;
$this->stringTranslation = $string_translation;
$this->configFactory = $config_factory;
$this->moduleHandler = $module_handler;
}
/**
......@@ -83,16 +122,17 @@ class GoogleApiClientService {
* @param \Google_Client
* Optionally parameter for developers who want to set initial google client object.
*
* @return \Google_Client
* Google_Client object with all params from the account.
* @return \Google_Client|bool
* Google_Client object with all params from the account or false.
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
private function getClient($client) {
if (!google_api_client_load_library()) {
// We don't have library installed notify admin and abort.
$status_report_link = Link::createFromRoute(t('Status Report'), 'system.status')->toString();
\Drupal::messenger()->addError(t("Can't get the google client as library is missing check %status_report for more details. Report this to site administrator.", [
$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>');
......@@ -120,7 +160,7 @@ class GoogleApiClientService {
// Let other modules change scopes.
$google_api_client_id = $google_api_client->getId();
\Drupal::moduleHandler()->alter('google_api_client_account_scopes', $scopes, $google_api_client_id);
$this->moduleHandler->alter('google_api_client_account_scopes', $scopes, $google_api_client_id);
$client->addScope($scopes);
$this->googleClient = $client;
if ($google_api_client->getAuthenticated()) {
......@@ -160,7 +200,8 @@ class GoogleApiClientService {
* Instead, you should just check googleClient object function.
*/
public function getAccessTokenWithRefreshToken() {
if ($access_token = $this->googleApiClient->getAccessToken() && isset($access_token['refresh_token'])) {
$access_token = $this->googleApiClient->getAccessToken();
if ($access_token && isset($access_token['refresh_token'])) {
return $this->googleClient->fetchAccessTokenWithRefreshToken($access_token['refresh_token']);
}
return FALSE;
......@@ -224,7 +265,7 @@ class GoogleApiClientService {
if (!is_array($services)) {
$services = [$services];
}
$classes = \Drupal::config('google_api_client.google_api_classes')->get('google_api_client_google_api_classes');
$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);
......
......@@ -3,8 +3,12 @@
namespace Drupal\google_api_client\Service;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Link;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\google_api_client\GoogleApiServiceClientInterface;
use Google_Client;
use Symfony\Component\HttpFoundation\RedirectResponse;
......@@ -16,6 +20,8 @@ use Symfony\Component\HttpFoundation\RedirectResponse;
*/
class GoogleApiServiceClientService {
use StringTranslationTrait;
/**
* The GoogleClient object.
*
......@@ -44,6 +50,20 @@ class GoogleApiServiceClientService {
*/
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.
*
......@@ -51,11 +71,19 @@ class GoogleApiServiceClientService {
* LoggerChannelFactoryInterface.
* @param \Drupal\Core\Cache\CacheBackendInterface $cacheBackend
* Cache Backend.
* @param 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) {
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;
}
/**
......@@ -83,8 +111,8 @@ class GoogleApiServiceClientService {
* @param \Google_Client
* Optionally parameter for developers who want to set initial google client object.
*
* @return \Google_Client
* Google_Client object with all params from the account.
* @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
......@@ -94,7 +122,7 @@ class GoogleApiServiceClientService {
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();
\Drupal::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.", [
$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>');
......@@ -142,7 +170,7 @@ class GoogleApiServiceClientService {
if (!is_array($services)) {
$services = [$services];
}
$classes = \Drupal::config('google_api_client.google_api_classes')->get('google_api_client_google_api_classes');
$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);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment