Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
79.59% covered (warning)
79.59%
39 / 49
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
ProcessConfirmationMail
79.59% covered (warning)
79.59%
39 / 49
75.00% covered (warning)
75.00%
3 / 4
16.91
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
6
 getProcessListOverview
37.50% covered (danger)
37.50%
6 / 16
0.00% covered (danger)
0.00%
0 / 1
5.20
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    public function readResponse(
28        \Psr\Http\Message\RequestInterface $request,
29        \Psr\Http\Message\ResponseInterface $response,
30        array $args
31    ) {
32        \BO\Zmsdb\Connection\Select::setCriticalReadSession();
33
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
40        $mail = $this->writeMail($process);
41        $message = 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        $collection = static::getProcessListOverview($process, $config);
54
55        $status = ($process->isWithAppointment()) ? 'appointment' : 'queued';
56        $mail = (new \BO\Zmsentities\Mail())
57            ->setTemplateProvider(new \BO\Zmsdb\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\Zmsdb\Mail())->writeInQueue($mail, \App::$now, false);
63            \App::$log->debug("Send mail", [$mail]);
64        }
65        return $mail;
66    }
67
68    protected function testProcessData($process)
69    {
70        $authCheck = (new ProcessRepository())->readAuthKeyByProcessId($process->id);
71        if (! $authCheck) {
72            throw new Exception\Process\ProcessNotFound();
73        } elseif ($authCheck['authKey'] != $process->authKey && $authCheck['authName'] != $process->authKey) {
74            throw new Exception\Process\AuthKeyMatchFailed();
75        } elseif (
76            $process->toProperty()->scope->preferences->client->emailRequired->get() &&
77            ! $process->getFirstClient()->hasEmail()
78        ) {
79            throw new Exception\Process\EmailRequired();
80        }
81    }
82
83    public static function getProcessListOverview($process, $config)
84    {
85        $collection  = (new Collection())->addEntity($process);
86        if (
87            in_array(
88                getenv('ZMS_ENV'),
89                explode(',', $config->getPreference('appointments', 'enableSummaryByMail'))
90            ) && $process->getFirstClient()->hasEmail()
91        ) {
92            $processList = (new ProcessRepository())->readListByMailAndStatusList(
93                $process->getFirstClient()->email,
94                [
95                    Process::STATUS_CONFIRMED,
96                    Process::STATUS_PICKUP
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}