Lines 78.57% 44 / 56
Functions and Methods 50.00% 2 / 4
Classes and Traits 0.00% 0 / 1
Name Lines Functions and Methods CRAP Classes and Traits
ProcessConfirm 78.57% 44 / 56 50.00% 2 / 4 10.98 0.00% 0 / 1
 readResponse 95.83% 23 / 24 0.00% 0 / 1 3
 writeMails 8.33% 1 / 12 0.00% 0 / 1 9.93
 updateOverviewCalendar 100.00% 15 / 15 100.00% 1 / 1 1
 testProcessData 100.00% 5 / 5 100.00% 1 / 1 3
1<?php
2
3/**
4 * @package ZMS API
5 * @copyright BerlinOnline Stadtportal GmbH & Co. KG
6 **/
7
8namespace BO\Zmsbackend\Process\Api;
9
10use BO\Slim\Render;
11use BO\Zmsbackend\Process\Service\Process;
12use BO\Zmsbackend\Mail\Service\Mail;
13use BO\Zmsbackend\Config\Service\Config;
14use BO\Mellon\Validator;
15
16/**
17 * @SuppressWarnings(CouplingBetweenObjects)
18 */
19class ProcessConfirm extends \BO\Zmsbackend\Api\BaseController
20{
21    /**
22     * @SuppressWarnings(Param)
23     * @return \Psr\Http\Message\ResponseInterface
24     */
25    #[\Override]
26    public function readResponse(
27        \Psr\Http\Message\RequestInterface $request,
28        \Psr\Http\Message\ResponseInterface $response,
29        array $args
30    ) {
31        \BO\Zmsbackend\Connection\Select::setCriticalReadSession();
32
33        $resolveReferences = Validator::param('resolveReferences')->isNumber()->setDefault(3)->getValue();
34        $input = Validator::input()->isJson()->assertValid()->getValue();
35        $entity = new \BO\Zmsentities\Process($input);
36        $entity->testValid();
37        $this->testProcessData($entity);
38
39        $userAccount = (new \BO\Zmsbackend\Helper\User($request))->readWorkstation()->getUseraccount();
40        $process = (new \BO\Zmsbackend\Process\Service\Process())->readEntity($entity->id, $entity->authKey, 2);
41
42        if ('preconfirmed' != $process->status && 'reserved' != $process->status) {
43            throw new \BO\Zmsbackend\Process\Exception\ProcessNotPreconfirmedAnymore();
44        }
45
46        $this->updateOverviewCalendar($process);
47
48        $process = (new \BO\Zmsbackend\Process\Service\Process())->updateProcessStatus(
49            $process,
50            'confirmed',
51            \App::$now,
52            $resolveReferences,
53            $userAccount
54        );
55        $this->writeMails($request, $process);
56        $message = \BO\Zmsbackend\Api\Response\Message::create($request);
57        $message->data = $process;
58
59        $response = Render::withLastModified($response, time(), '0');
60        $response = Render::withJson($response, $message->setUpdatedMetaData(), $message->getStatuscode());
61        return $response;
62    }
63    protected function writeMails(\Psr\Http\Message\RequestInterface $request, \BO\Zmsentities\Process|null $process): void
64    {
65        if ($process->hasScopeAdmin() && $process->sendAdminMailOnConfirmation()) {
66            $authority = $request->getUri()->getAuthority();
67            $validator = $request->getAttribute('validator');
68            $initiator = $validator->getParameter('initiator')
69                ->isString()
70                ->setDefault("$authority API-User")
71                ->getValue();
72            $config = (new \BO\Zmsbackend\Config\Service\Config())->readEntity();
73            $mail = (new \BO\Zmsentities\Mail())
74            ->setTemplateProvider(new \BO\Zmsbackend\Helper\MailTemplateProvider($process))
75            ->toResolvedEntity($process, $config, 'appointment', $initiator);
76            (new \BO\Zmsbackend\Mail\Service\Mail())->writeInQueueWithAdmin($mail, \App::$now);
77        }
78    }
79
80    private function updateOverviewCalendar(\BO\Zmsentities\Process $process): void
81    {
82        $appointment = $process->getFirstAppointment();
83        $scopeId = (int) $appointment->scope->id;
84
85        $timezone = new \DateTimeZone(\BO\Zmsbackend\Connection\Select::$connectionTimezone);
86        $startsAt = (new \DateTimeImmutable('@' . $appointment->date))->setTimezone($timezone);
87
88        $slotCount = (int)($appointment->slotCount ?? 0);
89        $slotTimeInMinutes = (int)($appointment->availability->slotTimeInMinutes ?? 0);
90        $durationMinutes   = $slotCount * $slotTimeInMinutes;
91
92        $endsAt = $startsAt->modify('+' . $durationMinutes . ' minutes');
93
94        (new \BO\Zmsbackend\OverviewCalendar\Service\OverviewCalendar())->insert(
95            $scopeId,
96            (int) $process->id,
97            'confirmed',
98            $startsAt,
99            $endsAt
100        );
101    }
102
103    /**
104     * @return void
105     */
106    protected function testProcessData(\BO\Zmsentities\Process $entity)
107    {
108        $authCheck = (new \BO\Zmsbackend\Process\Service\Process())->readAuthKeyByProcessId($entity->id);
109        if (! $authCheck) {
110            throw new \BO\Zmsbackend\Process\Exception\ProcessNotFound();
111        } elseif ($authCheck['authKey'] !== $entity->authKey) {
112            throw new \BO\Zmsbackend\Process\Exception\AuthKeyMatchFailed();
113        }
114    }
115}