SiteimproveUtils.php in Siteimprove 8
File
src/SiteimproveUtils.php
View source
<?php
namespace Drupal\siteimprove;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Logger\RfcLogLevel;
use Drupal\Core\Path\PathMatcherInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
use Drupal\Core\Utility\Error;
use Drupal\siteimprove\Plugin\SiteimproveDomainManager;
use GuzzleHttp\Client;
class SiteimproveUtils {
use StringTranslationTrait;
const TOKEN_REQUEST_URL = 'https://my2.siteimprove.com/auth/token';
protected $currentUser;
protected $configFactory;
protected $httpClient;
protected $routeMatch;
protected $pathMatcher;
protected $siteimproveDomainManager;
protected $logger;
public function __construct(ConfigFactoryInterface $config_factory, AccountInterface $current_user, Client $http_client, RouteMatchInterface $routeMatch, PathMatcherInterface $pathMatcher, SiteimproveDomainManager $siteimproveDomainManager, LoggerChannelFactoryInterface $loggerChannelFactory) {
$this->configFactory = $config_factory;
$this->currentUser = $current_user;
$this->httpClient = $http_client;
$this->routeMatch = $routeMatch;
$this->pathMatcher = $pathMatcher;
$this->siteimproveDomainManager = $siteimproveDomainManager;
$this->logger = $loggerChannelFactory
->get('Siteimprove');
}
public function requestToken() {
try {
$response = $this->httpClient
->get(self::getTokenRequestUrl(), [
'headers' => [
'Accept' => 'application/json',
],
]);
$data = (string) $response
->getBody();
if (!empty($data)) {
$json = json_decode($data);
if (!empty($json->token)) {
return $json->token;
}
else {
throw new \Exception();
}
}
else {
throw new \Exception();
}
} catch (\Exception $e) {
$this->logger
->log(RfcLogLevel::ERROR, 'There was an error requesting a new token. %type: @message in %function (line %line of %file).', Error::decodeException($e));
}
return FALSE;
}
public static function getTokenRequestUrl() {
return self::TOKEN_REQUEST_URL . '?cms=Drupal-' . \Drupal::VERSION;
}
public function getSiteimproveOverlayLibrary() {
return 'siteimprove/siteimprove.overlay';
}
public function getSiteimproveLibrary() {
return 'siteimprove/siteimprove';
}
public function getSiteimproveSettings(array $url, $type, $auto = TRUE) {
return [
'url' => $url,
'auto' => $auto,
];
}
public function getSiteimproveToken() {
return $this->configFactory
->get('siteimprove.settings')
->get('token');
}
public function setSessionUrl(?EntityInterface $entity) {
if ($this->currentUser
->hasPermission('use siteimprove')) {
$urls = $this
->getEntityUrls($entity);
foreach ($urls as $url) {
$_SESSION['siteimprove_url'][] = $url;
}
}
}
public function getEntityUrls(?EntityInterface $entity) {
if (is_null($entity) || !$entity
->hasLinkTemplate('canonical')) {
return [];
}
$domains = $this
->getEntityDomains($entity);
$url_relative = $entity
->toUrl('canonical', [
'absolute' => FALSE,
])
->toString(TRUE);
$urls = [];
foreach ($domains as $domain) {
$urls[] = $domain . $url_relative
->getGeneratedUrl();
}
$frontpage = $this->configFactory
->get('system.site')
->get('page.front');
$current_route_name = $this->routeMatch
->getRouteName();
$node_route = in_array($current_route_name, [
'entity.node.edit_form',
'entity.node.latest_version',
]);
$taxonomy_route = in_array($current_route_name, [
'entity.taxonomy_term.edit_form',
'entity.taxonomy_term.latest_version',
]);
if ($node_route && '/node/' . $entity
->id() === $frontpage || $taxonomy_route && '/taxonomy/term/' . $entity
->id() === $frontpage || $this->pathMatcher
->isFrontPage()) {
$front = Url::fromRoute('<front>')
->toString(TRUE);
foreach ($domains as $domain) {
$urls[] = $domain . $front
->getGeneratedUrl();
}
}
return $urls;
}
public function getEntityDomains(EntityInterface $entity) {
$config = $this->configFactory
->get('siteimprove.settings');
$plugin = $this->siteimproveDomainManager
->createInstance($config
->get('domain_plugin_id'));
return $plugin
->getUrls($entity);
}
}