Lines
96.42%
27 / 28
Methods
0.00%
0 / 1
Classes
0.00%
0 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| startProcessing | 96.42% | 27 / 28 | 0.00% | 0 / 1 | 3 | ||
| 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 | } |