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