Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
97.06% covered (success)
97.06%
33 / 34
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
ProcessDeleteMail
97.06% covered (success)
97.06%
33 / 34
75.00% covered (warning)
75.00%
3 / 4
10
0.00% covered (danger)
0.00%
0 / 1
 readResponse
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
1
 writeMail
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
3
 testProcessAccess
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 testEmailRequired
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
3.33
1<?php
2
3/**
4 * @package ZMS API
5 * @copyright BerlinOnline Stadtportal GmbH & Co. KG
6 **/
7
8namespace BO\Zmsbackend\Process\Api;
9
10use BO\Slim\Render;
11use BO\Mellon\Validator;
12use BO\Zmsbackend\Mail\Service\Mail;
13use BO\Zmsbackend\Config\Service\Config;
14use BO\Zmsbackend\Process\Service\Process as ProcessRepository;
15use BO\Zmsbackend\Department\Service\Department as DepartmentRepository;
16use BO\Zmsentities\Process;
17
18/**
19 * @SuppressWarnings(Coupling)
20 */
21class ProcessDeleteMail extends \BO\Zmsbackend\Api\BaseController
22{
23    /**
24     * @SuppressWarnings(Param)
25     * @return string
26     */
27    #[\Override]
28    public function readResponse(
29        \Psr\Http\Message\RequestInterface $request,
30        \Psr\Http\Message\ResponseInterface $response,
31        array $args
32    ) {
33        $input = Validator::input()->isJson()->assertValid()->getValue();
34        $process = new Process($input);
35        $initiator = Validator::param('initiator')->isString()->getValue();
36
37        $process->testValid();
38        $this->testProcessAccess($process);
39        $process = (new ProcessRepository())->readEntity($process->id, $process->authKey);
40        $this->testEmailRequired($process);
41
42        \BO\Zmsbackend\Connection\Select::getWriteConnection();
43
44        $mail = $this->writeMail($process, $initiator);
45
46        $message = \BO\Zmsbackend\Api\Response\Message::create($request);
47        $message->data = $mail;
48
49        $response = Render::withLastModified($response, time(), '0');
50        $response = Render::withJson($response, $message, 200);
51        return $response;
52    }
53
54    protected static function writeMail(Process $process, $initiator = null)
55    {
56        $config = (new \BO\Zmsbackend\Config\Service\Config())->readEntity();
57        $department = (new DepartmentRepository())->readByScopeId($process->scope->id);
58        $collection = ProcessConfirmationMail::getProcessListOverview($process, $config);
59
60        $mail = (new \BO\Zmsentities\Mail())
61            ->setTemplateProvider(new \BO\Zmsbackend\Helper\MailTemplateProvider($process))
62            ->toResolvedEntity($collection, $config, 'deleted', $initiator)
63            ->withDepartment($department);
64        $mail->testValid();
65        if ($process->getFirstClient()->hasEmail() && $process->scope->hasEmailFrom()) {
66            $mail = (new \BO\Zmsbackend\Mail\Service\Mail())->writeInQueue($mail, \App::$now, false);
67            \App::$log->debug("Send mail", [$mail]);
68        }
69        return $mail;
70    }
71
72    /**
73     * @return void
74     */
75    protected function testProcessAccess(Process $process)
76    {
77        $authCheck = (new ProcessRepository())->readAuthKeyByProcessId($process->getId());
78        if (! $authCheck) {
79            throw new \BO\Zmsbackend\Process\Exception\ProcessNotFound();
80        }
81        if ($authCheck['authKey'] !== $process->authKey) {
82            throw new \BO\Zmsbackend\Process\Exception\AuthKeyMatchFailed();
83        }
84    }
85
86    /**
87     * @return void
88     */
89    protected function testEmailRequired(Process $process)
90    {
91        if (
92            $process->toProperty()->scope->preferences->client->emailRequired->get() &&
93            ! $process->getFirstClient()->hasEmail()
94        ) {
95            throw new \BO\Zmsbackend\Process\Exception\EmailRequired();
96        }
97    }
98}