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\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Path\CurrentPathStack;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\acquia_vwo\Service\Condition\ConditionResolver;
use Drupal\acquia_vwo\Service\Helper\PathMatcher;
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;
/**
* The config factory service.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
private $configFactory;
/**
* Current path.
*
* @var string
*/
private $currentPath;
/**
* Path matcher.
*
* @var \Drupal\acquia_vwo\Service\Helper\PathMatcher
*/
private $pathMatcher;
/**
* 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->configFactory = $config_factory;
$this->currentPath = $current_path_stack->getPath();
$this->account = $account;
$this->userData = $user_data;
$this->routeMatch = $route_match;
$this->pathMatcher = $pathMatcher;
$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() {
$vwo_config_settings = $this->configFactory->get('acquia_vwo.settings');
// Check if Account id is configured.
if (empty($vwo_config_settings->get('id'))) {
return FALSE;
}
// Check visibility settings.
$visibility_config = $vwo_config_settings->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 = $vwo_config_settings->get('visibility.conditions');
return $this->conditionResolver->evaluateConditions($conditions_config);
}
}
