Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
93.02% covered (success)
93.02%
80 / 86
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ProcessUpdate
93.02% covered (success)
93.02%
80 / 86
33.33% covered (danger)
33.33%
1 / 3
17.10
0.00% covered (danger)
0.00%
0 / 1
 readResponse
92.31% covered (success)
92.31%
60 / 65
0.00% covered (danger)
0.00%
0 / 1
10.05
 testProcessData
85.71% covered (warning)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
6.10
 syncOverviewCalendarFromProcess
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * @package ZMS API
5 * @copyright BerlinOnline Stadtportal GmbH & Co. KG
6 **/
7
8namespace BO\Zmsapi;
9
10use BO\Slim\Render;
11use BO\Zmsdb\Config;
12use BO\Zmsdb\Log;
13use BO\Zmsdb\Mail;
14use BO\Mellon\Validator;
15use BO\Zmsdb\Process;
16
17/**
18 * @SuppressWarnings(Coupling)
19 * @return String
20 */
21class ProcessUpdate extends BaseController
22{
23    /**
24     * @SuppressWarnings(Param)
25     * @SuppressWarnings(Complexity)
26     * @return String
27     */
28    public function readResponse(
29        \Psr\Http\Message\RequestInterface $request,
30        \Psr\Http\Message\ResponseInterface $response,
31        array $args
32    ) {
33        $slotsRequired = Validator::param('slotsRequired')->isNumber()->getValue();
34        $slotType = Validator::param('slotType')->isString()->getValue();
35        $clientKey = Validator::param('clientkey')->isString()->getValue();
36        $initiator = Validator::param('initiator')->isString()->getValue();
37        $resolveReferences = Validator::param('resolveReferences')->isNumber()->setDefault(2)->getValue();
38        $input = Validator::input()->isJson()->assertValid()->getValue();
39        $entity = new \BO\Zmsentities\Process($input);
40        $entity->testValid();
41        $this->testProcessData($entity, ! $initiator);
42
43        \BO\Zmsdb\Connection\Select::setCriticalReadSession();
44        $workstation = (new Helper\User($request))->readWorkstation();
45
46        if ($slotType || $slotsRequired) {
47            $process = Process::init()->updateEntityWithSlots(
48                $entity,
49                \App::$now,
50                $slotType,
51                $slotsRequired,
52                $resolveReferences,
53                $workstation->getUseraccount()
54            );
55            Helper\Matching::testCurrentScopeHasRequest($process);
56            $this->syncOverviewCalendarFromProcess($entity, $process);
57        } elseif ($clientKey) {
58            $apiClient = (new \BO\Zmsdb\Apiclient())->readEntity($clientKey);
59            if (!$apiClient || !isset($apiClient->accesslevel) || $apiClient->accesslevel == 'blocked') {
60                throw new Exception\Process\ApiclientInvalid();
61            }
62            $entity->apiclient = $apiClient;
63            $process = (new Process())->updateEntity(
64                $entity,
65                \App::$now,
66                $resolveReferences,
67                null,
68                $workstation->getUseraccount()
69            );
70        } else {
71            $process = (new Process())->updateEntity(
72                $entity,
73                \App::$now,
74                $resolveReferences,
75                null,
76                $workstation->getUseraccount()
77            );
78
79            Log::writeProcessLog(
80                "UPDATE (ProcessUpdate.php) $process ",
81                Log::ACTION_CALLED,
82                $process,
83                $workstation->getUseraccount()
84            );
85            $this->syncOverviewCalendarFromProcess($entity, $process);
86        }
87
88        $process = (new Process())->updateProcessStatus(
89            $process,
90            $process->status,
91            \App::$now,
92            $resolveReferences,
93            $workstation->getUseraccount()
94        );
95
96        if ($initiator && $process->hasScopeAdmin() && $process->sendAdminMailOnUpdated()) {
97            $config = (new Config())->readEntity();
98
99            $mail = (new \BO\Zmsentities\Mail())
100                    ->setTemplateProvider(new \BO\Zmsdb\Helper\MailTemplateProvider($process))
101                    ->toResolvedEntity($process, $config, 'updated', $initiator);
102            (new Mail())->writeInQueueWithAdmin($mail);
103        }
104        $message = Response\Message::create($request);
105        $message->data = $process;
106
107        $response = Render::withLastModified($response, time(), '0');
108
109        return Render::withJson($response, $message->setUpdatedMetaData(), $message->getStatuscode());
110    }
111
112    protected function testProcessData($entity, bool $checkMailLimit = true)
113    {
114        $authCheck = (new Process())->readAuthKeyByProcessId($entity->id);
115
116        if ($checkMailLimit && ! (new Process())->isAppointmentAllowedWithSameMail($entity)) {
117            throw new Exception\Process\MoreThanAllowedAppointmentsPerMail();
118        }
119
120        if (! $authCheck) {
121            throw new Exception\Process\ProcessNotFound();
122        } elseif ($authCheck['authKey'] != $entity->authKey && $authCheck['authName'] != $entity->authKey) {
123            throw new Exception\Process\AuthKeyMatchFailed();
124        }
125    }
126
127    private function syncOverviewCalendarFromProcess(
128        \BO\Zmsentities\Process $entity,
129        \BO\Zmsentities\Process $process
130    ): void {
131        $appointment        = $entity->getFirstAppointment();
132        $connectionTimezone = new \DateTimeZone(\BO\Zmsdb\Connection\Select::$connectionTimezone);
133
134        $startsAt = (new \DateTimeImmutable('@' . $appointment->date))
135            ->setTimezone($connectionTimezone);
136
137        $slotCount = (int) $appointment->slotCount;
138        $scopeId   = (int) $appointment->scope->id;
139
140        $slotTimeInMinutes = (int) $process->getFirstAppointment()->availability->slotTimeInMinutes;
141
142        $endsAt = $startsAt->modify('+' . ($slotCount * $slotTimeInMinutes) . ' minutes');
143
144        (new \BO\Zmsdb\OverviewCalendar())->updateByProcess(
145            (int) $entity->id,
146            $scopeId,
147            $startsAt,
148            $endsAt
149        );
150    }
151}