Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
31 / 31 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
CronService | |
100.00% |
31 / 31 |
|
100.00% |
5 / 5 |
8 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
execute | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
deleteExpiredLogs | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
2 | |||
deleteBotLogs | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
3 | |||
dayCounter | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace Drupal\visitors\Service; |
6 | |
7 | use Drupal\Core\Config\ConfigFactoryInterface; |
8 | use Drupal\Core\Database\Connection; |
9 | use Drupal\Core\State\StateInterface; |
10 | use Drupal\visitors\VisitorsCounterInterface; |
11 | use Drupal\visitors\VisitorsCronInterface; |
12 | use Drupal\Component\Datetime\TimeInterface; |
13 | |
14 | /** |
15 | * Visitors Cron Service. |
16 | */ |
17 | class 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.config'); |
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') |
100 | ->condition('visitors_date_time', $delete_since, '<') |
101 | ->execute(); |
102 | } |
103 | |
104 | /** |
105 | * Delete bot logs. |
106 | */ |
107 | protected function deleteBotLogs() { |
108 | $bot_retention_log = $this->settings->get('bot_retention_log') ?? 0; |
109 | $bot_retention_log = abs($bot_retention_log); |
110 | if ($bot_retention_log == 0) { |
111 | return; |
112 | } |
113 | |
114 | $now = $this->time->getRequestTime(); |
115 | $delete_since = (string) $now - $bot_retention_log; |
116 | if ($bot_retention_log == 1) { |
117 | $delete_since = '0'; |
118 | } |
119 | |
120 | $this->database->delete('visitors') |
121 | ->condition('bot', 1) |
122 | ->condition('visitors_date_time', $delete_since, '<') |
123 | ->execute(); |
124 | } |
125 | |
126 | /** |
127 | * Reset the day count. |
128 | */ |
129 | protected function dayCounter() { |
130 | $this->counter->resetDayCount(); |
131 | $max_total_count = $this->counter->maxTotalCount('node'); |
132 | $this->state->set('visitors.node_counter_scale', 1.0 / max(1.0, $max_total_count)); |
133 | } |
134 | |
135 | } |