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