Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
31 / 31
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
ProcessDeleteMail
100.00% covered (success)
100.00%
31 / 31
100.00% covered (success)
100.00%
3 / 3
8
100.00% covered (success)
100.00%
1 / 1
 readResponse
100.00% covered (success)
100.00%
13 / 13
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
 testProcessData
100.00% covered (success)
100.00%
6 / 6
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\Mellon\Validator;
12use BO\Zmsdb\Mail as Query;
13use BO\Zmsdb\Config;
14use BO\Zmsdb\Process as ProcessRepository;
15use BO\Zmsdb\Department as DepartmentRepository;
16use BO\Zmsentities\Process;
17
18/**
19 * @SuppressWarnings(Coupling)
20 */
21class ProcessDeleteMail extends BaseController
22{
23    /**
24     * @SuppressWarnings(Param)
25     * @return string
26     */
27    public function readResponse(
28        \Psr\Http\Message\RequestInterface $request,
29        \Psr\Http\Message\ResponseInterface $response,
30        array $args
31    ) {
32        $input = Validator::input()->isJson()->assertValid()->getValue();
33        $process = new Process($input);
34        $initiator = Validator::param('initiator')->isString()->getValue();
35
36        $process->testValid();
37        $this->testProcessData($process);
38        $process = (new ProcessRepository())->readEntity($process->id, $process->authKey);
39
40        \BO\Zmsdb\Connection\Select::getWriteConnection();
41
42        $mail = $this->writeMail($process, $initiator);
43
44        $message = Response\Message::create($request);
45        $message->data = $mail;
46
47        $response = Render::withLastModified($response, time(), '0');
48        $response = Render::withJson($response, $message, 200);
49        return $response;
50    }
51
52    protected static function writeMail(Process $process, $initiator = null)
53    {
54        $config = (new Config())->readEntity();
55        $department = (new DepartmentRepository())->readByScopeId($process->scope['id']);
56        $collection = ProcessConfirmationMail::getProcessListOverview($process, $config);
57
58        $mail = (new \BO\Zmsentities\Mail())
59            ->setTemplateProvider(new \BO\Zmsdb\Helper\MailTemplateProvider($process))
60            ->toResolvedEntity($collection, $config, 'deleted', $initiator)
61            ->withDepartment($department);
62        $mail->testValid();
63        if ($process->getFirstClient()->hasEmail() && $process->scope->hasEmailFrom()) {
64            $mail = (new \BO\Zmsdb\Mail())->writeInQueue($mail, \App::$now, false);
65            \App::$log->debug("Send mail", [$mail]);
66        }
67        return $mail;
68    }
69
70    protected function testProcessData($process)
71    {
72        $authCheck = (new ProcessRepository())->readAuthKeyByProcessId($process->getId());
73        if (! $authCheck) {
74            throw new Exception\Process\ProcessNotFound();
75        } elseif (
76            $process->toProperty()->scope->preferences->client->emailRequired->get() &&
77            ! $process->getFirstClient()->hasEmail()
78        ) {
79            throw new Exception\Process\EmailRequired();
80        }
81    }
82}