Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
72.73% covered (warning)
72.73%
16 / 22
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
AvailabilityHistoryCleanUpByCron
72.73% covered (warning)
72.73%
16 / 22
0.00% covered (danger)
0.00%
0 / 3
7.99
0.00% covered (danger)
0.00%
0 / 1
 __construct
33.33% covered (danger)
33.33%
1 / 3
0.00% covered (danger)
0.00%
0 / 1
3.19
 log
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 startProcessing
88.24% covered (warning)
88.24%
15 / 17
0.00% covered (danger)
0.00%
0 / 1
3.01
1<?php
2
3/**
4 * @copyright BerlinOnline Stadtportal GmbH & Co. KG
5 **/
6
7declare(strict_types=1);
8
9namespace BO\Zmsbackend\Helper;
10
11use BO\Zmsbackend\Availability\Service\AvailabilityHistory as AvailabilityHistoryService;
12use BO\Zmsbackend\Config\Service\Config as ConfigRepository;
13
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}