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