Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
96.43% |
27 / 28 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| ProcessSearchHistoryCleanUp | |
96.43% |
27 / 28 |
|
0.00% |
0 / 1 |
3 | |
0.00% |
0 / 1 |
| startProcessing | |
96.43% |
27 / 28 |
|
0.00% |
0 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace BO\Zmsbackend\Helper; |
| 4 | |
| 5 | use BO\Zmsbackend\Config\Service\Config as ConfigService; |
| 6 | use BO\Zmsbackend\ProcessSearchHistory\Service\ProcessSearchHistory as HistoryService; |
| 7 | |
| 8 | class ProcessSearchHistoryCleanUp |
| 9 | { |
| 10 | private const int DEFAULT_RETENTION_DAYS = 90; |
| 11 | |
| 12 | public function startProcessing( |
| 13 | \DateTimeInterface $now, |
| 14 | bool $commit = false |
| 15 | ): void { |
| 16 | $config = (new ConfigService())->readEntity(); |
| 17 | |
| 18 | $retentionDays = filter_var( |
| 19 | $config->getPreference( |
| 20 | 'processSearchHistory', |
| 21 | 'deleteOlderThanDays' |
| 22 | ), |
| 23 | FILTER_VALIDATE_INT, |
| 24 | [ |
| 25 | 'options' => [ |
| 26 | 'min_range' => 1, |
| 27 | ], |
| 28 | ] |
| 29 | ); |
| 30 | |
| 31 | if ($retentionDays === false) { |
| 32 | $retentionDays = self::DEFAULT_RETENTION_DAYS; |
| 33 | } |
| 34 | |
| 35 | $cutoff = \DateTimeImmutable::createFromInterface($now) |
| 36 | ->modify('-' . $retentionDays . ' days'); |
| 37 | |
| 38 | \App::$log->info( |
| 39 | 'Process search history cleanup', |
| 40 | [ |
| 41 | 'retentionDays' => $retentionDays, |
| 42 | 'olderThan' => $cutoff->format('Y-m-d H:i:s'), |
| 43 | ] |
| 44 | ); |
| 45 | |
| 46 | if (!$commit) { |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | $historyService = new HistoryService(); |
| 51 | $historyService->deleteOlderThan($cutoff); |
| 52 | } |
| 53 | } |