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