Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
75.00% covered (warning)
75.00%
45 / 60
25.00% covered (danger)
25.00%
1 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
ProcessConfirm
75.00% covered (warning)
75.00%
45 / 60
25.00% covered (danger)
25.00%
1 / 4
21.52
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
 updateOverallCalendar
84.21% covered (warning)
84.21%
16 / 19
0.00% covered (danger)
0.00%
0 / 1
7.19
 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->updateOverallCalendar($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 updateOverallCalendar(\BO\Zmsentities\Process $process): void
79    {
80        $appointment = $process->getFirstAppointment();
81        if (!$appointment || !$appointment->date || !$appointment->scope) {
82            return;
83        }
84
85        $scopeId = (int) $appointment->scope->id;
86
87        $time = (new \DateTimeImmutable('@' . $appointment->date))
88            ->setTimezone(new \DateTimeZone(\BO\Zmsdb\Connection\Select::$connectionTimezone))
89            ->format('Y-m-d H:i:00');
90
91        $duration = 0;
92        foreach ($process->requests as $req) {
93            if (!empty($req['data']['duration'])) {
94                $duration += (int) $req['data']['duration'];
95            }
96        }
97
98        $duration = $duration ?: 5;
99        $slotUnits = (int) ceil($duration / 5);
100
101        (new \BO\Zmsdb\OverallCalendar())->book(
102            $scopeId,
103            $time,
104            $process->id,
105            $slotUnits
106        );
107    }
108
109    protected function testProcessData($entity)
110    {
111        $authCheck = (new Process())->readAuthKeyByProcessId($entity->id);
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}