Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
92.41% covered (success)
92.41%
73 / 79
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ProcessUpdate
92.41% covered (success)
92.41%
73 / 79
33.33% covered (danger)
33.33%
1 / 3
16.11
0.00% covered (danger)
0.00%
0 / 1
 readResponse
91.38% covered (success)
91.38%
53 / 58
0.00% covered (danger)
0.00%
0 / 1
10.06
 testProcessData
85.71% covered (warning)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
5.07
 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 \Psr\Http\Message\ResponseInterface
20 */
21class ProcessUpdate extends BaseController
22{
23    /**
24     * @SuppressWarnings(Param)
25     * @SuppressWarnings(Complexity)
26     * @return \Psr\Http\Message\ResponseInterface
27     */
28    #[\Override]
29    public function readResponse(
30        \Psr\Http\Message\RequestInterface $request,
31        \Psr\Http\Message\ResponseInterface $response,
32        array $args
33    ) {
34        $slotsRequired = Validator::param('slotsRequired')->isNumber()->getValue();
35        $slotType = Validator::param('slotType')->isString()->getValue();
36        $clientKey = Validator::param('clientkey')->isString()->getValue();
37        $initiator = Validator::param('initiator')->isString()->getValue();
38        $resolveReferences = Validator::param('resolveReferences')->isNumber()->setDefault(2)->getValue();
39        $input = Validator::input()->isJson()->assertValid()->getValue();
40        $entity = new \BO\Zmsentities\Process($input);
41        $entity->testValid();
42        $this->testProcessData($entity, ! $initiator);
43
44        \BO\Zmsdb\Connection\Select::setCriticalReadSession();
45        $workstation = (new Helper\User($request))->readWorkstation();
46
47        if ($slotType || $slotsRequired) {
48            $process = Process::init()->updateEntityWithSlots(
49                $entity,
50                \App::$now,
51                $slotType,
52                $slotsRequired,
53                $resolveReferences,
54                $workstation->getUseraccount()
55            );
56            Helper\Matching::testCurrentScopeHasRequest($process);
57            $this->syncOverviewCalendarFromProcess($entity, $process);
58        } elseif ($clientKey) {
59            $apiClient = (new \BO\Zmsdb\Apiclient())->readEntity($clientKey);
60            if (!$apiClient || !isset($apiClient->accesslevel) || $apiClient->accesslevel == 'blocked') {
61                throw new Exception\Process\ApiclientInvalid();
62            }
63            $entity->apiclient = $apiClient;
64            $process = (new Process())->updateEntity(
65                $entity,
66                \App::$now,
67                $resolveReferences,
68                null,
69                $workstation->getUseraccount()
70            );
71        } else {
72            $process = (new Process())->updateEntity(
73                $entity,
74                \App::$now,
75                $resolveReferences,
76                null,
77                $workstation->getUseraccount()
78            );
79
80            Log::writeProcessLog(
81                "UPDATE (ProcessUpdate.php) $process ",
82                Log::ACTION_CALLED,
83                $process,
84                $workstation->getUseraccount()
85            );
86            $this->syncOverviewCalendarFromProcess($entity, $process);
87        }
88
89        if ($initiator && $process->hasScopeAdmin() && $process->sendAdminMailOnUpdated()) {
90            $config = (new Config())->readEntity();
91
92            $mail = (new \BO\Zmsentities\Mail())
93                    ->setTemplateProvider(new \BO\Zmsdb\Helper\MailTemplateProvider($process))
94                    ->toResolvedEntity($process, $config, 'updated', $initiator);
95            (new Mail())->writeInQueueWithAdmin($mail);
96        }
97        $message = Response\Message::create($request);
98        $message->data = $process;
99
100        $response = Render::withLastModified($response, time(), '0');
101
102        return Render::withJson($response, $message->setUpdatedMetaData(), $message->getStatuscode());
103    }
104
105    protected function testProcessData($entity, bool $checkMailLimit = true)
106    {
107        $authCheck = (new Process())->readAuthKeyByProcessId($entity->id);
108
109        if ($checkMailLimit && ! (new Process())->isAppointmentAllowedWithSameMail($entity)) {
110            throw new Exception\Process\MoreThanAllowedAppointmentsPerMail();
111        }
112
113        /*if (! (new Process())->isAppointmentSlotCountAllowed($entity)) {
114            throw new Exception\Process\MoreThanAllowedSlotsPerAppointment();
115        } Should be moved to zmscitizenapi. */
116
117        // Note: isServiceQuantityAllowed is only checked in ProcessPreconfirm/ProcessConfirm
118        // to reduce DB queries on this frequently-called endpoint
119
120        if (! $authCheck) {
121            throw new Exception\Process\ProcessNotFound();
122        } elseif ($authCheck['authKey'] !== $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}