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