Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
98.36% covered (success)
98.36%
120 / 122
84.62% covered (warning)
84.62%
11 / 13
CRAP
0.00% covered (danger)
0.00%
0 / 1
AppointmentFormHelper
98.36% covered (success)
98.36%
120 / 122
84.62% covered (warning)
84.62%
11 / 13
60
0.00% covered (danger)
0.00%
0 / 1
 readFreeProcessList
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
1 / 1
5
 readRequestList
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
4
 readSelectedScope
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
1 / 1
10
 readSelectedProcess
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 updateMail
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 readProcessListByScopeAndDate
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getFreeProcessListWithSelectedProcess
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
7
 setSlotType
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 setSlotsRequired
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
6
 writeMail
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
 resolveSlotTimeInMinutes
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
10
 readAvailabilitySlotTimeInMinutes
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
 readProviderSlotTimeInMinutes
83.33% covered (warning)
83.33%
5 / 6
0.00% covered (danger)
0.00%
0 / 1
4.07
1<?php
2
3/**
4 *
5 * @package Zmsadmin
6 * @copyright BerlinOnline Stadtportal GmbH & Co. KG
7 *
8 */
9
10namespace BO\Zmsadmin\Helper;
11
12use BO\Zmsentities\Availability;
13use BO\Zmsentities\Collection\ScopeList;
14use BO\Zmsentities\Collection\RequestList;
15use BO\Zmsentities\Collection\ProcessList;
16use BO\Zmsentities\Provider;
17use BO\Zmsentities\Scope;
18use BO\Zmsentities\Process;
19use BO\Zmsentities\Workstation;
20
21/**
22 * @SuppressWarnings(Complexity)
23 */
24class AppointmentFormHelper
25{
26    public static function readFreeProcessList(\Psr\Http\Message\RequestInterface $request, $workstation, int $resolveReferences = 1)
27    {
28        $validator = $request->getAttribute('validator');
29        $selectedProcessId = $validator->getParameter('selectedprocess')->isNumber()->getValue();
30        $selectedProcess = ($selectedProcessId)
31            ? \App::$http->readGetResult('/process/' . $selectedProcessId . '/', [
32                'gql' => GraphDefaults::getProcess()
33            ])->getEntity()
34            : null;
35
36        $scope = static::readSelectedScope($request, $workstation, $selectedProcess, $resolveReferences);
37        $scopeList = ($scope) ? (new ScopeList())->addEntity($scope) : (new ClusterHelper($workstation))->getScopeList();
38
39        $slotType = static::setSlotType($validator);
40        $slotsRequired = static::setSlotsRequired($validator, $scope, $selectedProcess);
41        $freeProcessList = static::readProcessListByScopeAndDate(
42            $validator,
43            $scopeList,
44            $slotType,
45            $slotsRequired
46        );
47        $freeProcessList = ($freeProcessList) ? $freeProcessList->withoutExpiredAppointmentDate(\App::$now) : null;
48        $freeProcessList = static::getFreeProcessListWithSelectedProcess(
49            $validator,
50            $scopeList,
51            $freeProcessList,
52            $selectedProcess
53        );
54        return ($freeProcessList) ? $freeProcessList->toProcessListByTime()->sortByTimeKey() : null;
55    }
56
57    public static function readRequestList(\Psr\Http\Message\RequestInterface $request, $workstation, $selectedScope = null)
58    {
59        $scope = ($selectedScope) ? $selectedScope : static::readSelectedScope($request, $workstation);
60        $requestList = null;
61        if ($scope) {
62            $requestList = \App::$http
63                ->readGetResult('/scope/' . $scope->getId() . '/request/', [
64                    'gql' => GraphDefaults::getRequest()
65                ])
66                ->getCollection();
67        }
68        return ($requestList) ? $requestList->sortByName() : new RequestList();
69    }
70
71    public static function readSelectedScope(\Psr\Http\Message\RequestInterface $request, $workstation, $selectedProcess = null, int $resolveReferences = 1)
72    {
73        $validator = $request->getAttribute('validator');
74        $input = $request->getParsedBody();
75        $selectedScopeId = (isset($input['scope']))
76            ? $input['scope']
77            : $validator->getParameter('selectedscope')->isNumber()->getValue();
78
79        if ($workstation->queue['clusterEnabled'] && ! $selectedScopeId) {
80            $selectedScope = null;
81        }
82        if (! $workstation->queue['clusterEnabled'] && ! $selectedScopeId) {
83            $selectedScope = new Scope($workstation->scope);
84        }
85        if ($selectedScopeId) {
86            $selectedScope = \App::$http
87              ->readGetResult('/scope/' . $selectedScopeId . '/', [
88                  'resolveReferences' => $resolveReferences,
89                  'gql' => GraphDefaults::getScope()
90                ])
91              ->getEntity();
92        }
93        if (! $workstation->queue['clusterEnabled'] && $selectedProcess && $selectedProcess->hasId()) {
94            $selectedScope = $selectedProcess->getCurrentScope();
95        }
96        return $selectedScope;
97    }
98
99    public static function readSelectedProcess(\Psr\Http\Message\RequestInterface $request)
100    {
101        $validator = $request->getAttribute('validator');
102        $selectedProcessId = $validator->getParameter('selectedprocess')->isNumber()->getValue();
103        return ($selectedProcessId) ?
104            \App::$http->readGetResult('/process/' . $selectedProcessId . '/', [
105                'gql' => GraphDefaults::getProcess()
106            ])->getEntity() :
107            null;
108    }
109
110    public static function updateMail($formData, Process $process): void
111    {
112        if (isset($formData['sendMailConfirmation'])) {
113            $mailConfirmation = $formData['sendMailConfirmation'];
114            $mailConfirmation = (isset($mailConfirmation['value'])) ? $mailConfirmation['value'] : $mailConfirmation;
115            self::writeMail($mailConfirmation, $process);
116        }
117    }
118
119    protected static function readProcessListByScopeAndDate($validator, $scopeList, $slotType, int $slotsRequired)
120    {
121        $selectedDate = $validator->getParameter('selecteddate')->isString()->getValue();
122        $calendar = new Calendar($selectedDate);
123        return $calendar->readAvailableSlotsFromDayAndScopeList($scopeList, $slotType, $slotsRequired);
124    }
125
126    protected static function getFreeProcessListWithSelectedProcess(
127        $validator,
128        $scopeList,
129        $freeProcessList,
130        $selectedProcess
131    ) {
132        $selectedDate = $validator->getParameter('selecteddate')->isString()->getValue();
133        if (
134            $selectedProcess &&
135            $selectedProcess->queue->withAppointment &&
136            $selectedDate == $selectedProcess->getFirstAppointment()->toDateTime()->format('Y-m-d')
137        ) {
138            if ($freeProcessList) {
139                $freeProcessList->setTempAppointmentToProcess(
140                    $selectedProcess->getFirstAppointment()->toDateTime(),
141                    $scopeList->getFirst()->getId()
142                );
143            } elseif (! $freeProcessList) {
144                $freeProcessList = (new ProcessList())->addEntity($selectedProcess);
145            }
146        }
147
148        return ($freeProcessList) ? $freeProcessList : null;
149    }
150
151    protected static function setSlotType($validator)
152    {
153        $slotType = $validator->getParameter('slotType')->isString()->getValue();
154        $slotType = ($slotType) ? $slotType : 'intern';
155        return $slotType;
156    }
157
158    protected static function setSlotsRequired($validator, ?Scope $scope, $process)
159    {
160        $slotsRequired = 0;
161        if ($scope && $scope->getPreference('appointment', 'multipleSlotsEnabled')) {
162            $slotsRequired = $validator->getParameter('slotsRequired')->isNumber()->getValue();
163            $slotsRequired = ($slotsRequired) ? $slotsRequired : 0;
164        }
165        $slotsRequired = (0 == $slotsRequired && $process)
166            ? $process->getFirstAppointment()->getSlotCount()
167            : $slotsRequired;
168        return $slotsRequired;
169    }
170
171    protected static function writeMail($mailConfirmation, Process $process): void
172    {
173        if (
174            $mailConfirmation &&
175            $process->getFirstClient()->hasEmail() &&
176            $process->scope->hasEmailFrom()
177        ) {
178            \App::$http->readPostResult(
179                '/process/' . $process->id . '/' . $process->authKey . '/confirmation/mail/',
180                $process
181            );
182        }
183    }
184
185    public static function resolveSlotTimeInMinutes(
186        ?Process $selectedProcess,
187        ?Scope $selectedScope,
188        ?Workstation $workstation = null
189    ): int|string|null {
190        $availability = null;
191        if ($selectedProcess && $selectedProcess->hasId()) {
192            $availability = $selectedProcess->getAppointments()->getFirst()->getAvailability();
193        }
194
195        $slotTimeInMinutes = null;
196        if ($availability && $availability->hasId()) {
197            $slotTimeInMinutes = static::readAvailabilitySlotTimeInMinutes($availability);
198        }
199
200        if (! $slotTimeInMinutes) {
201            $scopes = [$selectedScope];
202            if ($selectedProcess && $selectedProcess->hasId()) {
203                $scopes[] = $selectedProcess->getCurrentScope();
204            }
205            $scopes[] = $workstation?->getScope();
206
207            foreach ($scopes as $scope) {
208                $slotTimeInMinutes = static::readProviderSlotTimeInMinutes($scope);
209                if ($slotTimeInMinutes) {
210                    break;
211                }
212            }
213        }
214
215        return $slotTimeInMinutes;
216    }
217
218    protected static function readAvailabilitySlotTimeInMinutes(?Availability $availability): int|string|null
219    {
220        if (! $availability) {
221            return null;
222        }
223        return $availability->getSlotTimeInMinutes();
224    }
225
226    protected static function readProviderSlotTimeInMinutes(?Scope $scope): int|string|null
227    {
228        if ($scope === null || ! isset($scope['provider'])) {
229            return null;
230        }
231        $provider = $scope['provider'];
232        if (! $provider instanceof Provider) {
233            $provider = new Provider($provider);
234        }
235        return $provider->getSlotTimeInMinutes();
236    }
237}