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