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