Lines 5.55% 3 / 54
Methods 16.66% 1 / 6
Classes 0.00% 0 / 1
Name Lines Methods CRAP
 readResponse 0.00% 0 / 15 0.00% 0 / 1 12
 writeMail 0.00% 0 / 13 0.00% 0 / 1 20
 testProcessAccess 0.00% 0 / 5 0.00% 0 / 1 12
 testEmailRequired 0.00% 0 / 3 0.00% 0 / 1 12
 getProcessListOverview 0.00% 0 / 15 0.00% 0 / 1 12
 [BO\Zmsbackend\Api\BaseController] __invoke 100.00% 3 / 3 100.00% 1 / 1 1
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}

Inherited from BO\Zmsbackend\Api\BaseController

20    public function __invoke(RequestInterface $request, ResponseInterface $response, array $args)
21    {
22        $request = $this->initRequest($request);
23        $noCacheResponse = \BO\Slim\Render::withLastModified($response, time(), '0');
24        return $this->readResponse($request, $noCacheResponse, $args);
25    }