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