Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
73.77% covered (warning)
73.77%
45 / 61
40.00% covered (danger)
40.00%
2 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
ProcessConfirm
73.77% covered (warning)
73.77%
45 / 61
40.00% covered (danger)
40.00%
2 / 5
17.54
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
 validateProcessLimits
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
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, 2);
40
41        //$this->validateProcessLimits($process); Should be moved to zmscitizenapi.
42        if ('preconfirmed' != $process->status && 'reserved' != $process->status) {
43            throw new Exception\Process\ProcessNotPreconfirmedAnymore();
44        }
45
46        $this->updateOverviewCalendar($process);
47
48        $process = (new Process())->updateProcessStatus(
49            $process,
50            'confirmed',
51            \App::$now,
52            $resolveReferences,
53            $userAccount
54        );
55        $this->writeMails($request, $process);
56        $message = 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($request, $process)
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 Config())->readEntity();
73            $mail = (new \BO\Zmsentities\Mail())
74            ->setTemplateProvider(new \BO\Zmsdb\Helper\MailTemplateProvider($process))
75            ->toResolvedEntity($process, $config, 'appointment', $initiator);
76            (new 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\Zmsdb\Connection\Select::$connectionTimezone);
86        $startsAt = (new \DateTimeImmutable('@' . $appointment->date))->setTimezone($timezone);
87
88        $durationMinutes = null;
89
90        $slotCount = (int)($appointment->slotCount ?? 0);
91        $slotTimeInMinutes = (int)($appointment->availability->slotTimeInMinutes ?? 0);
92        $durationMinutes   = $slotCount * $slotTimeInMinutes;
93
94        $endsAt = $startsAt->modify('+' . $durationMinutes . ' minutes');
95
96        (new \BO\Zmsdb\OverviewCalendar())->insert(
97            $scopeId,
98            (int) $process->id,
99            'confirmed',
100            $startsAt,
101            $endsAt
102        );
103    }
104
105    protected function testProcessData($entity)
106    {
107        $authCheck = (new Process())->readAuthKeyByProcessId($entity->id);
108        if (! $authCheck) {
109            throw new Exception\Process\ProcessNotFound();
110        } elseif ($authCheck['authKey'] != $entity->authKey && $authCheck['authName'] != $entity->authKey) {
111            throw new Exception\Process\AuthKeyMatchFailed();
112        }
113    }
114
115    protected function validateProcessLimits(\BO\Zmsentities\Process $process)
116    {
117        if (! (new Process())->isAppointmentSlotCountAllowed($process)) {
118            throw new Exception\Process\MoreThanAllowedSlotsPerAppointment();
119        }
120
121        if (! (new Process())->isServiceQuantityAllowed($process)) {
122            throw new Exception\Process\MoreThanAllowedQuantityPerService();
123        }
124    }
125}