Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
97.30% covered (success)
97.30%
72 / 74
60.00% covered (warning)
60.00%
3 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
ProcessSave
97.30% covered (success)
97.30%
72 / 74
60.00% covered (warning)
60.00%
3 / 5
21
0.00% covered (danger)
0.00%
0 / 1
 readResponse
97.37% covered (success)
97.37%
37 / 38
0.00% covered (danger)
0.00%
0 / 1
5
 getSuccessMessage
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 getConflictList
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
5
 writeUpdatedProcess
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
4
 shouldSendNotifications
92.31% covered (success)
92.31%
12 / 13
0.00% covered (danger)
0.00%
0 / 1
5.01
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;
16use BO\Zmsadmin\Helper\AppointmentFormHelper;
17
18/**
19 * Update a process
20 */
21class ProcessSave extends BaseController
22{
23    /**
24     * @SuppressWarnings(Param)
25     * @return String
26     */
27    public function readResponse(
28        \Psr\Http\Message\RequestInterface $request,
29        \Psr\Http\Message\ResponseInterface $response,
30        array $args
31    ) {
32        $workstation = \App::$http->readGetResult('/workstation/', ['resolveReferences' => 2])->getEntity();
33
34        $validator = $request->getAttribute('validator');
35        $input = $request->getParams();
36
37        $scope = Helper\AppointmentFormHelper::readSelectedScope($request, $workstation);
38        $processId = $validator->value($args['id'])->isNumber()->getValue();
39        $process = \App::$http->readGetResult('/process/' . $processId . '/')->getEntity();
40        $dateTime = ($process->isWithAppointment()) ?
41            (new \DateTime())->setTimestamp($process->getFirstAppointment()->date) :
42            \App::$now;
43        $shouldNotify = $this->shouldSendNotifications($input, $process);
44        $process->withUpdatedData($input, $dateTime, $scope);
45
46        $validatedForm = ($process->isWithAppointment()) ?
47            ProcessReserve::getValidatedForm($validator, $process) :
48            ProcessQueue::getValidatedForm($validator, $process);
49
50        if ($validatedForm['failed']) {
51            return \BO\Slim\Render::withJson(
52                $response,
53                $validatedForm
54            );
55        }
56
57        $process = $this->writeUpdatedProcess(
58            $input,
59            $process,
60            $validator,
61            $shouldNotify
62        );
63        $appointment = $process->getFirstAppointment();
64        $conflictList = ($process->isWithAppointment()) ?
65            static::getConflictList($scope->getId(), $appointment) :
66            null;
67        return \BO\Slim\Render::withHtml(
68            $response,
69            'element/helper/messageHandler.twig',
70            array(
71                'selectedprocess' => $process,
72                'success' => $this->getSuccessMessage($process),
73                'conflictlist' => $conflictList
74            )
75        );
76    }
77
78    protected function getSuccessMessage(Entity $process)
79    {
80        return ($process->isWithAppointment()) ? 'process_updated' : 'process_withoutappointment_updated';
81    }
82
83    public static function getConflictList($scopeId, $appointment)
84    {
85        $conflictList = ScopeAvailabilityDay::readConflictList($scopeId, $appointment->getStartTime());
86        $conflictList = ($conflictList && $conflictList->count()) ?
87            $conflictList
88                ->withTimeRangeByAppointment($appointment)
89                ->setConflictAmendment()
90                ->toConflictListByDay() :
91            null;
92        $dayKey = $appointment->getStartTime()->format('Y-m-d');
93        return ($conflictList && isset($conflictList[$dayKey])) ? $conflictList[$dayKey] : null;
94    }
95
96    protected function writeUpdatedProcess($input, Entity $process, $validator, $notify = true)
97    {
98        $initiator = $validator->getParameter('initiator')->isString()->getValue();
99        $process = \App::$http->readPostResult(
100            '/process/' . $process->id . '/' . $process->authKey . '/',
101            $process,
102            [
103                'initiator' => $initiator ?? 'admin',
104                'slotType' => 'intern',
105                'slotsRequired' => (isset($input['slotCount']) && 1 < $input['slotCount']) ? $input['slotCount'] : 0
106            ]
107        )->getEntity();
108
109        if ($notify) {
110            AppointmentFormHelper::updateMail($input, $process);
111        }
112
113        return $process;
114    }
115
116    private function shouldSendNotifications($requestData, \BO\Zmsentities\Schema\Entity $process)
117    {
118        $requestIds = $requestData['requests'] ?? [];
119        $currentRequestIds = [];
120        foreach ($process->getRequests() as $request) {
121            $currentRequestIds[] = $request['id'];
122        }
123
124        if (array_merge(array_diff($requestIds, $currentRequestIds), array_diff($currentRequestIds, $requestIds))) {
125            return true;
126        }
127
128        if ($process->getFirstClient()['familyName'] !== $requestData['familyName']) {
129            return true;
130        }
131
132        $newDate = $requestData['selecteddate'] . ' '
133            . str_replace('-', ':', $requestData['selectedtime']);
134
135        if ($process->getFirstAppointment()->toDateTime()->format('Y-m-d H:i') !== $newDate) {
136            return true;
137        }
138
139        return false;
140    }
141}