Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
94.34% covered (success)
94.34%
100 / 106
60.00% covered (warning)
60.00%
3 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
ProcessReserve
94.34% covered (success)
94.34%
100 / 106
60.00% covered (warning)
60.00%
3 / 5
14.04
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
93.44% covered (success)
93.44%
57 / 61
0.00% covered (danger)
0.00%
0 / 1
4.00
 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        $processValidator->getCollection()->addValid(
136            $validator->getParameter('sendConfirmation')->isNumber(),
137            $validator->getParameter('sendReminder')->isNumber()
138        );
139
140        $form = $processValidator->getCollection()->getStatus(null, true);
141        $form['failed'] = $processValidator->getCollection()->hasFailed();
142        return $form;
143    }
144
145    public static function writeReservedProcess($input, $process)
146    {
147        $process = \App::$http
148            ->readPostResult('/process/status/reserved/', $process, [
149                'slotType' => 'intern',
150                'clientkey' => \App::CLIENTKEY,
151                'slotsRequired' => (isset($input['slotCount']) && 1 < $input['slotCount']) ? $input['slotCount'] : 0
152            ])
153            ->getEntity();
154        return $process;
155    }
156
157    public static function writeConfirmedProcess($input, $process)
158    {
159        $confirmedProcess = \App::$http->readPostResult('/process/status/confirmed/', $process)->getEntity();
160        if ('confirmed' == $confirmedProcess->getStatus()) {
161            $process = $confirmedProcess;
162            Helper\AppointmentFormHelper::updateMailAndNotification($input, $process);
163        }
164        return $process;
165    }
166}