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\Zmsdb\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\Zmsdb\ProcessStatusArchived();
30    }
31
32    public function startProcessing(\DateTimeImmutable $dateTime, $commit = false)
33    {
34        $scopeList = (new \BO\Zmsdb\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\Zmsdb\Cluster())->readByScopeId($scope->getId());
42                $department = (new \BO\Zmsdb\Department())->readByScopeId($scope->getId());
43                if ($department) {
44                    $organisation = (new \BO\Zmsdb\Organisation())->readByDepartmentId($department->getId());
45                    $owner = (new \BO\Zmsdb\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        $commit = false
89    ) {
90        $requestList = (new \BO\Zmsdb\Request())->readRequestByArchiveId($process->archiveId);
91        $processingTime = null;
92        if ($requestList->count()) {
93            $processingTime = $requestList->count() === 1 ? $process->processingTime : null;
94        } else {
95            $requestList = [new \BO\Zmsentities\Request(['id' => '-1'])];
96        }
97
98        foreach ($requestList as $request) {
99            $archived = true; // for verbose
100            if ($commit) {
101                $archived = $this->query->writeArchivedProcessToStatistic(
102                    $process,
103                    $request->getId(),
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 {$request->getId()}"
118                    . " for scope {$scope->getId()} archived on $processDate"
119                );
120            } else {
121                $this->logMessage(
122                    "WARN: Could not archive process {$process->archiveId} with request {$request->getId()}!"
123                );
124            }
125        }
126    }
127}