PathContext.php in Acquia Lift Connector 8.3
File
src/Service/Context/PathContext.php
View source
<?php
namespace Drupal\acquia_lift\Service\Context;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Path\CurrentPathStack;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Component\Utility\Html;
use Drupal\user\UserInterface;
use Drupal\acquia_lift\Service\Helper\PathMatcher;
use Drupal\acquia_lift\Service\Helper\SettingsHelper;
class PathContext extends BaseContext implements CacheableDependencyInterface {
private $settings;
private $credentialSettings;
private $identitySettings;
private $requestPathPatterns;
private $currentPath;
private $pathMatcher;
public function __construct(ConfigFactoryInterface $config_factory, CurrentPathStack $current_path_stack, RequestStack $request_stack, PathMatcher $pathMatcher) {
$this->settings = $config_factory
->get('acquia_lift.settings');
$this->credentialSettings = $this->settings
->get('credential');
$this->identitySettings = $this->settings
->get('identity');
$visibilitySettings = $this->settings
->get('visibility');
$this->requestPathPatterns = $visibilitySettings['path_patterns'];
$this->currentPath = $current_path_stack
->getPath();
$this->pathMatcher = $pathMatcher;
$this
->setContextIdentityByRequest($request_stack);
}
public function shouldAttach() {
if (SettingsHelper::isInvalidCredential($this->credentialSettings)) {
return FALSE;
}
if ($this->pathMatcher
->match($this->currentPath, $this->requestPathPatterns)) {
return FALSE;
}
return TRUE;
}
private function setContextIdentityByRequest($request_stack) {
$identity_parameter = $this->identitySettings['identity_parameter'];
if (empty($identity_parameter)) {
return;
}
$identity_type_parameter = $this->identitySettings['identity_type_parameter'];
$query_names = [
$identity_parameter,
$identity_type_parameter,
];
$this
->setContextCacheByQueryNames($query_names);
$query_string = $request_stack
->getCurrentRequest()
->getQueryString();
$parsed_query_string = UrlHelper::parse('?' . $query_string);
$queries = $parsed_query_string['query'];
if (empty($queries[$identity_parameter])) {
return;
}
$default_identity_type = $this->identitySettings['default_identity_type'];
$identity = $queries[$identity_parameter];
$identityType = empty($default_identity_type) ? SettingsHelper::DEFAULT_IDENTITY_TYPE_DEFAULT : $default_identity_type;
if (!empty($identity_type_parameter) && !empty($queries[$identity_type_parameter])) {
$identityType = $queries[$identity_type_parameter];
}
$this
->setContextIdentity($identity, $identityType);
}
private function setContextCacheByQueryNames($query_names) {
foreach ($query_names as $query_name) {
if (empty($query_name)) {
continue;
}
$this->cacheContexts[] = 'url.query_args:' . $query_name;
}
}
public function setContextIdentityByUser(UserInterface $user) {
if (empty($this->identitySettings['capture_identity'])) {
return;
}
$this
->setContextIdentity($user
->getEmail(), 'email');
}
private function setContextIdentity($identity, $identityType) {
$sanitized_identity = Html::escape($identity);
$sanitized_identity_type = Html::escape($identityType);
$this->htmlHeadContexts['identity:' . $sanitized_identity_type] = $sanitized_identity;
}
public function getCacheMaxAge() {
return $this->settings
->getCacheMaxAge();
}
public function getCacheContexts() {
return $this->settings
->getCacheContexts();
}
public function getCacheTags() {
return $this->settings
->getCacheTags();
}
}