You are here

class PathContext in Acquia Lift Connector 8.4

Same name and namespace in other branches
  1. 8 src/Service/Context/PathContext.php \Drupal\acquia_lift\Service\Context\PathContext
  2. 8.3 src/Service/Context/PathContext.php \Drupal\acquia_lift\Service\Context\PathContext

Hierarchy

Expanded class hierarchy of PathContext

1 file declares its use of PathContext
PathContextTest.php in tests/src/Unit/Service/Context/PathContextTest.php
1 string reference to 'PathContext'
acquia_lift.services.yml in ./acquia_lift.services.yml
acquia_lift.services.yml
1 service uses PathContext
acquia_lift.service.context.path_context in ./acquia_lift.services.yml
Drupal\acquia_lift\Service\Context\PathContext

File

src/Service/Context/PathContext.php, line 16

Namespace

Drupal\acquia_lift\Service\Context
View source
class PathContext extends BaseContext implements CacheableDependencyInterface {

  /**
   * Acquia Lift settings.
   *
   * @var \Drupal\Core\Config\ImmutableConfig
   */
  private $settings;

  /**
   * Acquia Lift credential settings.
   *
   * @var array
   */
  private $credentialSettings;

  /**
   * Identity settings.
   *
   * @var array
   */
  private $identitySettings;

  /**
   * Request path patterns (exclusion).
   *
   * @var array
   */
  private $requestPathPatterns;

  /**
   * Current path.
   *
   * @var string
   */
  private $currentPath;

  /**
   * Path matcher.
   *
   * @var \Drupal\acquia_lift\Service\Helper\PathMatcher
   */
  private $pathMatcher;

  /**
   * Constructor.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory service.
   * @param \Drupal\Core\Path\CurrentPathStack $current_path_stack
   *   The current path service.
   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
   *   The request stack.
   * @param \Drupal\acquia_lift\Service\Helper\PathMatcher $pathMatcher
   *   The path matcher.
   */
  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);
  }

  /**
   * Should attach.
   *
   * @return boolean
   *   True if should attach.
   */
  public function shouldAttach() {

    // Should not attach if credential is invalid.
    if (SettingsHelper::isInvalidCredential($this->credentialSettings)) {
      return FALSE;
    }

    // Should not attach if current path match the path patterns.
    if ($this->pathMatcher
      ->match($this->currentPath, $this->requestPathPatterns)) {
      return FALSE;
    }

    // Should attach.
    return TRUE;
  }

  /**
   * Set Path Context Identity by request stack's query parameters.
   *
   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
   *   The request stack.
   */
  private function setContextIdentityByRequest($request_stack) {

    // Stop, if there is no "identity parameter".
    $identity_parameter = $this->identitySettings['identity_parameter'];
    if (empty($identity_parameter)) {
      return;
    }

    // Set cache contexts. This is done as long as the identity parameter is set.
    $identity_type_parameter = $this->identitySettings['identity_type_parameter'];
    $query_names = [
      $identity_parameter,
      $identity_type_parameter,
    ];
    $this
      ->setContextCacheByQueryNames($query_names);

    // Find the current URL queries.
    $query_string = $request_stack
      ->getCurrentRequest()
      ->getQueryString();
    $parsed_query_string = UrlHelper::parse('?' . $query_string);
    $queries = $parsed_query_string['query'];

    // Stop, if there is no or empty identity parameter in the query string.
    if (empty($queries[$identity_parameter])) {
      return;
    }

    // Gather the identity and identity type by configuration.
    $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);
  }

  /**
   * Set Cache Context by query names.
   *
   * @todo Add implements CacheContextInterface instead of brewing our own.
   *
   * @param array $query_names
   *   The query names.
   */
  private function setContextCacheByQueryNames($query_names) {
    foreach ($query_names as $query_name) {
      if (empty($query_name)) {
        continue;
      }
      $this->cacheContexts[] = 'url.query_args:' . $query_name;
    }
  }

  /**
   * Set Context Identity by User.
   *
   * @param \Drupal\user\UserInterface $user
   *   User.
   */
  public function setContextIdentityByUser(UserInterface $user) {
    if (empty($this->identitySettings['capture_identity'])) {
      return;
    }
    $this
      ->setContextIdentity($user
      ->getEmail(), 'email');
  }

  /**
   * Set Context Identity.
   *
   * @param string $identity
   *   Identity.
   * @param string $identityType
   *   Identity type.
   */
  private function setContextIdentity($identity, $identityType) {

    // Sanitize string and output.
    $sanitized_identity = Html::escape($identity);
    $sanitized_identity_type = Html::escape($identityType);
    $this->htmlHeadContexts['identity:' . $sanitized_identity_type] = $sanitized_identity;
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheMaxAge() {
    return $this->settings
      ->getCacheMaxAge();
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheContexts() {
    return $this->settings
      ->getCacheContexts();
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheTags() {
    return $this->settings
      ->getCacheTags();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BaseContext::$cacheContexts protected property Cache contexts.
BaseContext::$htmlHeadContexts protected property HTML head contexts. 1
BaseContext::getMetaTagRenderArray protected function Get the render array for a single meta tag.
BaseContext::populate public function Populate page by context. Overrides ContextInterface::populate
BaseContext::populateCache protected function Populate page's cache context.
BaseContext::populateHtmlHead protected function Populate page's HTML head. 1
PathContext::$credentialSettings private property Acquia Lift credential settings.
PathContext::$currentPath private property Current path.
PathContext::$identitySettings private property Identity settings.
PathContext::$pathMatcher private property Path matcher.
PathContext::$requestPathPatterns private property Request path patterns (exclusion).
PathContext::$settings private property Acquia Lift settings.
PathContext::getCacheContexts public function The cache contexts associated with this object. Overrides CacheableDependencyInterface::getCacheContexts
PathContext::getCacheMaxAge public function The maximum age for which this object may be cached. Overrides CacheableDependencyInterface::getCacheMaxAge
PathContext::getCacheTags public function The cache tags associated with this object. Overrides CacheableDependencyInterface::getCacheTags
PathContext::setContextCacheByQueryNames private function Set Cache Context by query names.
PathContext::setContextIdentity private function Set Context Identity.
PathContext::setContextIdentityByRequest private function Set Path Context Identity by request stack's query parameters.
PathContext::setContextIdentityByUser public function Set Context Identity by User.
PathContext::shouldAttach public function Should attach.
PathContext::__construct public function Constructor.