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