Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
91.07% covered (success)
91.07%
102 / 112
60.00% covered (warning)
60.00%
3 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
ProcessReserve
91.07% covered (success)
91.07%
102 / 112
60.00% covered (warning)
60.00%
3 / 5
16.18
0.00% covered (danger)
0.00%
0 / 1
 readResponse
92.86% covered (success)
92.86%
26 / 28
0.00% covered (danger)
0.00%
0 / 1
4.01
 getProcess
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 getValidatedForm
88.06% covered (warning)
88.06%
59 / 67
0.00% covered (danger)
0.00%
0 / 1
6.06
 writeReservedProcess
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
 writeConfirmedProcess
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3/**
4 *
5 * @package Zmsadmin
6 * @copyright BerlinOnline Stadtportal GmbH & Co. KG
7 *
8 */
9
10namespace BO\Zmsadmin;
11
12use BO\Mellon\Condition;
13use BO\Slim\Render;
14use BO\Zmsentities\Validator\ProcessValidator;
15use BO\Zmsentities\Process as Entity;
16
17/**
18 * Reserve a process
19 */
20class ProcessReserve extends BaseController
21{
22    /**
23     * @SuppressWarnings(Param)
24     * @return String
25     */
26    public function readResponse(
27        \Psr\Http\Message\RequestInterface $request,
28        \Psr\Http\Message\ResponseInterface $response,
29        array $args
30    ) {
31        $workstation = \App::$http->readGetResult('/workstation/', ['resolveReferences' => 2])->getEntity();
32        $input = $request->getParams();
33        $scope = Helper\AppointmentFormHelper::readSelectedScope($request, $workstation);
34        $process = $this->getProcess($input, $scope);
35        $validatedForm = static::getValidatedForm($request->getAttribute('validator'), $process);
36        if ($validatedForm['failed']) {
37            return \BO\Slim\Render::withJson(
38                $response,
39                $validatedForm
40            );
41        }
42
43        $process = static::writeReservedProcess($input, $process);
44        $process = static::writeConfirmedProcess($input, $process);
45        $appointment = $process->getFirstAppointment();
46        $conflictList = ($process->isWithAppointment()) ?
47            ProcessSave::getConflictList($scope->getId(), $appointment) :
48            null;
49        $queryParams = ('confirmed' == $process->getStatus()) ?
50            [
51                'selectedprocess' => $process,
52                'success' => 'process_reserved',
53                'conflictlist' => $conflictList
54            ] :
55            [];
56
57        return \BO\Slim\Render::withHtml(
58            $response,
59            'element/helper/messageHandler.twig',
60            $queryParams
61        );
62    }
63
64    protected function getProcess($input, $scope)
65    {
66        $process = new \BO\Zmsentities\Process();
67        $selectedTime = str_replace('-', ':', $input['selectedtime']);
68        $dateTime = \DateTime::createFromFormat('Y-m-d H:i', $input['selecteddate'] . ' ' . $selectedTime);
69
70        return $process->withUpdatedData($input, $dateTime, $scope);
71    }
72
73    public static function getValidatedForm($validator, $process)
74    {
75        $processValidator = new ProcessValidator($process);
76        $delegatedProcess = $processValidator->getDelegatedProcess();
77        $processValidator
78            ->validateName(
79                $validator->getParameter('familyName'),
80                $delegatedProcess->setter('clients', 0, 'familyName')
81            )
82            ->validateRequests(
83                $validator->getParameter('requests'),
84                function () use ($process, $delegatedProcess) {
85                    $arrayKeys = array_keys(json_decode(json_encode($process->requests), true));
86                    foreach ($arrayKeys as $key) {
87                        $delegatedProcess->setter('requests', $key, 'id');
88                        $delegatedProcess->setter('requests', $key, 'source');
89                    }
90                }
91            )
92            ->validateMail(
93                $validator->getParameter('email'),
94                $delegatedProcess->setter('clients', 0, 'email'),
95                new Condition(
96                    $validator->getParameter('sendMailConfirmation')->isNumber()->isNotEqualTo(1),
97                    $validator->getParameter('surveyAccepted')->isString()->isDevoidOf([1])
98                )
99            )
100            ->validateTelephone(
101                $validator->getParameter('telephone'),
102                $delegatedProcess->setter('clients', 0, 'telephone'),
103                new Condition(
104                    $validator->getParameter('sendConfirmation')->isNumber()->isNotEqualTo(1),
105                    $validator->getParameter('sendReminder')->isNumber()->isNotEqualTo(1)
106                )
107            )
108            ->validateSurvey(
109                $validator->getParameter('surveyAccepted'),
110                $delegatedProcess->setter('clients', 0, 'surveyAccepted')
111            )
112            ->validateText(
113                $validator->getParameter('amendment'),
114                $delegatedProcess->setter('amendment')
115            )
116            ->validateReminderTimestamp(
117                $validator->getParameter('headsUpTime'),
118                $delegatedProcess->setter('reminderTimestamp'),
119                new Condition(
120                    $validator->getParameter('sendReminder')->isNumber()->isNotEqualTo(1)
121                )
122            )
123        ;
124
125        if (
126            isset($process->scope->preferences['client']['customTextfieldRequired'])
127            && $process->scope->preferences['client']['customTextfieldRequired']
128        ) {
129            $processValidator->validateCustomField(
130                $validator->getParameter('customTextfield'),
131                $delegatedProcess->setter('customTextfield')
132            );
133        }
134
135        if (
136            isset($process->scope->preferences['client']['customTextfield2Required'])
137            && $process->scope->preferences['client']['customTextfield2Required']
138        ) {
139            $processValidator->validateCustomField(
140                $validator->getParameter('customTextfield2'),
141                $delegatedProcess->setter('customTextfield2')
142            );
143        }
144
145        $processValidator->getCollection()->addValid(
146            $validator->getParameter('sendConfirmation')->isNumber(),
147            $validator->getParameter('sendReminder')->isNumber()
148        );
149
150        $form = $processValidator->getCollection()->getStatus(null, true);
151        $form['failed'] = $processValidator->getCollection()->hasFailed();
152        return $form;
153    }
154
155    public static function writeReservedProcess($input, $process)
156    {
157        $process = \App::$http
158            ->readPostResult('/process/status/reserved/', $process, [
159                'slotType' => 'intern',
160                'clientkey' => \App::CLIENTKEY,
161                'slotsRequired' => (isset($input['slotCount']) && 1 < $input['slotCount']) ? $input['slotCount'] : 0
162            ])
163            ->getEntity();
164        return $process;
165    }
166
167    public static function writeConfirmedProcess($input, $process)
168    {
169        $confirmedProcess = \App::$http->readPostResult('/process/status/confirmed/', $process)->getEntity();
170        if ('confirmed' == $confirmedProcess->getStatus()) {
171            $process = $confirmedProcess;
172            Helper\AppointmentFormHelper::updateMailAndNotification($input, $process);
173        }
174        return $process;
175    }
176}