Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
78.43% covered (warning)
78.43%
40 / 51
40.00% covered (danger)
40.00%
2 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
ProcessConfirmationMail
78.43% covered (warning)
78.43%
40 / 51
40.00% covered (danger)
40.00%
2 / 5
18.57
0.00% covered (danger)
0.00%
0 / 1
 readResponse
93.33% covered (success)
93.33%
14 / 15
0.00% covered (danger)
0.00%
0 / 1
3.00
 writeMail
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
4
 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
 getProcessListOverview
40.00% covered (danger)
40.00%
6 / 15
0.00% covered (danger)
0.00%
0 / 1
4.94
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\Process\Service\Process as ProcessRepository;
13use BO\Zmsbackend\Config\Service\Config;
14use BO\Zmsbackend\Department\Service\Department;
15use BO\Zmsentities\Process;
16use BO\Zmsentities\Collection\ProcessList as Collection;
17
18/**
19 * @SuppressWarnings(Coupling)
20 */
21class ProcessConfirmationMail 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        \BO\Zmsbackend\Connection\Select::setCriticalReadSession();
34
35        $input = Validator::input()->isJson()->assertValid()->getValue();
36        $process = new Process($input);
37        $process->testValid();
38        $this->testProcessAccess($process);
39        $process = (new ProcessRepository())->readEntity($process->id, $process->authKey);
40        if (!$process instanceof Process) {
41            throw new \BO\Zmsbackend\Process\Exception\ProcessNotFound();
42        }
43        $this->testEmailRequired($process);
44
45        $mail = $this->writeMail($process);
46        $message = \BO\Zmsbackend\Api\Response\Message::create($request);
47        $message->data = ($mail->hasId()) ? $mail : null;
48
49        $response = Render::withLastModified($response, time(), '0');
50        $response = Render::withJson($response, $message->setUpdatedMetaData(), $message->getStatuscode());
51        return $response;
52    }
53
54    protected static function writeMail(Process $process)
55    {
56        $config = (new Config())->readEntity();
57        $department = (new Department())->readByScopeId($process->scope['id']);
58        $collection = static::getProcessListOverview($process, $config);
59
60        $status = ($process->isWithAppointment()) ? 'appointment' : 'queued';
61        $mail = (new \BO\Zmsentities\Mail())
62            ->setTemplateProvider(new \BO\Zmsbackend\Helper\MailTemplateProvider($process))
63            ->toResolvedEntity($collection, $config, $status)
64            ->withDepartment($department);
65        $mail->testValid();
66        if ($process->getFirstClient()->hasEmail() && $process->scope->hasEmailFrom()) {
67            $mail = (new \BO\Zmsbackend\Mail\Service\Mail())->writeInQueue($mail, \App::$now, false);
68            \App::$log->debug("Send mail", [$mail]);
69        }
70        return $mail;
71    }
72
73    /**
74     * @return void
75     */
76    protected function testProcessAccess(Process $process)
77    {
78        $authCheck = (new ProcessRepository())->readAuthKeyByProcessId($process->id);
79        if (! $authCheck) {
80            throw new \BO\Zmsbackend\Process\Exception\ProcessNotFound();
81        }
82        if ($authCheck['authKey'] !== $process->authKey) {
83            throw new \BO\Zmsbackend\Process\Exception\AuthKeyMatchFailed();
84        }
85    }
86
87    /**
88     * @return void
89     */
90    protected function testEmailRequired(Process $process)
91    {
92        if (
93            $process->toProperty()->scope->preferences->client->emailRequired->get() &&
94            ! $process->getFirstClient()->hasEmail()
95        ) {
96            throw new \BO\Zmsbackend\Process\Exception\EmailRequired();
97        }
98    }
99
100    public static function getProcessListOverview(Process $process, \BO\Zmsentities\Config $config): Collection
101    {
102        $collection  = (new Collection())->addEntity($process);
103        if (
104            in_array(
105                getenv('ZMS_ENV'),
106                explode(',', $config->getPreference('appointments', 'enableSummaryByMail'))
107            ) && $process->getFirstClient()->hasEmail()
108        ) {
109            $processList = (new ProcessRepository())->readListByMailAndStatusList(
110                $process->getFirstClient()->email,
111                [
112                    \BO\Zmsentities\Process::STATUS_CONFIRMED
113                ],
114                2,
115                50
116            );
117            //add list of found processes without the main process
118            $collection->addList($processList->withOutProcessId($process->getId()));
119        }
120        return $collection;
121    }
122}