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
17.13
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
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        if ($initiator && $process->hasScopeAdmin() && $process->sendAdminMailOnUpdated()) {
89            $config = (new Config())->readEntity();
90
91            $mail = (new \BO\Zmsentities\Mail())
92                    ->setTemplateProvider(new \BO\Zmsdb\Helper\MailTemplateProvider($process))
93                    ->toResolvedEntity($process, $config, 'updated', $initiator);
94            (new Mail())->writeInQueueWithAdmin($mail);
95        }
96        $message = Response\Message::create($request);
97        $message->data = $process;
98
99        $response = Render::withLastModified($response, time(), '0');
100
101        return Render::withJson($response, $message->setUpdatedMetaData(), $message->getStatuscode());
102    }
103
104    protected function testProcessData($entity, bool $checkMailLimit = true)
105    {
106        $authCheck = (new Process())->readAuthKeyByProcessId($entity->id);
107
108        if ($checkMailLimit && ! (new Process())->isAppointmentAllowedWithSameMail($entity)) {
109            throw new Exception\Process\MoreThanAllowedAppointmentsPerMail();
110        }
111
112        if (! $authCheck) {
113            throw new Exception\Process\ProcessNotFound();
114        } elseif ($authCheck['authKey'] != $entity->authKey && $authCheck['authName'] != $entity->authKey) {
115            throw new Exception\Process\AuthKeyMatchFailed();
116        }
117    }
118
119    private function syncOverviewCalendarFromProcess(
120        \BO\Zmsentities\Process $entity,
121        \BO\Zmsentities\Process $process
122    ): void {
123        $appointment        = $entity->getFirstAppointment();
124        $connectionTimezone = new \DateTimeZone(\BO\Zmsdb\Connection\Select::$connectionTimezone);
125
126        $startsAt = (new \DateTimeImmutable('@' . $appointment->date))
127            ->setTimezone($connectionTimezone);
128
129        $slotCount = (int) $appointment->slotCount;
130        $scopeId   = (int) $appointment->scope->id;
131
132        $slotTimeInMinutes = (int) $process->getFirstAppointment()->availability->slotTimeInMinutes;
133
134        $endsAt = $startsAt->modify('+' . ($slotCount * $slotTimeInMinutes) . ' minutes');
135
136        (new \BO\Zmsdb\OverviewCalendar())->updateByProcess(
137            (int) $entity->id,
138            $scopeId,
139            $startsAt,
140            $endsAt
141        );
142    }
143}