Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
79.41% covered (warning)
79.41%
108 / 136
71.43% covered (warning)
71.43%
5 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
ProcessQueue
79.41% covered (warning)
79.41%
108 / 136
71.43% covered (warning)
71.43%
5 / 7
24.85
0.00% covered (danger)
0.00%
0 / 1
 readResponse
100.00% covered (success)
100.00%
30 / 30
100.00% covered (success)
100.00%
1 / 1
5
 getValidatedForm
81.13% covered (warning)
81.13%
43 / 53
0.00% covered (danger)
0.00%
0 / 1
3.06
 readSelectedProcessWithWaitingnumber
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 getProcess
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 writeQueuedProcess
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 isOpened
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
4
 printProcessResponse
33.33% covered (danger)
33.33%
9 / 27
0.00% covered (danger)
0.00%
0 / 1
8.74
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\Zmsadmin\Helper\MailTemplateArrayProvider;
15use BO\Zmsentities\Client;
16use BO\Zmsentities\Collection\ProcessList;
17use BO\Zmsentities\Config;
18use BO\Zmsentities\Helper\Messaging;
19use BO\Zmsentities\Validator\ProcessValidator;
20use BO\Zmsentities\Process as Entity;
21use BO\Zmsadmin\Helper\AppointmentFormHelper;
22use Psr\Http\Message\RequestInterface;
23use Psr\Http\Message\ResponseInterface;
24
25/**
26 * Queue a process from appointment formular without appointment
27 */
28class ProcessQueue extends BaseController
29{
30    /**
31     * @SuppressWarnings(Param)
32     */
33    public function readResponse(
34        RequestInterface $request,
35        ResponseInterface $response,
36        array $args
37    ) {
38        $workstation = \App::$http->readGetResult('/workstation/', ['resolveReferences' => 3])->getEntity();
39
40        $validator = $request->getAttribute('validator');
41        $selectedProcessId = $validator->getParameter('selectedprocess')->isNumber()->getValue();
42
43        if ($selectedProcessId) {
44            $process = $this->readSelectedProcessWithWaitingnumber($selectedProcessId);
45
46            if ($process && $validator->getParameter('print')->isNumber()->getValue()) {
47                return $this->printProcessResponse(
48                    $response,
49                    $process,
50                    $validator->getParameter('printType')->isString()->getValue(),
51                    $workstation->scope['provider']['id']
52                );
53            }
54        }
55
56        $input = $request->getParams();
57        $scope = AppointmentFormHelper::readSelectedScope($request, $workstation);
58        $process = $this->getProcess($input, $scope);
59        $validatedForm = static::getValidatedForm($validator, $process);
60        if ($validatedForm['failed']) {
61            return \BO\Slim\Render::withJson(
62                $response,
63                $validatedForm
64            );
65        }
66
67        $process = $this->writeQueuedProcess($input, $process);
68        return \BO\Slim\Render::withHtml(
69            $response,
70            'element/helper/messageHandler.twig',
71            array(
72                'selectedprocess' => $process,
73                'success' => 'process_queued'
74            )
75        );
76    }
77
78    public static function getValidatedForm($validator, $process)
79    {
80        $processValidator = new ProcessValidator($process);
81        $delegatedProcess = $processValidator->getDelegatedProcess();
82        $processValidator
83            ->validateName(
84                $validator->getParameter('familyName'),
85                $delegatedProcess->setter('clients', 0, 'familyName')
86            )
87            ->validateMail(
88                $validator->getParameter('email'),
89                $delegatedProcess->setter('clients', 0, 'email')
90            )
91            ->validateTelephone(
92                $validator->getParameter('telephone'),
93                $delegatedProcess->setter('clients', 0, 'telephone')
94            )
95            ->validateSurvey(
96                $validator->getParameter('surveyAccepted'),
97                $delegatedProcess->setter('clients', 0, 'surveyAccepted')
98            )
99            ->validateText(
100                $validator->getParameter('amendment'),
101                $delegatedProcess->setter('amendment')
102            )
103        ;
104
105        $scope = $process->getCurrentScope();
106        if ((int) $scope->getCustomTextfieldActivated()) {
107            $processValidator->validateCustomTextfield(
108                $validator->getParameter('customTextfield'),
109                $delegatedProcess->setter('customTextfield'),
110                (bool) (int) $scope->getCustomTextfieldRequired()
111            );
112        }
113        if ((int) $scope->getCustomTextfield2Activated()) {
114            $processValidator->validateCustomTextfield(
115                $validator->getParameter('customTextfield2'),
116                $delegatedProcess->setter('customTextfield2'),
117                (bool) (int) $scope->getCustomTextfield2Required()
118            );
119        }
120
121        $processValidator
122            ->validateReminderTimestamp(
123                $validator->getParameter('headsUpTime'),
124                $delegatedProcess->setter('reminderTimestamp'),
125                new Condition(
126                    $validator->getParameter('sendReminder')->isNumber()->isNotEqualTo(1)
127                )
128            )
129        ;
130        $processValidator->getCollection()->addValid(
131            $validator->getParameter('sendConfirmation')->isNumber(),
132            $validator->getParameter('sendReminder')->isNumber()
133        );
134
135        $form = $processValidator->getCollection()->getStatus(null, true);
136        $form['failed'] = $processValidator->getCollection()->hasFailed();
137        return $form;
138    }
139
140    protected function readSelectedProcessWithWaitingnumber($selectedProcessId)
141    {
142        $result = null;
143        if ($selectedProcessId) {
144            $result = \App::$http->readGetResult('/process/' . $selectedProcessId . '/')->getEntity();
145        }
146        return $result;
147    }
148
149    protected function getProcess($input, $scope)
150    {
151        $process = new \BO\Zmsentities\Process();
152        $notice = (! $this->isOpened($scope)) ? 'Außerhalb der Öffnungszeiten gebucht! ' : '';
153        return $process->withUpdatedData($input, \App::$now, $scope, $notice);
154    }
155
156    protected function writeQueuedProcess($input, $process)
157    {
158        $process = \App::$http->readPostResult('/workstation/process/waitingnumber/', $process)->getEntity();
159        AppointmentFormHelper::updateMail($input, $process);
160        return $process;
161    }
162
163    protected function isOpened($scope)
164    {
165        if ($scope->getResolveLevel() < 1) {
166            $scope =  \App::$http->readGetResult('/scope/' . $scope->getId() . '/', [
167                'resolveReferences' => 1,
168                'gql' => Helper\GraphDefaults::getScope()
169            ])
170                ->getEntity();
171        }
172        $isOpened = false;
173        try {
174            $isOpened = \App::$http
175                ->readGetResult('/scope/' . $scope->getId() . '/availability/', ['resolveReferences' => 0])
176                ->getCollection()
177                ->withScope($scope)
178                ->isOpened(\App::$now);
179        } catch (\BO\Zmsclient\Exception $exception) {
180            if ($exception->template == 'BO\\Zmsapi\\Exception\\Availability\\AvailabilityNotFound') {
181                $isOpened = false;
182            }
183        }
184        return $isOpened;
185    }
186
187    private function printProcessResponse(
188        ResponseInterface $response,
189        Entity $process,
190        ?string $printType = null,
191        ?int $providerId = null
192    ): ResponseInterface {
193        if ($printType === 'mail') {
194            $mergedMailTemplates = \App::$http->readGetResult('/merged-mailtemplates/' . $providerId . '/')
195                ->getCollection();
196
197            $templates = [];
198
199            foreach ($mergedMailTemplates as $template) {
200                $templates[$template->name] = $template->value;
201            };
202
203            $templateProvider = new MailTemplateArrayProvider();
204            $templateProvider->setTemplates($templates);
205
206            $config = \App::$http->readGetResult('/config/')->getEntity();
207
208            $mail = (new \BO\Zmsentities\Mail())
209                ->setTemplateProvider($templateProvider)
210                ->toResolvedEntity($process, $config, 'appointment');
211
212            return \BO\Slim\Render::withHtml(
213                $response,
214                'page/printAppointmentMail.twig',
215                [
216                    'render' => $mail->getHtmlPart()
217                ]
218            );
219        }
220
221        return \BO\Slim\Render::withHtml(
222            $response,
223            'page/printWaitingNumber.twig',
224            array(
225                'title' => ($process->isWithAppointment()) ? 'Vorgangsnummer drucken' : 'Wartenummer drucken',
226                'process' => $process
227            )
228        );
229    }
230}