Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
82.88% covered (warning)
82.88%
92 / 111
71.43% covered (warning)
71.43%
10 / 14
CRAP
0.00% covered (danger)
0.00%
0 / 1
ProcessStatusArchived
82.88% covered (warning)
82.88%
92 / 111
71.43% covered (warning)
71.43%
10 / 14
25.65
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
 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%
13 / 13
100.00% covered (success)
100.00%
1 / 1
3
 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%
12 / 12
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\ProcessStatusArchived(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 readListForStatistic($dateTime, \BO\Zmsentities\Scope $scope, $limit = 500, $resolveReferences = 0)
67    {
68        $query = new Query\ProcessStatusArchived(Query\Base::SELECT);
69        $query->addEntityMapping()
70            ->addResolvedReferences($resolveReferences)
71            ->addJoinStatisticFailed($dateTime, $scope)
72            ->addLimit($limit);
73        return $this->readResolvedList($query, $resolveReferences);
74    }
75
76    public function readListIsMissed($isMissed = 1, $resolveReferences = 0)
77    {
78        $query = new Query\ProcessStatusArchived(Query\Base::SELECT);
79        $query->addEntityMapping()
80            ->addResolvedReferences($resolveReferences)
81            ->addConditionIsMissed($isMissed);
82        return $this->readResolvedList($query, $resolveReferences);
83    }
84
85    public function readListWithAppointment($withAppointment = 1, $resolveReferences = 0)
86    {
87        $query = new Query\ProcessStatusArchived(Query\Base::SELECT);
88        $query->addEntityMapping()
89            ->addResolvedReferences($resolveReferences)
90            ->addConditionWithAppointment($withAppointment);
91        return $this->readResolvedList($query, $resolveReferences);
92    }
93
94    protected function readResolvedList($query, $resolveReferences)
95    {
96        $processList = new Collection();
97        $resultList = $this->fetchList($query, new Entity());
98        if (count($resultList)) {
99            foreach ($resultList as $entity) {
100                if (0 == $resolveReferences) {
101                    $processList->addEntity($entity);
102                } else {
103                    if ($entity instanceof Entity) {
104                        $entity = $this->readResolvedReferences($entity, $resolveReferences);
105                        $processList->addEntity($entity);
106                    }
107                }
108            }
109        }
110        return $processList;
111    }
112
113    public function writeEntityFinished(
114        \BO\Zmsentities\Process $process,
115        \DateTimeInterface $now,
116        bool $calculateStatistic = false,
117        ?\BO\Zmsentities\Useraccount $useraccount = null
118    ) {
119        $process = $this->updateEntity(
120            $process,
121            $now,
122            1,
123            null,
124            $useraccount
125        );
126        $archived = null;
127        if ($this->writeBlockedEntity($process)) {
128            $archived = $this->writeNewArchivedProcess($process, $now, 0, $calculateStatistic);
129        }
130        // update xRequest entry and update process id as well as archived id
131        if ($archived) {
132            $this->writeXRequestsArchived($process->id, $archived->archiveId);
133        }
134        return $archived;
135    }
136
137    /**
138     * write an archived process to statistic table
139     *
140     */
141    public function writeArchivedProcessToStatistic(
142        Entity $process,
143        $requestId,
144        $clusterId,
145        $providerId,
146        $departmentId,
147        $organisationId,
148        $ownerId,
149        $dateTime,
150        $processingTime
151    ) {
152        return $this->perform(
153            Query\ProcessStatusArchived::QUERY_INSERT_IN_STATISTIC,
154            [
155                'archiveId' => $process->archiveId,
156                'scopeId' => $process->scope->getId(),
157                'clusterId' => $clusterId,
158                'providerId' => $providerId,
159                'departmentId' => $departmentId,
160                'organisationId' => $organisationId,
161                'ownerId' => $ownerId,
162                'date' => $process->getFirstAppointment()->toDateTime()->format('Y-m-d'),
163                'withAppointment' => ($process->toQueue($dateTime)->withAppointment) ? 1 : 0,
164                'requestId' => $requestId,
165                'processingTime' => $processingTime
166            ]
167        );
168    }
169
170    /**
171     * write a new archived process to DB
172     *
173     */
174    public function writeNewArchivedProcess(
175        \BO\Zmsentities\Process $process,
176        \DateTimeInterface $now,
177        $resolveReferences = 0,
178        bool $calculateStatistic = false
179    ) {
180        $query = new Query\ProcessStatusArchived(Query\Base::INSERT);
181        $query->addValuesNewArchive($process, $now);
182        $this->writeItem($query);
183        $archiveId = $this->getWriter()->lastInsertId();
184        Log::writeProcessLog(
185            "ARCHIVE (Archive::writeNewArchivedProcess) $archiveId -> $process ",
186            Log::ACTION_ARCHIVED,
187            $process
188        );
189
190        if ($calculateStatistic) {
191            (new ExchangeWaitingscope())->updateWaitingStatistics($process, $now);
192        }
193
194        return $this->readArchivedEntity($archiveId, $resolveReferences);
195    }
196
197    protected function writeXRequestsArchived($processId, $archiveId)
198    {
199        $query = new Query\XRequest(Query\Base::UPDATE);
200        $query->addConditionProcessId($processId);
201        $query->addValues([
202            'BuergerID' => 0,
203            'BuergerarchivID' => $archiveId
204        ]);
205        $this->writeItem($query);
206    }
207
208    /**
209     * Anonymizes names in the buergerarchiv table for entries older than a specified timespan.
210     *
211     * @param \DateTimeInterface $dateTime The date before which records should be anonymized.
212     * @return bool Indicates whether the update operation was successful.
213     */
214    public function anonymizeNames(\DateTimeInterface $dateTime)
215    {
216        $query = new Query\ProcessStatusArchived(Query\Base::UPDATE);
217        $query->addConditionOlderThanDate($dateTime);
218        $query->addValues([
219            'name' => 'ANONYMIZED'
220        ]);
221
222        return $this->writeItem($query);
223    }
224}