Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
71.79% covered (warning)
71.79%
28 / 39
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ProcessDelete
71.79% covered (warning)
71.79%
28 / 39
66.67% covered (warning)
66.67%
2 / 3
15.23
0.00% covered (danger)
0.00%
0 / 1
 readResponse
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
1 / 1
5
 writeMails
8.33% covered (danger)
8.33%
1 / 12
0.00% covered (danger)
0.00%
0 / 1
9.93
 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 ProcessDelete 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        $workstation = (new Helper\User($request))->readWorkstation();
31        \BO\Zmsdb\Connection\Select::getWriteConnection();
32        $process = (new Process())->readEntity($args['id'], new \BO\Zmsdb\Helper\NoAuth(), 2);
33        $this->testProcessData($process, $args['authKey']);
34        if ('reserved' == $process->status) {
35            if (!(new Process())->writeBlockedEntity($process, false, $workstation->getUseraccount())) {
36                throw new Exception\Process\ProcessDeleteFailed(); // @codeCoverageIgnore
37            }
38            $processDeleted = $process;
39        } else {
40            $scopeId = (int) $process->scope->id;
41            (new \BO\Zmsdb\OverallCalendar())->unbook($scopeId, $process->id);
42
43            $processDeleted = (new Process())->writeCanceledEntity(
44                $args['id'],
45                $args['authKey'],
46                null,
47                $workstation->getUseraccount()
48            );
49            if (! $processDeleted || ! $processDeleted->hasId()) {
50                throw new Exception\Process\ProcessDeleteFailed(); // @codeCoverageIgnore
51            }
52        }
53        $this->writeMails($request, $process);
54        $message = Response\Message::create($request);
55        $message->data = $processDeleted;
56
57        $response = Render::withLastModified($response, time(), '0');
58        $response = Render::withJson($response, $message->setUpdatedMetaData(), $message->getStatuscode());
59        return $response;
60    }
61
62    protected function writeMails($request, $process)
63    {
64        if ($process->hasScopeAdmin() && $process->sendAdminMailOnDeleted()) {
65            $authority = $request->getUri()->getAuthority();
66            $validator = $request->getAttribute('validator');
67            $initiator = $validator->getParameter('initiator')
68                ->isString()
69                ->setDefault("$authority API-User")
70                ->getValue();
71            $config = (new Config())->readEntity();
72            $mail = (new \BO\Zmsentities\Mail())
73                    ->setTemplateProvider(new \BO\Zmsdb\Helper\MailTemplateProvider($process))
74                    ->toResolvedEntity($process, $config, 'deleted', $initiator);
75            (new Mail())->writeInQueueWithAdmin($mail, \App::$now);
76        }
77    }
78
79    protected function testProcessData($process, $authKey)
80    {
81        if (! $process) {
82            throw new Exception\Process\ProcessNotFound();
83        }
84        $authName = $process->getFirstClient()['familyName'];
85        if ($process['authKey'] != $authKey && $authName != $authKey) {
86            throw new Exception\Process\AuthKeyMatchFailed();
87        }
88    }
89}