Lines 22.22% 2 / 9
Methods 0.00% 0 / 2
Classes 0.00% 0 / 1
Name Lines Methods CRAP
 __construct n/a 0 / 0 n/a 0 / 0 3
 startProcessing n/a 0 / 0 n/a 0 / 0 5
 getArchivedList n/a 0 / 0 n/a 0 / 0 1
 logMessage n/a 0 / 0 n/a 0 / 0 1
 writeProcessInStatisticTable n/a 0 / 0 n/a 0 / 0 7
 [BO\Zmsbackend\Helper\VerboseCronLogTrait] resolveCronLogLevel 0.00% 0 / 5 0.00% 0 / 1 6
 [BO\Zmsbackend\Helper\VerboseCronLogTrait] writeVerboseCronLog 50.00% 2 / 4 0.00% 0 / 1 2.50
8class ArchivedDataIntoStatisticByCron
9{
10    use VerboseCronLogTrait;
11
12    protected $verbose = false;
13
14    protected $limit = 1000;
15
16    protected $query;
17
18    protected $timespan = "-7days";
19
20    protected $archivedList = [];
21
22    public function __construct(?int $limit = null, bool $verbose = false)
23    {
24        if ($verbose) {
25            $this->verbose = true;
26        }
27        $this->logMessage("INFO: Insert archived waiting, request and client data into statisik table");
28        $this->limit = ($limit) ? $limit : $this->limit;
29        $this->query = new \BO\Zmsbackend\Process\Service\ProcessStatusArchived();
30    }
31
32    public function startProcessing(\DateTimeImmutable $dateTime, bool $commit = false): void
33    {
34        $scopeList = (new \BO\Zmsbackend\Scope\Service\Scope())->readList(0);
35        $dateTime = $dateTime->modify($this->timespan);
36        foreach ($scopeList as $scope) {
37            $this->logMessage("INFO: Processing $scope");
38            $processList = $this->query->readListForStatistic($dateTime, $scope, $this->limit);
39            if ($processList->count()) {
40                $this->logMessage("INFO: " . count($processList) . " processes for $scope");
41                $cluster = (new \BO\Zmsbackend\Cluster\Service\Cluster())->readByScopeId($scope->getId());
42                $department = (new \BO\Zmsbackend\Department\Service\Department())->readByScopeId($scope->getId());
43                if ($department) {
44                    $organisation = (new \BO\Zmsbackend\Organisation\Service\Organisation())->readByDepartmentId($department->getId());
45                    $owner = (new \BO\Zmsbackend\Owner\Service\Owner())->readByOrganisationId($organisation->getId());
46                } else {
47                    $department = new \BO\Zmsentities\Department();
48                    $organisation = new \BO\Zmsentities\Organisation();
49                    $owner = new \BO\Zmsentities\Owner();
50                }
51                foreach ($processList as $process) {
52                    $this->writeProcessInStatisticTable(
53                        $process,
54                        $scope,
55                        $cluster,
56                        $department,
57                        $organisation,
58                        $owner,
59                        $dateTime,
60                        $commit
61                    );
62                }
63            } else {
64                $this->logMessage("INFO: No changes for scope $scope");
65            }
66        }
67        $this->logMessage("\nSUMMARY: number of archived processes: " . count($this->archivedList));
68    }
69
70    public function getArchivedList()
71    {
72        return $this->archivedList;
73    }
74
75    protected function logMessage(string $message, string $level = 'info'): void
76    {
77        $this->writeVerboseCronLog($message, $level);
78    }
79
80    protected function writeProcessInStatisticTable(
81        $process,
82        $scope,
83        $cluster,
84        $department,
85        $organisation,
86        $owner,
87        \DateTimeImmutable|false $dateTime,
88        bool $commit = false
89    ): void {
90        $requestIds = (new \BO\Zmsbackend\Request\Service\Request())
91            ->readRequestIdsByArchiveId($process->archiveId);
92        if (!count($requestIds)) {
93            // unsigned buergeranliegen.AnliegenID cannot store -1 ("ohne Erfassung")
94            $requestIds = [-1];
95        }
96        $processingTime = count($requestIds) === 1 ? $process->processingTime : null;
97
98        foreach ($requestIds as $requestId) {
99            $archived = true; // for verbose
100            if ($commit) {
101                $archived = $this->query->writeArchivedProcessToStatistic(
102                    $process,
103                    $requestId,
104                    $cluster ? $cluster->getId() : 0,
105                    $scope->toProperty()->provider->id->get(0),
106                    $department->getId(),
107                    $organisation->getId(),
108                    $owner->getId(),
109                    $dateTime,
110                    $processingTime
111                );
112            }
113            if ($archived) {
114                $this->archivedList['scope_' . $scope->getId()][] = $process->archiveId;
115                $processDate = $process->getFirstAppointment()->toDateTime()->format('Y-m-d');
116                $this->logMessage(
117                    "INFO: Process {$process->archiveId} with request {$requestId}"
118                    . " for scope {$scope->getId()} archived on $processDate"
119                );
120            } else {
121                $this->logMessage(
122                    "WARN: Could not archive process {$process->archiveId} with request {$requestId}!"
123                );
124            }
125        }
126    }
127}

From BO\Zmsbackend\Helper\VerboseCronLogTrait

5trait VerboseCronLogTrait
6{
7    /**
8     * @return array{0: string, 1: string}
9     */
10    protected static function resolveCronLogLevel(string $message, string $level): array
11    {
12        if (preg_match('/^(DEBUG|INFO|NOTICE|WARN(?:ING)?|ERROR|CRITICAL|ALERT|EMERGENCY):\s*(.*)$/s', $message, $matches)) {
13            $level = $matches[1];
14            $message = $matches[2];
15        }
16        $level = \BO\Slim\Bootstrap::normalizeLogLevelName($level);
17
18        return [$level, $message];
19    }
20
21    protected function writeVerboseCronLog(string $message, string $level = 'info'): void
22    {
23        if (!$this->verbose) {
24            return;
25        }
26        [$level, $message] = self::resolveCronLogLevel($message, $level);
27        \App::$log->{$level}($message);
28    }
29}