acquia_vwo-1.0.x-dev/src/Service/Context/VisibilityContext.php
src/Service/Context/VisibilityContext.php
<?php namespace Drupal\acquia_vwo\Service\Context; use Drupal\acquia_vwo\Service\Condition\ConditionResolver; use Drupal\acquia_vwo\Service\Helper\PathMatcher; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Path\CurrentPathStack; use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Session\AccountInterface; use Drupal\user\UserDataInterface; /** * Path Context class. */ class VisibilityContext { /** * The condition resolver. * * @var \Drupal\acquia_vwo\Service\Condition\ConditionResolver */ protected ConditionResolver $conditionResolver; /** * The account interface. * * @var \Drupal\Core\Session\AccountInterface */ protected $account; /** * The user data service. * * @var \Drupal\user\UserDataInterface */ protected $userData; /** * The route match service. * * @var \Drupal\Core\Routing\RouteMatchInterface */ protected $routeMatch; /** * Acquia VWO settings. * * @var \Drupal\Core\Config\ImmutableConfig */ private $config; /** * Current path. * * @var string */ private $currentPath; /** * Path matcher. * * @var \Drupal\acquia_vwo\Service\Helper\ */ private $pathMatcher; /** * The array of cache contexts. * * @var array */ private $cacheContexts = []; /** * Constructor for VisibilityContext. * * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory * The config factory service. * @param \Drupal\Core\Path\CurrentPathStack $current_path_stack * The current path service. * @param \Drupal\Core\Session\AccountInterface $account * The current user. * @param \Drupal\user\UserDataInterface $user_data * The user data service. * @param \Drupal\Core\Routing\RouteMatchInterface $route_match * The current route match. * @param \Drupal\acquia_vwo\Service\Helper\PathMatcher $pathMatcher * The path matcher. * @param \Drupal\acquia_vwo\Service\Condition\ConditionResolver $condition_resolver * The Condition Resolver for building the insertion conditions. */ public function __construct( ConfigFactoryInterface $config_factory, CurrentPathStack $current_path_stack, AccountInterface $account, UserDataInterface $user_data, RouteMatchInterface $route_match, PathMatcher $pathMatcher, ConditionResolver $condition_resolver, ) { $this->config = $config_factory->get('acquia_vwo.settings'); $this->currentPath = $current_path_stack->getPath(); $this->account = $account; $this->userData = $user_data; $this->routeMatch = $route_match; $this->pathMatcher = $pathMatcher; $this->cacheContexts = $this->config->getCacheContexts(); $this->conditionResolver = $condition_resolver; } /** * Checks is we should attach Acquia VWO scripts to the page. * * @return bool * True if it should attach. */ public function shouldAttach() { // Check if Account id is configured. if (empty($this->config->get('id'))) { return FALSE; } // Check visibility settings. $visibility_config = $this->config->get('visibility'); // Skip evaluation of conditions when the module is configured // to add the VWO scripts to every page. if ($visibility_config['enabled'] != 'on') { return TRUE; } $conditions_config = $this->config->get('visibility.conditions'); return $this->conditionResolver->evaluateConditions($conditions_config); } }