Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
82.91% covered (warning)
82.91%
97 / 117
66.67% covered (warning)
66.67%
10 / 15
CRAP
0.00% covered (danger)
0.00%
0 / 1
ProcessStatusArchived
82.91% covered (warning)
82.91%
97 / 117
66.67% covered (warning)
66.67%
10 / 15
29.38
0.00% covered (danger)
0.00%
0 / 1
 readArchivedEntity
88.89% covered (warning)
88.89%
8 / 9
0.00% covered (danger)
0.00%
0 / 1
2.01
 readListByScopeId
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 readListByDate
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 readListByScopeAndDate
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 readListByScopesAndDates
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 deleteAllToday
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 readListForStatistic
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 readListIsMissed
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 readListWithAppointment
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 readResolvedList
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
5
 writeEntityFinished
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
5
 writeArchivedProcessToStatistic
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
2
 writeNewArchivedProcess
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
2
 writeXRequestsArchived
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 anonymizeNames
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace BO\Zmsdb;
4
5use BO\Zmsentities\Processarchived as Entity;
6use BO\Zmsentities\Collection\ProcessList as Collection;
7
8/**
9 *
10 */
11class ProcessStatusArchived extends Process
12{
13    public function readArchivedEntity($archiveId, $resolveReferences = 0)
14    {
15        if (!$archiveId) {
16            return null;
17        }
18        $query = new Query\ProcessStatusArchived(Query\Base::SELECT);
19        $query->addEntityMapping()
20            ->addResolvedReferences($resolveReferences)
21            ->addConditionArchiveId($archiveId);
22        $archive = $this->fetchOne($query, new Entity());
23        $archive = $this->readResolvedReferences($archive, $resolveReferences);
24        return $archive;
25    }
26
27    public function readListByScopeId($scopeId, $resolveReferences = 0)
28    {
29        $query = new Query\ProcessStatusArchived(Query\Base::SELECT);
30        $query->addEntityMapping()
31            ->addResolvedReferences($resolveReferences)
32            ->addConditionScopeId($scopeId);
33        return $this->readResolvedList($query, $resolveReferences);
34    }
35
36    public function readListByDate($dateTime, $resolveReferences = 0)
37    {
38        $query = new Query\ProcessStatusArchived(Query\Base::SELECT);
39        $query->addEntityMapping()
40            ->addResolvedReferences($resolveReferences)
41            ->addConditionTime($dateTime);
42        return $this->readResolvedList($query, $resolveReferences);
43    }
44
45    public function readListByScopeAndDate($scopeId, $dateTime, $resolveReferences = 0)
46    {
47        $query = new Query\ProcessStatusArchived(Query\Base::SELECT);
48        $query->addEntityMapping()
49            ->addConditionScopeId($scopeId)
50            ->addResolvedReferences($resolveReferences)
51            ->addConditionTime($dateTime);
52        return $this->readResolvedList($query, $resolveReferences);
53    }
54
55    public function readListByScopesAndDates($scopeIds, $dateTimes, $resolveReferences = 0)
56    {
57        $query = new Query\ProcessStatusArchivedToday(Query\Base::SELECT);
58        $query->addEntityMapping()
59            ->addConditionScopeIds($scopeIds)
60            ->addResolvedReferences($resolveReferences)
61            ->addConditionTimes($dateTimes);
62
63        return $this->readResolvedList($query, $resolveReferences);
64    }
65
66    public function deleteAllToday(): bool
67    {
68        return (bool)$this->perform(Query\ProcessStatusArchivedToday::DELETE_ALL);
69    }
70
71    public function readListForStatistic($dateTime, \BO\Zmsentities\Scope $scope, $limit = 500, $resolveReferences = 0)
72    {
73        $query = new Query\ProcessStatusArchived(Query\Base::SELECT);
74        $query->addEntityMapping()
75            ->addResolvedReferences($resolveReferences)
76            ->addJoinStatisticFailed($dateTime, $scope)
77            ->addLimit($limit);
78        return $this->readResolvedList($query, $resolveReferences);
79    }
80
81    public function readListIsMissed($isMissed = 1, $resolveReferences = 0)
82    {
83        $query = new Query\ProcessStatusArchived(Query\Base::SELECT);
84        $query->addEntityMapping()
85            ->addResolvedReferences($resolveReferences)
86            ->addConditionIsMissed($isMissed);
87        return $this->readResolvedList($query, $resolveReferences);
88    }
89
90    public function readListWithAppointment($withAppointment = 1, $resolveReferences = 0)
91    {
92        $query = new Query\ProcessStatusArchived(Query\Base::SELECT);
93        $query->addEntityMapping()
94            ->addResolvedReferences($resolveReferences)
95            ->addConditionWithAppointment($withAppointment);
96        return $this->readResolvedList($query, $resolveReferences);
97    }
98
99    protected function readResolvedList($query, $resolveReferences)
100    {
101        $processList = new Collection();
102        $resultList = $this->fetchList($query, new Entity());
103        if (count($resultList)) {
104            foreach ($resultList as $entity) {
105                if (0 == $resolveReferences) {
106                    $processList->addEntity($entity);
107                } else {
108                    if ($entity instanceof Entity) {
109                        $entity = $this->readResolvedReferences($entity, $resolveReferences);
110                        $processList->addEntity($entity);
111                    }
112                }
113            }
114        }
115        return $processList;
116    }
117
118    public function writeEntityFinished(
119        \BO\Zmsentities\Process $process,
120        \DateTimeInterface $now,
121        bool $calculateStatistic = false,
122        ?\BO\Zmsentities\Useraccount $useraccount = null
123    ) {
124        if ($process->isDereferenced() || !$process->getScopeId()) {
125            return null;
126        }
127
128        $process = $this->updateEntity(
129            $process,
130            $now,
131            1,
132            null,
133            $useraccount
134        );
135        $archived = null;
136        if ($this->writeBlockedEntity($process)) {
137            $archived = $this->writeNewArchivedProcess($process, $now, 0, $calculateStatistic);
138        }
139        // update xRequest entry and update process id as well as archived id
140        if ($archived) {
141            $this->writeXRequestsArchived($process->id, $archived->archiveId);
142        }
143        return $archived;
144    }
145
146    /**
147     * write an archived process to statistic table
148     *
149     */
150    public function writeArchivedProcessToStatistic(
151        Entity $process,
152        $requestId,
153        $clusterId,
154        $providerId,
155        $departmentId,
156        $organisationId,
157        $ownerId,
158        $dateTime,
159        $processingTime
160    ) {
161        return $this->perform(
162            Query\ProcessStatusArchived::QUERY_INSERT_IN_STATISTIC,
163            [
164                'archiveId' => $process->archiveId,
165                'scopeId' => $process->scope->getId(),
166                'clusterId' => $clusterId,
167                'providerId' => $providerId,
168                'departmentId' => $departmentId,
169                'organisationId' => $organisationId,
170                'ownerId' => $ownerId,
171                'date' => $process->getFirstAppointment()->toDateTime()->format('Y-m-d'),
172                'withAppointment' => ($process->toQueue($dateTime)->withAppointment) ? 1 : 0,
173                'requestId' => $requestId,
174                'processingTime' => $processingTime
175            ]
176        );
177    }
178
179    /**
180     * write a new archived process to DB
181     *
182     */
183    public function writeNewArchivedProcess(
184        \BO\Zmsentities\Process $process,
185        \DateTimeInterface $now,
186        $resolveReferences = 0,
187        bool $calculateStatistic = false
188    ) {
189        $query = new Query\ProcessStatusArchivedToday(Query\Base::INSERT);
190        $query->addValuesNewArchive($process, $now);
191        $this->writeItem($query);
192
193        $query = new Query\ProcessStatusArchived(Query\Base::INSERT);
194        $query->addValuesNewArchive($process, $now);
195        $this->writeItem($query);
196
197        $archiveId = $this->getWriter()->lastInsertId();
198        Log::writeProcessLog(
199            "ARCHIVE (Archive::writeNewArchivedProcess) $archiveId -> $process ",
200            Log::ACTION_ARCHIVED,
201            $process
202        );
203
204        if ($calculateStatistic) {
205            (new ExchangeWaitingscope())->updateWaitingStatistics($process, $now);
206        }
207
208        return $this->readArchivedEntity($archiveId, $resolveReferences);
209    }
210
211    protected function writeXRequestsArchived($processId, $archiveId)
212    {
213        $query = new Query\XRequest(Query\Base::UPDATE);
214        $query->addConditionProcessId($processId);
215        $query->addValues([
216            'BuergerID' => 0,
217            'BuergerarchivID' => $archiveId
218        ]);
219        $this->writeItem($query);
220    }
221
222    /**
223     * Anonymizes names in the buergerarchiv table for entries older than a specified timespan.
224     *
225     * @param \DateTimeInterface $dateTime The date before which records should be anonymized.
226     * @return bool Indicates whether the update operation was successful.
227     */
228    public function anonymizeNames(\DateTimeInterface $dateTime)
229    {
230        $query = new Query\ProcessStatusArchived(Query\Base::UPDATE);
231        $query->addConditionOlderThanDate($dateTime);
232        $query->addValues([
233            'name' => 'ANONYMIZED'
234        ]);
235
236        return $this->writeItem($query);
237    }
238}