Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 52
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 / 52
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 / 24
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 / 14
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(\Psr\Http\Message\RequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
28    {
29        $workstation = (new Helper\User($request))->checkRights();
30        $input = Validator::input()->isJson()->assertValid()->getValue();
31        $entity = new \BO\Zmsentities\Process($input);
32        $newProcess = new \BO\Zmsentities\Process($input);
33        $process = $this->readValidProcess($workstation, $entity, $input, $workstation);
34        $newProcess->requests = new RequestList();
35        $this->testProcessAccess($workstation, $process);
36        \BO\Zmsdb\Connection\Select::getWriteConnection();
37        $processStatusArchived = new \BO\Zmsdb\ProcessStatusArchived();
38        $process->status = 'finished';
39        $process = (new Query())->updateEntity($process, \App::$now, 0, 'processing', $workstation->getUseraccount());
40        (new Workstation())->writeRemovedProcess($workstation);
41        $processStatusArchived->writeEntityFinished($process, \App::$now, false);
42        $newProcess->displayNumber = $process->displayNumber;
43        $newProcess = (new \BO\Zmsdb\Process())->redirectToScope($newProcess, $process->scope, $process->queue['number'] ?? $process->id, $workstation->getUseraccount());
44        \App::$log->info('Process redirected', [
45            'process_redirected' => true,
46            'process_id' => $newProcess->id,
47            'scope_id' => $newProcess->scope['id']
48        ]);
49        $message = Response\Message::create($request);
50        $message->data = $newProcess;
51        $response = Render::withLastModified($response, time(), '0');
52        return Render::withJson($response, $message->setUpdatedMetaData(), $message->getStatuscode());
53    }
54
55    protected function testProcessData($entity)
56    {
57        $entity->testValid();
58        $authCheck = (new Query())->readAuthKeyByProcessId($entity->id);
59        if (! $authCheck) {
60            throw new Exception\Process\ProcessNotFound();
61        } elseif ($authCheck['authKey'] != $entity->authKey && $authCheck['authName'] != $entity->authKey) {
62            throw new Exception\Process\AuthKeyMatchFailed();
63        }
64    }
65
66    protected function testProcessAccess($workstation, $process)
67    {
68        $cluster = (new \BO\Zmsdb\Cluster())->readByScopeId($workstation->scope['id'], 1);
69        $workstation->validateProcessScopeAccess($workstation->getScopeList($cluster), $process);
70        if ($workstation->process && $workstation->process->hasId() && $workstation->process->id != $process->id) {
71            $exception = new Exception\Workstation\WorkstationHasAssignedProcess();
72            $exception->data = [
73                'process' => $workstation->process
74            ];
75            throw $exception;
76        }
77    }
78
79    protected function readValidProcess($workstation, $entity, $input)
80    {
81        if ($entity->hasProcessCredentials()) {
82            $this->testProcessData($entity);
83            $entity->addData($input);
84            $process = (new Query())->updateEntity($entity, \App::$now, 0, null, $workstation->getUseraccount());
85        } elseif ($entity->hasQueueNumber()) {
86        // Allow waitingnumbers over 1000 with the fourth parameter
87            $process = ProcessStatusQueued::init()
88                ->readByQueueNumberAndScope($entity['queue']['number'], $workstation->scope['id'], 0, 100000000);
89            if (! $process->id) {
90                $workstation = (new \BO\Zmsdb\Workstation())->readResolvedReferences($workstation, 1);
91                $process = (new Query())->writeNewPickup($workstation->scope, \App::$now, $entity['queue']['number'], $workstation->getUseraccount());
92            }
93            $process->testValid();
94        } else {
95            $entity->testValid();
96            throw new Exception\Process\ProcessInvalid();
97        }
98        return $process;
99    }
100}