Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
38 / 38
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
ProcessFinished
100.00% covered (success)
100.00%
38 / 38
100.00% covered (success)
100.00%
4 / 4
12
100.00% covered (success)
100.00%
1 / 1
 readResponse
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
2
 testProcessInWorkstation
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 testProcessData
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
6
 writeSurveyMail
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
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\ProcessStatusArchived as Query;
13use BO\Zmsbackend\ProcessSearchHistory\Service\ProcessSearchHistory as HistoryService;
14
15/**
16 * @SuppressWarnings(Coupling)
17 */
18class ProcessFinished extends \BO\Zmsbackend\Api\BaseController
19{
20    /**
21     * @SuppressWarnings(Param)
22     * @return \Psr\Http\Message\ResponseInterface
23     */
24    #[\Override]
25    public function readResponse(
26        \Psr\Http\Message\RequestInterface $request,
27        \Psr\Http\Message\ResponseInterface $response,
28        array $args
29    ) {
30        $workstation = (new \BO\Zmsbackend\Helper\User($request))->checkPermissions('appointment');
31        $input = Validator::input()->isJson()->assertValid()->getValue();
32        $survey = Validator::param('survey')->isNumber()->setDefault(1)->getValue();
33        $process = new \BO\Zmsentities\Process($input);
34        $process->testValid();
35        $this->testProcessData($process);
36        $this->testProcessInWorkstation($process, $workstation);
37
38        \BO\Zmsbackend\Connection\Select::getWriteConnection();
39        $query = new Query();
40        $query->writeEntityFinished($process, \App::$now, false, $workstation->getUseraccount(), HistoryService::STATUS_COMPLETED);
41
42        if ($survey) {
43            $this->writeSurveyMail($process);
44        }
45
46        $message = \BO\Zmsbackend\Api\Response\Message::create($request);
47        $message->data = $process;
48
49        $response = Render::withLastModified($response, time(), '0');
50        $response = Render::withJson($response, $message->setUpdatedMetaData(), $message->getStatuscode());
51        return $response;
52    }
53
54    protected function testProcessInWorkstation(\BO\Zmsentities\Process $process, $workstation): void
55    {
56        $department = (new \BO\Zmsbackend\Department\Service\Department())->readByScopeId($workstation->scope['id'], 1);
57        $workstation->process = $process;
58        $workstation->validateProcessScopeAccess($department->getScopeList());
59    }
60
61    /**
62     * @return void
63     */
64    protected function testProcessData(\BO\Zmsentities\Process $process)
65    {
66        $hasValidId = (
67            $process->hasId() &&
68            'finished' == $process['status']
69        );
70        if (! $hasValidId) {
71            throw new \BO\Zmsbackend\Process\Exception\ProcessInvalid();
72        }
73
74        $processCheck = (new \BO\Zmsbackend\Process\Service\Process())->readEntity($process->id, new \BO\Zmsbackend\Helper\NoAuth());
75        if (null === $processCheck || false === $processCheck->hasId()) {
76            throw new \BO\Zmsbackend\Process\Exception\ProcessNotFound();
77        } elseif ($processCheck->authKey !== $process->authKey) {
78            throw new \BO\Zmsbackend\Process\Exception\AuthKeyMatchFailed();
79        }
80    }
81
82    protected function writeSurveyMail($process): void
83    {
84        $process = clone $process;
85        foreach ($process->getClients() as $client) {
86            if ($client->hasSurveyAccepted()) {
87                $config = (new \BO\Zmsbackend\Config\Service\Config())->readEntity();
88                $process->scope = (new \BO\Zmsbackend\Scope\Service\Scope())->readEntity($process['scope']['id'], 1);
89                $mail = (new \BO\Zmsentities\Mail())->toResolvedEntity($process, $config, 'survey');
90                (new \BO\Zmsbackend\Mail\Service\Mail())->writeInQueue($mail, \App::$now, false);
91            }
92        }
93    }
94}