You are here

class GoogleAnalyticsCounterAuthManager in Google Analytics Counter 8.3

Class GoogleAnalyticsAuthManager.

Sets the auth methods.

@package Drupal\google_analytics_counter

Hierarchy

Expanded class hierarchy of GoogleAnalyticsCounterAuthManager

1 string reference to 'GoogleAnalyticsCounterAuthManager'
google_analytics_counter.services.yml in ./google_analytics_counter.services.yml
google_analytics_counter.services.yml
1 service uses GoogleAnalyticsCounterAuthManager
google_analytics_counter.auth_manager in ./google_analytics_counter.services.yml
\Drupal\google_analytics_counter\GoogleAnalyticsCounterAuthManager

File

src/GoogleAnalyticsCounterAuthManager.php, line 20

Namespace

Drupal\google_analytics_counter
View source
class GoogleAnalyticsCounterAuthManager implements GoogleAnalyticsCounterAuthManagerInterface {
  use StringTranslationTrait;

  /**
   * The table for the node__field_google_analytics_counter storage.
   */
  const TABLE = 'node__field_google_analytics_counter';

  /**
   * The google_analytics_counter.settings config object.
   *
   * @var \Drupal\Core\Config\Config
   */
  protected $config;

  /**
   * The database connection service.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $connection;

  /**
   * The state where all the tokens are saved.
   *
   * @var \Drupal\Core\State\StateInterface
   */
  protected $state;

  /**
   * A logger instance.
   *
   * @var \Psr\Log\LoggerInterface
   */
  protected $logger;

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

  /**
   * Constructs a Google Analytics Counter object.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The factory for configuration objects.
   * @param \Drupal\Core\Database\Connection $connection
   *   A database connection.
   * @param \Drupal\Core\State\StateInterface $state
   *   The state of the drupal site.
   * @param \Psr\Log\LoggerInterface $logger
   *   A logger instance.
   * @param \Drupal\Core\Messenger\MessengerInterface $messenger
   *   The messenger service.
   */
  public function __construct(ConfigFactoryInterface $config_factory, Connection $connection, StateInterface $state, LoggerInterface $logger, MessengerInterface $messenger) {
    $this->config = $config_factory
      ->get('google_analytics_counter.settings');
    $this->connection = $connection;
    $this->state = $state;
    $this->logger = $logger;
    $this->messenger = $messenger;
  }

  /**
   * Check to make sure we are authenticated with google.
   *
   * @return bool
   *   True if there is a refresh token set.
   */
  public function isAuthenticated() {
    return $this->state
      ->get('google_analytics_counter.access_token') != NULL ? TRUE : FALSE;
  }

  /**
   * Begin authentication to Google authentication page with the client_id.
   */
  public function beginGacAuthentication() {
    $gafeed = new GoogleAnalyticsCounterFeed();
    $gafeed
      ->beginAuthentication($this->config
      ->get('general_settings.client_id'), $this->config
      ->get('general_settings.redirect_uri'));
  }

  /**
   * Instantiate a new GoogleAnalyticsCounterFeed object.
   *
   * @return object
   *   GoogleAnalyticsCounterFeed object to authorize access and request data
   *   from the Google Analytics Core Reporting API.
   */
  public function newGaFeed() {
    $config = $this->config;

    // If the access token is still valid, return an authenticated GAFeed.
    if ($this->state
      ->get('google_analytics_counter.access_token') && time() < $this->state
      ->get('google_analytics_counter.expires_at')) {
      return new GoogleAnalyticsCounterFeed($this->state
        ->get('google_analytics_counter.access_token'));
    }
    elseif ($this->state
      ->get('google_analytics_counter.refresh_token')) {
      $client_id = $config
        ->get('general_settings.client_id');
      $client_secret = $config
        ->get('general_settings.client_secret');
      $refresh_token = $this->state
        ->get('google_analytics_counter.refresh_token');
      try {
        $gac_feed = new GoogleAnalyticsCounterFeed();
        $gac_feed
          ->refreshToken($client_id, $client_secret, $refresh_token);
        $this->state
          ->setMultiple([
          'google_analytics_counter.access_token' => $gac_feed->accessToken,
          'google_analytics_counter.expires_at' => $gac_feed->expiresAt,
        ]);
        return $gac_feed;
      } catch (Exception $e) {
        $this->messenger
          ->addError($this
          ->t('There was an authentication error. Message: %message', [
          '%message' => $e
            ->getMessage(),
        ]));
        return NULL;
      }
    }
    elseif (isset($_GET['code'])) {
      try {
        $gac_feed = new GoogleAnalyticsCounterFeed();
        $gac_feed
          ->finishAuthentication($config
          ->get('general_settings.client_id'), $config
          ->get('general_settings.client_secret'), $config
          ->get('general_settings.redirect_uri'));
        $this->state
          ->setMultiple([
          'google_analytics_counter.access_token' => $gac_feed->accessToken,
          'google_analytics_counter.expires_at' => $gac_feed->expiresAt,
          'google_analytics_counter.refresh_token' => $gac_feed->refreshToken,
        ]);
      } catch (Exception $e) {
        $this->messenger
          ->addError($this
          ->t('There was an authentication error. Message: %message', [
          '%message' => $e
            ->getMessage(),
        ]));
        return NULL;
      }
    }
    return NULL;
  }

  /**
   * Get the list of available web properties.
   *
   * @return array
   *   Array of options.
   */
  public function getWebPropertiesOptions() {

    // When not authenticated of if there are no Analytics, no options will be displayed.
    $feed = $this
      ->newGaFeed();
    if (!$feed) {
      $options = [
        'unauthenticated' => '',
      ];
      return $options;
    }

    // Check for web properties.
    if (isset($feed
      ->queryWebProperties()->results->items)) {
      $web_properties = $feed
        ->queryWebProperties()->results->items;
      $profiles = $feed
        ->queryProfiles()->results->items;
    }
    $options = [];

    // Add options for each web property.
    if (!empty($profiles)) {
      foreach ($profiles as $profile) {
        $webprop = NULL;
        foreach ($web_properties as $web_property) {
          if ($web_property->id == $profile->webPropertyId) {
            $webprop = $web_property;
            break;
          }
        }
        $options[$webprop->name][$profile->id] = $profile->name . ' (' . $profile->id . ')';
      }
    }
    return $options;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
GoogleAnalyticsCounterAuthManager::$config protected property The google_analytics_counter.settings config object.
GoogleAnalyticsCounterAuthManager::$connection protected property The database connection service.
GoogleAnalyticsCounterAuthManager::$logger protected property A logger instance.
GoogleAnalyticsCounterAuthManager::$messenger protected property The Messenger service.
GoogleAnalyticsCounterAuthManager::$state protected property The state where all the tokens are saved.
GoogleAnalyticsCounterAuthManager::beginGacAuthentication public function Begin authentication to Google authentication page with the client_id. Overrides GoogleAnalyticsCounterAuthManagerInterface::beginGacAuthentication
GoogleAnalyticsCounterAuthManager::getWebPropertiesOptions public function Get the list of available web properties. Overrides GoogleAnalyticsCounterAuthManagerInterface::getWebPropertiesOptions
GoogleAnalyticsCounterAuthManager::isAuthenticated public function Check to make sure we are authenticated with google. Overrides GoogleAnalyticsCounterAuthManagerInterface::isAuthenticated
GoogleAnalyticsCounterAuthManager::newGaFeed public function Instantiate a new GoogleAnalyticsCounterFeed object. Overrides GoogleAnalyticsCounterAuthManagerInterface::newGaFeed
GoogleAnalyticsCounterAuthManager::TABLE constant The table for the node__field_google_analytics_counter storage.
GoogleAnalyticsCounterAuthManager::__construct public function Constructs a Google Analytics Counter object.
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.