GoogleAnalyticsCounterAuthManager.php in Google Analytics Counter 8.3
File
src/GoogleAnalyticsCounterAuthManager.php
View source
<?php
namespace Drupal\google_analytics_counter;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\State\StateInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Exception;
use Psr\Log\LoggerInterface;
class GoogleAnalyticsCounterAuthManager implements GoogleAnalyticsCounterAuthManagerInterface {
use StringTranslationTrait;
const TABLE = 'node__field_google_analytics_counter';
protected $config;
protected $connection;
protected $state;
protected $logger;
protected $messenger;
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;
}
public function isAuthenticated() {
return $this->state
->get('google_analytics_counter.access_token') != NULL ? TRUE : FALSE;
}
public function beginGacAuthentication() {
$gafeed = new GoogleAnalyticsCounterFeed();
$gafeed
->beginAuthentication($this->config
->get('general_settings.client_id'), $this->config
->get('general_settings.redirect_uri'));
}
public function newGaFeed() {
$config = $this->config;
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;
}
public function getWebPropertiesOptions() {
$feed = $this
->newGaFeed();
if (!$feed) {
$options = [
'unauthenticated' => '',
];
return $options;
}
if (isset($feed
->queryWebProperties()->results->items)) {
$web_properties = $feed
->queryWebProperties()->results->items;
$profiles = $feed
->queryProfiles()->results->items;
}
$options = [];
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;
}
}