Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
42 / 42
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
CronService
100.00% covered (success)
100.00%
42 / 42
100.00% covered (success)
100.00%
5 / 5
8
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 execute
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 deleteExpiredLogs
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
2
 deleteBotLogs
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
1 / 1
3
 dayCounter
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace Drupal\visitors\Service;
6
7use Drupal\Component\Datetime\TimeInterface;
8use Drupal\Core\Config\ConfigFactoryInterface;
9use Drupal\Core\Database\Connection;
10use Drupal\Core\State\StateInterface;
11use Drupal\visitors\VisitorsCounterInterface;
12use Drupal\visitors\VisitorsCronInterface;
13
14/**
15 * Visitors Cron Service.
16 */
17class CronService implements VisitorsCronInterface {
18
19  /**
20   * The database.
21   *
22   * @var \Drupal\Core\Database\Connection
23   */
24  protected $database;
25
26  /**
27   * The counter.
28   *
29   * @var \Drupal\visitors\VisitorsCounterInterface
30   */
31  protected $counter;
32
33  /**
34   * The state.
35   *
36   * @var \Drupal\Core\State\StateInterface
37   */
38  protected $state;
39
40  /**
41   * The time.
42   *
43   * @var \Drupal\Component\Datetime\TimeInterface
44   */
45  protected $time;
46
47  /**
48   * The settings.
49   *
50   * @var \Drupal\Core\Config\ImmutableConfig
51   */
52  protected $settings;
53
54  /**
55   * CronService constructor.
56   *
57   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
58   *   The config factory.
59   * @param \Drupal\Core\Database\Connection $database
60   *   The database.
61   * @param \Drupal\Core\State\StateInterface $state
62   *   The state.
63   * @param \Drupal\Component\Datetime\TimeInterface $time
64   *   The time.
65   * @param \Drupal\visitors\VisitorsCounterInterface $counter
66   *   The counter.
67   */
68  public function __construct(ConfigFactoryInterface $config_factory, Connection $database, StateInterface $state, TimeInterface $time, VisitorsCounterInterface $counter) {
69    $this->database = $database;
70    $this->state = $state;
71    $this->time = $time;
72    $this->counter = $counter;
73
74    $this->settings = $config_factory->get('visitors.settings');
75  }
76
77  /**
78   * {@inheritdoc}
79   */
80  public function execute() {
81
82    $this->deleteExpiredLogs();
83    $this->deleteBotLogs();
84    $this->dayCounter();
85
86  }
87
88  /**
89   * Delete expired logs.
90   */
91  protected function deleteExpiredLogs() {
92    $flush_log_timer = $this->settings->get('flush_log_timer') ?? 0;
93    if ($flush_log_timer == 0) {
94      return;
95    }
96    $now = $this->time->getRequestTime();
97    $delete_since = (string) $now - $flush_log_timer;
98    // Clean up expired access logs.
99    $this->database->delete('visitors_event')
100      ->condition('created', $delete_since, '<')
101      ->execute();
102    $this->database->delete('visitors_visit')
103      ->condition('exit_time', $delete_since, '<')
104      ->execute();
105  }
106
107  /**
108   * Delete bot logs.
109   */
110  protected function deleteBotLogs() {
111    $bot_retention_log = $this->settings->get('bot_retention_log') ?? 0;
112    $bot_retention_log = abs($bot_retention_log);
113    if ($bot_retention_log == 0) {
114      return;
115    }
116
117    $now = $this->time->getRequestTime();
118    $delete_since = (string) $now - $bot_retention_log;
119    if ($bot_retention_log == 1) {
120      $delete_since = '0';
121    }
122
123    // Clean up expired access logs.
124    $subquery = $this->database->select('visitors_visit', 'visit')
125      ->fields('visit', ['id'])
126      ->condition('visit.bot', 1)
127      ->condition('entry_time', $delete_since, '<');
128    $this->database->delete('visitors_event')
129      ->condition('visit_id', $subquery, 'IN')
130      ->condition('created', $delete_since, '<')
131      ->execute();
132
133    $this->database->delete('visitors_visit')
134      ->condition('bot', 1)
135      ->condition('exit_time', $delete_since, '<')
136      ->execute();
137  }
138
139  /**
140   * Reset the day count.
141   */
142  protected function dayCounter() {
143    $this->counter->resetDayCount();
144    $max_total_count = $this->counter->maxTotalCount('node');
145    $this->state->set('visitors.node_counter_scale', 1.0 / max(1.0, $max_total_count));
146  }
147
148}