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