Coverage index

src/Service/VisibilityService.php

Lines: 100.00%

LineHitsSource
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Drupal\visitors\Service;
6
7
use Drupal\Core\Condition\ConditionAccessResolverTrait;
8
use Drupal\Core\Condition\ConditionPluginCollection;
9
use Drupal\Core\Config\ConfigFactoryInterface;
10
use Drupal\Core\Executable\ExecutableManagerInterface;
11
use Drupal\Core\Plugin\Context\ContextHandlerInterface;
12
use Drupal\Core\Plugin\Context\ContextRepositoryInterface;
13
use Drupal\Core\Session\AccountInterface;
14
use Drupal\user\UserDataInterface;
15
use Drupal\visitors\VisitorsVisibilityInterface;
16
17
/**
18
 * Service for checking visitors visibility.
19
 */
20
final class VisibilityService implements VisitorsVisibilityInterface {
21
22
  use ConditionAccessResolverTrait;
23
24
  /**
25
   * The config object.
26
   *
27
   * @var \Drupal\Core\Config\ImmutableConfig
28
   */
29
  protected $config;
30
31
  /**
32
   * The condition plugin manager.
33
   *
34
   * @var \Drupal\Core\Executable\ExecutableManagerInterface
35
   */
36
  protected ExecutableManagerInterface $conditionManager;
37
38
  /**
39
   * The context repository.
40
   *
41
   * @var \Drupal\Core\Plugin\Context\ContextRepositoryInterface
42
   */
43
  protected ContextRepositoryInterface $contextRepository;
44
45
  /**
46
   * The context handler.
47
   *
48
   * @var \Drupal\Core\Plugin\Context\ContextHandlerInterface
49
   */
50
  protected ContextHandlerInterface $contextHandler;
51
52
  /**
53
   * The visibility condition collection.
54
   *
55
   * @var \Drupal\Core\Condition\ConditionPluginCollection|null
56
   */
57
  protected ?ConditionPluginCollection $conditionCollection = NULL;
58
59
  /**
60
   * The current user.
61
   *
62
   * @var \Drupal\Core\Session\AccountInterface
63
   */
64
  protected AccountInterface $currentUser;
65
66
  /**
67
   * The user data service.
68
   *
69
   * @var \Drupal\user\UserDataInterface
70
   */
71
  protected UserDataInterface $userData;
72
73
  /**
74
   * Constructs a new VisibilityService.
75
   *
76
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
77
   *   The config factory.
78
   * @param \Drupal\Core\Executable\ExecutableManagerInterface $condition_manager
79
   *   The condition plugin manager.
80
   * @param \Drupal\Core\Plugin\Context\ContextRepositoryInterface $context_repository
81
   *   The context repository.
82
   * @param \Drupal\Core\Plugin\Context\ContextHandlerInterface $context_handler
83
   *   The context handler.
84
   * @param \Drupal\Core\Session\AccountInterface $current_user
85
   *   The current user.
86
   * @param \Drupal\user\UserDataInterface $user_data
87
   *   The user data service.
88
   */
89
  public function __construct(
90
    ConfigFactoryInterface $config_factory,
91
    ExecutableManagerInterface $condition_manager,
92
    ContextRepositoryInterface $context_repository,
93
    ContextHandlerInterface $context_handler,
94
    AccountInterface $current_user,
95
    UserDataInterface $user_data,
96
  ) {
978
    $this->config            = $config_factory->get('visitors.settings');
988
    $this->conditionManager  = $condition_manager;
998
    $this->contextRepository = $context_repository;
1008
    $this->contextHandler    = $context_handler;
1018
    $this->currentUser       = $current_user;
1028
    $this->userData          = $user_data;
103
  }
104
105
  /**
106
   * {@inheritdoc}
107
   */
108
  public function isVisible(): bool {
109
1108
    if ($this->config->get('disable_tracking')) {
1111
      return FALSE;
112
    }
113
1148
    if ($this->isUserExcluded()) {
1153
      return FALSE;
116
    }
117
1188
    return $this->evaluateTrackingConditions();
119
  }
120
121
  /**
122
   * Whether the current account is excluded from tracking.
123
   *
124
   * Anonymous users have no account choice. User 1 is excluded only when
125
   * that setting is saved and enabled.
126
   *
127
   * @return bool
128
   *   TRUE when this account must not be tracked.
129
   */
130
  protected function isUserExcluded(): bool {
1318
    $exclude_user1 = (bool) $this->config->get('visibility.exclude_user1');
1328
    if ($exclude_user1 && (int) $this->currentUser->id() === 1) {
1331
      return TRUE;
134
    }
135
1368
    $mode = (int) $this->config->get('visibility.user_account_mode');
1378
    if ($mode === VisitorsVisibilityInterface::USER_NO_PERSONALIZATION) {
1384
      return FALSE;
139
    }
1406
    if (!$this->currentUser->isAuthenticated()) {
1414
      return FALSE;
142
    }
143
1442
    $stored = $this->userData->get(
1452
      'visitors',
1462
      (int) $this->currentUser->id(),
1472
      'user_account_users',
1482
    );
149
    // Opt-in mode tracks only an explicit choice to enable tracking.
1502
    if ($stored === NULL) {
1511
      return $mode === VisitorsVisibilityInterface::USER_OPT_IN;
152
    }
153
1542
    return (int) $stored !== 1;
155
  }
156
157
  /**
158
   * Evaluates tracking conditions using the condition plugin system.
159
   *
160
   * @return bool
161
   *   TRUE if all conditions pass (AND logic), FALSE otherwise.
162
   */
163
  protected function evaluateTrackingConditions(): bool {
1648
    $conditions = $this->getTrackingConditions();
165
1668
    if ($conditions->count() === 0) {
1678
      return TRUE;
168
    }
169
1703
    return $this->resolveConditions(iterator_to_array($conditions), 'and');
171
  }
172
173
  /**
174
   * Gets the tracking condition plugin collection.
175
   *
176
   * @return \Drupal\Core\Condition\ConditionPluginCollection
177
   *   The condition plugin collection.
178
   */
179
  protected function getTrackingConditions(): ConditionPluginCollection {
1808
    if (!isset($this->conditionCollection)) {
1818
      $conditions = $this->config->get('tracking_conditions') ?: [];
182
      // Theme has no empty option, so a saved value would limit tracking
183
      // to one theme. Ignore it even when older config still stores it.
1848
      unset($conditions['current_theme']);
1858
      $this->conditionCollection = new ConditionPluginCollection(
1868
        $this->conditionManager,
1878
        $conditions
1888
      );
189
    }
1908
    return $this->conditionCollection;
191
  }
192
193
}