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 String
24     */
25    public function readResponse(
26        \Psr\Http\Message\RequestInterface $request,
27        \Psr\Http\Message\ResponseInterface $response,
28        array $args
29    ) {
30        $workstation = (new Helper\User($request))->checkRights();
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\Zmsdb\Connection\Select::getWriteConnection();
39        $query = new Query();
40        if ('pending' == $process['status']) {
41            $process = $query->updateEntity(
42                $process,
43                \App::$now,
44                0,
45                $process['status'],
46                $workstation->getUseraccount()
47            );
48            (new Workstation())->writeRemovedProcess($workstation);
49        } else {
50            $query->writeEntityFinished($process, \App::$now, true, $workstation->getUseraccount());
51        }
52
53        if ($survey) {
54            $this->writeSurveyMail($process);
55        }
56
57        $message = Response\Message::create($request);
58        $message->data = $process;
59
60        $response = Render::withLastModified($response, time(), '0');
61        $response = Render::withJson($response, $message->setUpdatedMetaData(), $message->getStatuscode());
62        return $response;
63    }
64
65    protected function testProcessInWorkstation($process, $workstation)
66    {
67        $department = (new \BO\Zmsdb\Department())->readByScopeId($workstation->scope['id'], 1);
68        $workstation->process = $process;
69        $workstation->testMatchingProcessScope($department->getScopeList());
70    }
71
72    protected function testProcessData($process)
73    {
74        $hasValidId = (
75            $process->hasId() &&
76            ('pending' == $process['status'] || 'finished' == $process['status'])
77        );
78        if (! $hasValidId) {
79            throw new Exception\Process\ProcessInvalid();
80        }
81
82        $processCheck = (new Process())->readEntity($process->id, new \BO\Zmsdb\Helper\NoAuth());
83        if (null === $processCheck || false === $processCheck->hasId()) {
84            throw new Exception\Process\ProcessNotFound();
85        } elseif ($processCheck->authKey != $process->authKey) {
86            throw new Exception\Process\AuthKeyMatchFailed();
87        }
88    }
89
90    protected function writeSurveyMail($process)
91    {
92        $process = clone $process;
93        foreach ($process->getClients() as $client) {
94            if ($client->hasSurveyAccepted()) {
95                $config = (new \BO\Zmsdb\Config())->readEntity();
96                $process->scope = (new \BO\Zmsdb\Scope())->readEntity($process['scope']['id'], 1);
97                $mail = (new \BO\Zmsentities\Mail())->toResolvedEntity($process, $config, 'survey');
98                (new \BO\Zmsdb\Mail())->writeInQueue($mail, \App::$now, false);
99            }
100        }
101    }
102}