Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
AnonymizeStatisticDataByCron
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 2
30
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
6
 startAnonymizing
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3namespace BO\Zmsdb\Helper;
4
5use BO\Zmsdb\ProcessStatusArchived;
6use BO\Zmsdb\Config as ConfigRepository;
7
8class AnonymizeStatisticDataByCron
9{
10    protected $verbose = false;
11
12    protected $timespan = '-90 days';
13
14    public function __construct($verbose = false)
15    {
16        $this->verbose = $verbose;
17
18        // Fetching the configuration setting
19        $config = (new ConfigRepository())->readEntity();
20
21        // Extracting the retention setting and converting it to an integer
22        $retentionSetting = explode(',', $config->getPreference('buergerarchiv', 'setRetentionPeriodDays'));
23        if ($retentionSetting[0] !== "none") {
24            $this->timespan = (int)$retentionSetting[0];
25            \App::$log->info('Using retention period from admin system config', [
26                'retentionDays' => $this->timespan,
27            ]);
28        } else {
29            $this->timespan = 90;
30            \App::$log->info('Using default retention period', [
31                'retentionDays' => $this->timespan,
32            ]);
33        }
34    }
35
36    public function startAnonymizing(\DateTimeImmutable $currentDate, $commit = false)
37    {
38        // Adjust the currentDate based on the numeric timespan
39        $targetDate = $currentDate->modify("-{$this->timespan} days");
40        \App::$log->info('Beginning anonymization', [
41            'olderThan' => $targetDate->format('Y-m-d'),
42            'retentionDays' => $this->timespan,
43        ]);
44
45        if ($commit) {
46            $processStatusArchived = new ProcessStatusArchived();
47            $success = $processStatusArchived->anonymizeNames($targetDate);
48            if ($success) {
49                \App::$log->info('Anonymization process completed successfully', [
50                    'olderThan' => $targetDate->format('Y-m-d'),
51                ]);
52            } else {
53                \App::$log->error('Anonymization process failed', [
54                    'olderThan' => $targetDate->format('Y-m-d'),
55                ]);
56            }
57        } else {
58            \App::$log->notice('Anonymization dry run — no database changes', [
59                'olderThan' => $targetDate->format('Y-m-d'),
60            ]);
61        }
62    }
63}