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