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