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;
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 updateMail($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    }
111
112    protected static function readProcessListByScopeAndDate($validator, $scopeList, $slotType, $slotsRequired)
113    {
114        $selectedDate = $validator->getParameter('selecteddate')->isString()->getValue();
115        $calendar = new Calendar($selectedDate);
116        return $calendar->readAvailableSlotsFromDayAndScopeList($scopeList, $slotType, $slotsRequired);
117    }
118
119    protected static function getFreeProcessListWithSelectedProcess(
120        $validator,
121        $scopeList,
122        $freeProcessList,
123        $selectedProcess
124    ) {
125        $selectedDate = $validator->getParameter('selecteddate')->isString()->getValue();
126        if (
127            $selectedProcess &&
128            $selectedProcess->queue->withAppointment &&
129            $selectedDate == $selectedProcess->getFirstAppointment()->toDateTime()->format('Y-m-d')
130        ) {
131            if ($freeProcessList) {
132                $freeProcessList->setTempAppointmentToProcess(
133                    $selectedProcess->getFirstAppointment()->toDateTime(),
134                    $scopeList->getFirst()->getId()
135                );
136            } elseif (! $freeProcessList) {
137                $freeProcessList = (new \BO\Zmsentities\Collection\ProcessList())->addEntity($selectedProcess);
138            }
139        }
140
141        return ($freeProcessList) ? $freeProcessList : null;
142    }
143
144    protected static function setSlotType($validator)
145    {
146        $slotType = $validator->getParameter('slotType')->isString()->getValue();
147        $slotType = ($slotType) ? $slotType : 'intern';
148        return $slotType;
149    }
150
151    protected static function setSlotsRequired($validator, \BO\Zmsentities\Scope $scope, $process)
152    {
153        $slotsRequired = 0;
154        if ($scope && $scope->getPreference('appointment', 'multipleSlotsEnabled')) {
155            $slotsRequired = $validator->getParameter('slotsRequired')->isNumber()->getValue();
156            $slotsRequired = ($slotsRequired) ? $slotsRequired : 0;
157        }
158        $slotsRequired = (0 == $slotsRequired && $process)
159            ? $process->getFirstAppointment()->getSlotCount()
160            : $slotsRequired;
161        return $slotsRequired;
162    }
163
164    protected static function writeMail($mailConfirmation, \BO\Zmsentities\Process $process)
165    {
166        if (
167            $mailConfirmation &&
168            $process->getFirstClient()->hasEmail() &&
169            $process->scope->hasEmailFrom()
170        ) {
171            \App::$http->readPostResult(
172                '/process/' . $process->id . '/' . $process->authKey . '/confirmation/mail/',
173                $process
174            );
175        }
176    }
177}