Lines 72.72% 16 / 22
Methods 0.00% 0 / 3
Classes 0.00% 0 / 1
Name Lines Methods CRAP
 __construct 33.33% 1 / 3 0.00% 0 / 1 3.19
 log 0.00% 0 / 2 0.00% 0 / 1 6
 startProcessing 88.23% 15 / 17 0.00% 0 / 1 3.01
14class AvailabilityHistoryCleanUpByCron
15{
16    protected bool $verbose = false;
17
18    public function __construct(bool $verbose = false)
19    {
20        if ($verbose) {
21            $this->verbose = true;
22            $this->log('INFO: Delete old availability_history entries');
23        }
24    }
25
26    protected function log(string $message): void
27    {
28        if ($this->verbose) {
29            \App::$log->info($message);
30        }
31    }
32
33    public function startProcessing(bool $commit = false): int
34    {
35        $config = (new ConfigRepository())->readEntity();
36        $olderThan = (int) ($config->getPreference('availability_history', 'deleteOlderThanDays')
37            ?? AvailabilityHistoryService::DEFAULT_RETENTION_DAYS);
38        if ($olderThan < 1) {
39            $olderThan = AvailabilityHistoryService::DEFAULT_RETENTION_DAYS;
40        }
41
42        $now = \DateTimeImmutable::createFromInterface(\App::$now ?? new \DateTimeImmutable('now'));
43        $cutoff = $now->modify('-' . $olderThan . ' days');
44
45        \App::$log->info('Starting availability_history cleanup', [
46            'olderThanDays' => $olderThan,
47            'cutoff' => $cutoff->format('Y-m-d H:i:s'),
48            'commit' => (bool) $commit,
49        ]);
50
51        if (!$commit) {
52            return 0;
53        }
54
55        $deleted = (new AvailabilityHistoryService())->deleteOlderThanDays($olderThan);
56        \App::$log->info('availability_history cleanup completed', ['deleted' => $deleted]);
57
58        return $deleted;
59    }
60}