Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
VisibilityService
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
4 / 4
8
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 isVisible
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 evaluateTrackingConditions
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 getTrackingConditions
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3declare(strict_types=1);
4
5namespace Drupal\visitors\Service;
6
7use Drupal\Core\Condition\ConditionAccessResolverTrait;
8use Drupal\Core\Condition\ConditionPluginCollection;
9use Drupal\Core\Config\ConfigFactoryInterface;
10use Drupal\Core\Executable\ExecutableManagerInterface;
11use Drupal\Core\Plugin\Context\ContextHandlerInterface;
12use Drupal\Core\Plugin\Context\ContextRepositoryInterface;
13use Drupal\visitors\VisitorsVisibilityInterface;
14
15/**
16 * Service for checking visitors visibility.
17 */
18final class VisibilityService implements VisitorsVisibilityInterface {
19
20  use ConditionAccessResolverTrait;
21
22  /**
23   * The config object.
24   *
25   * @var \Drupal\Core\Config\ImmutableConfig
26   */
27  protected $config;
28
29  /**
30   * The condition plugin manager.
31   *
32   * @var \Drupal\Core\Executable\ExecutableManagerInterface
33   */
34  protected ExecutableManagerInterface $conditionManager;
35
36  /**
37   * The context repository.
38   *
39   * @var \Drupal\Core\Plugin\Context\ContextRepositoryInterface
40   */
41  protected ContextRepositoryInterface $contextRepository;
42
43  /**
44   * The context handler.
45   *
46   * @var \Drupal\Core\Plugin\Context\ContextHandlerInterface
47   */
48  protected ContextHandlerInterface $contextHandler;
49
50  /**
51   * The visibility condition collection.
52   *
53   * @var \Drupal\Core\Condition\ConditionPluginCollection|null
54   */
55  protected ?ConditionPluginCollection $conditionCollection = NULL;
56
57  /**
58   * Constructs a new VisibilityService.
59   *
60   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
61   *   The config factory.
62   * @param \Drupal\Core\Executable\ExecutableManagerInterface $condition_manager
63   *   The condition plugin manager.
64   * @param \Drupal\Core\Plugin\Context\ContextRepositoryInterface $context_repository
65   *   The context repository.
66   * @param \Drupal\Core\Plugin\Context\ContextHandlerInterface $context_handler
67   *   The context handler.
68   */
69  public function __construct(
70    ConfigFactoryInterface $config_factory,
71    ExecutableManagerInterface $condition_manager,
72    ContextRepositoryInterface $context_repository,
73    ContextHandlerInterface $context_handler,
74  ) {
75    $this->config            = $config_factory->get('visitors.settings');
76    $this->conditionManager  = $condition_manager;
77    $this->contextRepository = $context_repository;
78    $this->contextHandler    = $context_handler;
79  }
80
81  /**
82   * {@inheritdoc}
83   */
84  public function isVisible(): bool {
85
86    if ($this->config->get('disable_tracking')) {
87      return FALSE;
88    }
89
90    return $this->evaluateTrackingConditions();
91  }
92
93  /**
94   * Evaluates tracking conditions using the condition plugin system.
95   *
96   * @return bool
97   *   TRUE if all conditions pass (AND logic), FALSE otherwise.
98   */
99  protected function evaluateTrackingConditions(): bool {
100    $conditions = $this->getTrackingConditions();
101
102    if ($conditions->count() === 0) {
103      return TRUE;
104    }
105
106    return $this->resolveConditions(iterator_to_array($conditions), 'and');
107  }
108
109  /**
110   * Gets the tracking condition plugin collection.
111   *
112   * @return \Drupal\Core\Condition\ConditionPluginCollection
113   *   The condition plugin collection.
114   */
115  protected function getTrackingConditions(): ConditionPluginCollection {
116    if (!isset($this->conditionCollection)) {
117      $conditions = $this->config->get('tracking_conditions') ?: [];
118      $this->conditionCollection = new ConditionPluginCollection(
119        $this->conditionManager,
120        $conditions
121      );
122    }
123    return $this->conditionCollection;
124  }
125
126}