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
18
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
2
 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    protected $verbose = false;
11
12    protected $limit = 1000;
13
14    protected $query;
15
16    protected $timespan = "-7days";
17
18    protected $archivedList = [];
19
20    public function __construct($limit = null, $verbose = false)
21    {
22        if ($verbose) {
23            $this->verbose = true;
24        }
25        $this->logMessage("INFO: Insert archived waiting, request and client data into statisik table");
26        $this->limit = ($limit) ? $limit : $this->limit;
27        $this->query = new \BO\Zmsdb\ProcessStatusArchived();
28    }
29
30    public function startProcessing(\DateTimeImmutable $dateTime, $commit = false)
31    {
32        $scopeList = (new \BO\Zmsdb\Scope())->readList(0);
33        $dateTime = $dateTime->modify($this->timespan);
34        foreach ($scopeList as $scope) {
35            $this->logMessage("INFO: Processing $scope");
36            $processList = $this->query->readListForStatistic($dateTime, $scope, $this->limit);
37            if ($processList->count()) {
38                $this->logMessage("INFO: " . count($processList) . " processes for $scope");
39                $cluster = (new \BO\Zmsdb\Cluster())->readByScopeId($scope->getId());
40                $department = (new \BO\Zmsdb\Department())->readByScopeId($scope->getId());
41                if ($department) {
42                    $organisation = (new \BO\Zmsdb\Organisation())->readByDepartmentId($department->getId());
43                    $owner = (new \BO\Zmsdb\Owner())->readByOrganisationId($organisation->getId());
44                } else {
45                    $department = new \BO\Zmsentities\Department();
46                    $organisation = new \BO\Zmsentities\Organisation();
47                    $owner = new \BO\Zmsentities\Owner();
48                }
49                foreach ($processList as $process) {
50                    $this->writeProcessInStatisticTable(
51                        $process,
52                        $scope,
53                        $cluster,
54                        $department,
55                        $organisation,
56                        $owner,
57                        $dateTime,
58                        $commit
59                    );
60                }
61            } else {
62                $this->logMessage("INFO: No changes for scope $scope");
63            }
64        }
65        $this->logMessage("\nSUMMARY: number of archived processes: " . count($this->archivedList));
66    }
67
68    public function getArchivedList()
69    {
70        return $this->archivedList;
71    }
72
73    protected function logMessage($message)
74    {
75        if ($this->verbose) {
76            error_log($message);
77        }
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}