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