Coverage index

src/Service/CronService.php

Lines: 100.00%

LineHitsSource
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Drupal\visitors\Service;
6
7
use Drupal\Component\Datetime\TimeInterface;
8
use Drupal\Core\Config\ConfigFactoryInterface;
9
use Drupal\Core\Database\Connection;
10
use Drupal\Core\State\StateInterface;
11
use Drupal\visitors\VisitorsCounterInterface;
12
use Drupal\visitors\VisitorsCronInterface;
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) {
697
    $this->database = $database;
707
    $this->state = $state;
717
    $this->time = $time;
727
    $this->counter = $counter;
73
747
    $this->settings = $config_factory->get('visitors.settings');
75
  }
76
77
  /**
78
   * {@inheritdoc}
79
   */
80
  public function execute() {
81
821
    $this->deleteExpiredLogs();
831
    $this->deleteBotLogs();
841
    $this->dayCounter();
85
86
  }
87
88
  /**
89
   * Delete expired logs.
90
   */
91
  protected function deleteExpiredLogs() {
923
    $flush_log_timer = $this->settings->get('flush_log_timer') ?? 0;
933
    if ($flush_log_timer == 0) {
941
      return;
95
    }
962
    $now = $this->time->getRequestTime();
972
    $delete_since = (string) $now - $flush_log_timer;
98
    // Clean up expired access logs.
992
    $this->database->delete('visitors_event')
1002
      ->condition('created', $delete_since, '<')
1012
      ->execute();
1022
    $this->database->delete('visitors_visit')
1032
      ->condition('exit_time', $delete_since, '<')
1042
      ->execute();
105
  }
106
107
  /**
108
   * Delete bot logs.
109
   */
110
  protected function deleteBotLogs() {
1113
    $bot_retention_log = $this->settings->get('bot_retention_log') ?? 0;
1123
    $bot_retention_log = abs($bot_retention_log);
1133
    if ($bot_retention_log == 0) {
1141
      return;
115
    }
116
1172
    $now = $this->time->getRequestTime();
1182
    $delete_since = (string) $now - $bot_retention_log;
1192
    if ($bot_retention_log == 1) {
1201
      $delete_since = '0';
121
    }
122
123
    // Clean up expired access logs.
1242
    $subquery = $this->database->select('visitors_visit', 'visit')
1252
      ->fields('visit', ['id'])
1262
      ->condition('visit.bot', 1)
1272
      ->condition('entry_time', $delete_since, '<');
1282
    $this->database->delete('visitors_event')
1292
      ->condition('visit_id', $subquery, 'IN')
1302
      ->condition('created', $delete_since, '<')
1312
      ->execute();
132
1332
    $this->database->delete('visitors_visit')
1342
      ->condition('bot', 1)
1352
      ->condition('exit_time', $delete_since, '<')
1362
      ->execute();
137
  }
138
139
  /**
140
   * Reset the day count.
141
   */
142
  protected function dayCounter() {
1432
    $this->counter->resetDayCount();
1442
    $max_total_count = $this->counter->maxTotalCount('node');
1452
    $this->state->set('visitors.node_counter_scale', 1.0 / max(1.0, $max_total_count));
146
  }
147
148
}