Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
n/a
0 / 0
n/a
0 / 0
CRAP
n/a
0 / 0
ArchivedDataIntoStatisticByCron
n/a
0 / 0
n/a
0 / 0
17
n/a
0 / 0
 __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
1<?php
2
3namespace BO\Zmsbackend\Helper;
4
5/**
6 * @codeCoverageIgnore
7 */
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($limit = null, $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($message, string $level = 'info')
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        $dateTime,
88        bool $commit = false
89    ) {
90        $requestIds = (new \BO\Zmsbackend\Request\Service\Request())
91            ->readRequestIdsByArchiveId($process->archiveId);
92        $processingTime = null;
93        if (count($requestIds)) {
94            $processingTime = count($requestIds) === 1 ? $process->processingTime : null;
95        } else {
96            $requestIds = [-1];
97        }
98
99        foreach ($requestIds as $requestId) {
100            $archived = true; // for verbose
101            if ($commit) {
102                $archived = $this->query->writeArchivedProcessToStatistic(
103                    $process,
104                    $requestId,
105                    $cluster ? $cluster->getId() : 0,
106                    $scope->toProperty()->provider->id->get(0),
107                    $department->getId(),
108                    $organisation->getId(),
109                    $owner->getId(),
110                    $dateTime,
111                    $processingTime
112                );
113            }
114            if ($archived) {
115                $this->archivedList['scope_' . $scope->getId()][] = $process->archiveId;
116                $processDate = $process->getFirstAppointment()->toDateTime()->format('Y-m-d');
117                $this->logMessage(
118                    "INFO: Process {$process->archiveId} with request {$requestId}"
119                    . " for scope {$scope->getId()} archived on $processDate"
120                );
121            } else {
122                $this->logMessage(
123                    "WARN: Could not archive process {$process->archiveId} with request {$requestId}!"
124                );
125            }
126        }
127    }
128}