Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 68
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
ProcessRedirect
0.00% covered (danger)
0.00%
0 / 68
0.00% covered (danger)
0.00%
0 / 4
182
0.00% covered (danger)
0.00%
0 / 1
 readResponse
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 1
2
 testProcessData
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
20
 testProcessAccess
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
20
 readValidProcess
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 1
20
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\Process;
13use BO\Zmsdb\Process as Query;
14use BO\Zmsdb\ProcessStatusQueued;
15use BO\Zmsdb\Workstation;
16use BO\Zmsentities\Collection\RequestList;
17
18/**
19 * @SuppressWarnings(Coupling)
20 */
21class ProcessRedirect extends BaseController
22{
23    /**
24     * @SuppressWarnings(Param)
25     * @return String
26     */
27    public function readResponse(
28        \Psr\Http\Message\RequestInterface $request,
29        \Psr\Http\Message\ResponseInterface $response,
30        array $args
31    ) {
32        $workstation = (new Helper\User($request))->checkRights();
33        $input = Validator::input()->isJson()->assertValid()->getValue();
34        $entity = new \BO\Zmsentities\Process($input);
35        $newProcess = new \BO\Zmsentities\Process($input);
36        $process = $this->readValidProcess($workstation, $entity, $input, $workstation);
37        $newProcess->requests = new RequestList();
38
39        $this->testProcessAccess($workstation, $process);
40
41        \BO\Zmsdb\Connection\Select::getWriteConnection();
42        $processStatusArchived = new \BO\Zmsdb\ProcessStatusArchived();
43
44        $process->status = 'finished';
45        $process = (new Query())->updateEntity(
46            $process,
47            \App::$now,
48            0,
49            'processing',
50            $workstation->getUseraccount()
51        );
52        (new Workstation())->writeRemovedProcess($workstation);
53        $processStatusArchived->writeEntityFinished($process, \App::$now, true);
54
55        $newProcess = (new \BO\Zmsdb\Process())->redirectToScope(
56            $newProcess,
57            $process->scope,
58            $process->queue['number'] ?? $process->id,
59            $workstation->getUseraccount()
60        );
61
62        $message = Response\Message::create($request);
63        $message->data = $newProcess;
64        $response = Render::withLastModified($response, time(), '0');
65
66        return Render::withJson($response, $message->setUpdatedMetaData(), $message->getStatuscode());
67    }
68
69    protected function testProcessData($entity)
70    {
71        $entity->testValid();
72        $authCheck = (new Query())->readAuthKeyByProcessId($entity->id);
73        if (! $authCheck) {
74            throw new Exception\Process\ProcessNotFound();
75        } elseif ($authCheck['authKey'] != $entity->authKey && $authCheck['authName'] != $entity->authKey) {
76            throw new Exception\Process\AuthKeyMatchFailed();
77        }
78    }
79
80    protected function testProcessAccess($workstation, $process)
81    {
82        $cluster = (new \BO\Zmsdb\Cluster())->readByScopeId($workstation->scope['id'], 1);
83        $workstation->testMatchingProcessScope($workstation->getScopeList($cluster), $process);
84        if ($workstation->process && $workstation->process->hasId() && $workstation->process->id != $process->id) {
85            $exception = new Exception\Workstation\WorkstationHasAssignedProcess();
86            $exception->data = [
87                'process' => $workstation->process
88            ];
89            throw $exception;
90        }
91    }
92
93    protected function readValidProcess($workstation, $entity, $input)
94    {
95        if ($entity->hasProcessCredentials()) {
96            $this->testProcessData($entity);
97            $entity->addData($input);
98            $process = (new Query())->updateEntity(
99                $entity,
100                \App::$now,
101                0,
102                null,
103                $workstation->getUseraccount()
104            );
105        } elseif ($entity->hasQueueNumber()) {
106            // Allow waitingnumbers over 1000 with the fourth parameter
107            $process = ProcessStatusQueued::init()
108                ->readByQueueNumberAndScope($entity['queue']['number'], $workstation->scope['id'], 0, 100000000);
109            if (! $process->id) {
110                $workstation = (new \BO\Zmsdb\Workstation())->readResolvedReferences($workstation, 1);
111                $process = (new Query())->writeNewPickup(
112                    $workstation->scope,
113                    \App::$now,
114                    $entity['queue']['number'],
115                    $workstation->getUseraccount()
116                );
117            }
118            $process->testValid();
119        } else {
120            $entity->testValid();
121            throw new Exception\Process\ProcessInvalid();
122        }
123        return $process;
124    }
125}