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