Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
108 / 108
100.00% covered (success)
100.00%
11 / 11
CRAP
100.00% covered (success)
100.00%
1 / 1
AppointmentFormHelper
100.00% covered (success)
100.00%
108 / 108
100.00% covered (success)
100.00%
11 / 11
50
100.00% covered (success)
100.00%
1 / 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
 updateMailAndNotification
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
5
 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
 writeNotification
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
 writeMail
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
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\Collection\ScopeList;
13
14/**
15 * @SuppressWarnings(Complexity)
16 */
17class AppointmentFormHelper
18{
19    public static function readFreeProcessList($request, $workstation, $resolveReferences = 1)
20    {
21        $validator = $request->getAttribute('validator');
22        $selectedProcessId = $validator->getParameter('selectedprocess')->isNumber()->getValue();
23        $selectedProcess = ($selectedProcessId)
24            ? \App::$http->readGetResult('/process/' . $selectedProcessId . '/', [
25                'gql' => GraphDefaults::getProcess()
26            ])->getEntity()
27            : null;
28
29        $scope = static::readSelectedScope($request, $workstation, $selectedProcess, $resolveReferences);
30        $scopeList = ($scope) ? (new ScopeList())->addEntity($scope) : (new ClusterHelper($workstation))->getScopeList();
31
32        $slotType = static::setSlotType($validator);
33        $slotsRequired = static::setSlotsRequired($validator, $scope, $selectedProcess);
34        $freeProcessList = static::readProcessListByScopeAndDate(
35            $validator,
36            $scopeList,
37            $slotType,
38            $slotsRequired
39        );
40        $freeProcessList = ($freeProcessList) ? $freeProcessList->withoutExpiredAppointmentDate(\App::$now) : null;
41        $freeProcessList = static::getFreeProcessListWithSelectedProcess(
42            $validator,
43            $scopeList,
44            $freeProcessList,
45            $selectedProcess
46        );
47        return ($freeProcessList) ? $freeProcessList->toProcessListByTime()->sortByTimeKey() : null;
48    }
49
50    public static function readRequestList($request, $workstation, $selectedScope = null)
51    {
52        $scope = ($selectedScope) ? $selectedScope : static::readSelectedScope($request, $workstation);
53        $requestList = null;
54        if ($scope) {
55            $requestList = \App::$http
56                ->readGetResult('/scope/' . $scope->getId() . '/request/', [
57                    'gql' => GraphDefaults::getRequest()
58                ])
59                ->getCollection();
60        }
61        return ($requestList) ? $requestList->sortByName() : new \BO\Zmsentities\Collection\RequestList();
62    }
63
64    public static function readSelectedScope($request, $workstation, $selectedProcess = null, $resolveReferences = 1)
65    {
66        $validator = $request->getAttribute('validator');
67        $input = $request->getParsedBody();
68        $selectedScopeId = (isset($input['scope']))
69            ? $input['scope']
70            : $validator->getParameter('selectedscope')->isNumber()->getValue();
71
72        if ($workstation->queue['clusterEnabled'] && ! $selectedScopeId) {
73            $selectedScope = null;
74        }
75        if (! $workstation->queue['clusterEnabled'] && ! $selectedScopeId) {
76            $selectedScope = new \BO\Zmsentities\Scope($workstation->scope);
77        }
78        if ($selectedScopeId) {
79            $selectedScope = \App::$http
80              ->readGetResult('/scope/' . $selectedScopeId . '/', [
81                  'resolveReferences' => $resolveReferences,
82                  'gql' => GraphDefaults::getScope()
83                ])
84              ->getEntity();
85        }
86        if (! $workstation->queue['clusterEnabled'] && $selectedProcess && $selectedProcess->hasId()) {
87            $selectedScope = $selectedProcess->getCurrentScope();
88        }
89        return $selectedScope;
90    }
91
92    public static function readSelectedProcess($request)
93    {
94        $validator = $request->getAttribute('validator');
95        $selectedProcessId = $validator->getParameter('selectedprocess')->isNumber()->getValue();
96        return ($selectedProcessId) ?
97            \App::$http->readGetResult('/process/' . $selectedProcessId . '/', [
98                'gql' => GraphDefaults::getProcess()
99            ])->getEntity() :
100            null;
101    }
102
103    public static function updateMailAndNotification($formData, \BO\Zmsentities\Process $process)
104    {
105        if (isset($formData['sendMailConfirmation'])) {
106            $mailConfirmation = $formData['sendMailConfirmation'];
107            $mailConfirmation = (isset($mailConfirmation['value'])) ? $mailConfirmation['value'] : $mailConfirmation;
108            self::writeMail($mailConfirmation, $process);
109        }
110        if (isset($formData['sendConfirmation'])) {
111            $smsConfirmation = $formData['sendConfirmation'];
112            $smsConfirmation = (isset($smsConfirmation['value'])) ? $smsConfirmation['value'] : $smsConfirmation;
113            self::writeNotification($smsConfirmation, $process);
114        }
115    }
116
117    protected static function readProcessListByScopeAndDate($validator, $scopeList, $slotType, $slotsRequired)
118    {
119        $selectedDate = $validator->getParameter('selecteddate')->isString()->getValue();
120        $calendar = new Calendar($selectedDate);
121        return $calendar->readAvailableSlotsFromDayAndScopeList($scopeList, $slotType, $slotsRequired);
122    }
123
124    protected static function getFreeProcessListWithSelectedProcess(
125        $validator,
126        $scopeList,
127        $freeProcessList,
128        $selectedProcess
129    ) {
130        $selectedDate = $validator->getParameter('selecteddate')->isString()->getValue();
131        if (
132            $selectedProcess &&
133            $selectedProcess->queue->withAppointment &&
134            $selectedDate == $selectedProcess->getFirstAppointment()->toDateTime()->format('Y-m-d')
135        ) {
136            if ($freeProcessList) {
137                $freeProcessList->setTempAppointmentToProcess(
138                    $selectedProcess->getFirstAppointment()->toDateTime(),
139                    $scopeList->getFirst()->getId()
140                );
141            } elseif (! $freeProcessList) {
142                $freeProcessList = (new \BO\Zmsentities\Collection\ProcessList())->addEntity($selectedProcess);
143            }
144        }
145
146        return ($freeProcessList) ? $freeProcessList : null;
147    }
148
149    protected static function setSlotType($validator)
150    {
151        $slotType = $validator->getParameter('slotType')->isString()->getValue();
152        $slotType = ($slotType) ? $slotType : 'intern';
153        return $slotType;
154    }
155
156    protected static function setSlotsRequired($validator, \BO\Zmsentities\Scope $scope, $process)
157    {
158        $slotsRequired = 0;
159        if ($scope && $scope->getPreference('appointment', 'multipleSlotsEnabled')) {
160            $slotsRequired = $validator->getParameter('slotsRequired')->isNumber()->getValue();
161            $slotsRequired = ($slotsRequired) ? $slotsRequired : 0;
162        }
163        $slotsRequired = (0 == $slotsRequired && $process)
164            ? $process->getFirstAppointment()->getSlotCount()
165            : $slotsRequired;
166        return $slotsRequired;
167    }
168
169    protected static function writeNotification($smsConfirmation, \BO\Zmsentities\Process $process)
170    {
171        if (
172            $smsConfirmation &&
173            $process->scope->hasNotificationEnabled() &&
174            $process->getFirstClient()->hasTelephone()
175        ) {
176            \App::$http->readPostResult(
177                '/process/' . $process->id . '/' . $process->authKey . '/confirmation/notification/',
178                $process
179            );
180        }
181    }
182
183    protected static function writeMail($mailConfirmation, \BO\Zmsentities\Process $process)
184    {
185        if (
186            $mailConfirmation &&
187            $process->getFirstClient()->hasEmail() &&
188            $process->scope->hasEmailFrom()
189        ) {
190            \App::$http->readPostResult(
191                '/process/' . $process->id . '/' . $process->authKey . '/confirmation/mail/',
192                $process
193            );
194        }
195    }
196}