Lines: 100.00%
| Line | Hits | Source |
|---|---|---|
| 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) { | |
| 69 | 7 | $this->database = $database; |
| 70 | 7 | $this->state = $state; |
| 71 | 7 | $this->time = $time; |
| 72 | 7 | $this->counter = $counter; |
| 73 | ||
| 74 | 7 | $this->settings = $config_factory->get('visitors.settings'); |
| 75 | } | |
| 76 | ||
| 77 | /** | |
| 78 | * {@inheritdoc} | |
| 79 | */ | |
| 80 | public function execute() { | |
| 81 | ||
| 82 | 1 | $this->deleteExpiredLogs(); |
| 83 | 1 | $this->deleteBotLogs(); |
| 84 | 1 | $this->dayCounter(); |
| 85 | ||
| 86 | } | |
| 87 | ||
| 88 | /** | |
| 89 | * Delete expired logs. | |
| 90 | */ | |
| 91 | protected function deleteExpiredLogs() { | |
| 92 | 3 | $flush_log_timer = $this->settings->get('flush_log_timer') ?? 0; |
| 93 | 3 | if ($flush_log_timer == 0) { |
| 94 | 1 | return; |
| 95 | } | |
| 96 | 2 | $now = $this->time->getRequestTime(); |
| 97 | 2 | $delete_since = (string) $now - $flush_log_timer; |
| 98 | // Clean up expired access logs. | |
| 99 | 2 | $this->database->delete('visitors_event') |
| 100 | 2 | ->condition('created', $delete_since, '<') |
| 101 | 2 | ->execute(); |
| 102 | 2 | $this->database->delete('visitors_visit') |
| 103 | 2 | ->condition('exit_time', $delete_since, '<') |
| 104 | 2 | ->execute(); |
| 105 | } | |
| 106 | ||
| 107 | /** | |
| 108 | * Delete bot logs. | |
| 109 | */ | |
| 110 | protected function deleteBotLogs() { | |
| 111 | 3 | $bot_retention_log = $this->settings->get('bot_retention_log') ?? 0; |
| 112 | 3 | $bot_retention_log = abs($bot_retention_log); |
| 113 | 3 | if ($bot_retention_log == 0) { |
| 114 | 1 | return; |
| 115 | } | |
| 116 | ||
| 117 | 2 | $now = $this->time->getRequestTime(); |
| 118 | 2 | $delete_since = (string) $now - $bot_retention_log; |
| 119 | 2 | if ($bot_retention_log == 1) { |
| 120 | 1 | $delete_since = '0'; |
| 121 | } | |
| 122 | ||
| 123 | // Clean up expired access logs. | |
| 124 | 2 | $subquery = $this->database->select('visitors_visit', 'visit') |
| 125 | 2 | ->fields('visit', ['id']) |
| 126 | 2 | ->condition('visit.bot', 1) |
| 127 | 2 | ->condition('entry_time', $delete_since, '<'); |
| 128 | 2 | $this->database->delete('visitors_event') |
| 129 | 2 | ->condition('visit_id', $subquery, 'IN') |
| 130 | 2 | ->condition('created', $delete_since, '<') |
| 131 | 2 | ->execute(); |
| 132 | ||
| 133 | 2 | $this->database->delete('visitors_visit') |
| 134 | 2 | ->condition('bot', 1) |
| 135 | 2 | ->condition('exit_time', $delete_since, '<') |
| 136 | 2 | ->execute(); |
| 137 | } | |
| 138 | ||
| 139 | /** | |
| 140 | * Reset the day count. | |
| 141 | */ | |
| 142 | protected function dayCounter() { | |
| 143 | 2 | $this->counter->resetDayCount(); |
| 144 | 2 | $max_total_count = $this->counter->maxTotalCount('node'); |
| 145 | 2 | $this->state->set('visitors.node_counter_scale', 1.0 / max(1.0, $max_total_count)); |
| 146 | } | |
| 147 | ||
| 148 | } |