Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
51 / 51
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
ProcessPickup
100.00% covered (success)
100.00%
51 / 51
100.00% covered (success)
100.00%
4 / 4
13
100.00% covered (success)
100.00%
1 / 1
 readResponse
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
1
 testProcessData
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
 testProcessAccess
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
4
 readValidProcess
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
1 / 1
4
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 as Query;
13use BO\Zmsdb\ProcessStatusQueued;
14
15/**
16 * @SuppressWarnings(Coupling)
17 */
18class ProcessPickup extends BaseController
19{
20    /**
21     * @SuppressWarnings(Param)
22     * @return String
23     */
24    public function readResponse(
25        \Psr\Http\Message\RequestInterface $request,
26        \Psr\Http\Message\ResponseInterface $response,
27        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        \BO\Zmsdb\Connection\Select::getWriteConnection();
33
34        $process = $this->readValidProcess($workstation, $entity, $input);
35        $this->testProcessAccess($workstation, $process);
36        (new \BO\Zmsdb\Workstation())->writeAssignedProcess($workstation, $process, \App::$now);
37
38        $message = Response\Message::create($request);
39        $message->data = (new Query())->readEntity($process->id, $process->authKey);
40
41        $response = Render::withLastModified($response, time(), '0');
42        $response = Render::withJson($response, $message->setUpdatedMetaData(), $message->getStatuscode());
43        return $response;
44    }
45
46    protected function testProcessData($entity)
47    {
48        $entity->testValid();
49        $authCheck = (new Query())->readAuthKeyByProcessId($entity->id);
50        if (! $authCheck) {
51            throw new Exception\Process\ProcessNotFound();
52        } elseif ($authCheck['authKey'] != $entity->authKey && $authCheck['authName'] != $entity->authKey) {
53            throw new Exception\Process\AuthKeyMatchFailed();
54        }
55    }
56
57    protected function testProcessAccess($workstation, $process)
58    {
59        $cluster = (new \BO\Zmsdb\Cluster())->readByScopeId($workstation->scope['id'], 1);
60        $workstation->testMatchingProcessScope($workstation->getScopeList($cluster), $process);
61        if ($workstation->process && $workstation->process->hasId() && $workstation->process->id != $process->id) {
62            $exception = new Exception\Workstation\WorkstationHasAssignedProcess();
63            $exception->data = [
64                'process' => $workstation->process
65            ];
66            throw $exception;
67        }
68    }
69
70    protected function readValidProcess($workstation, $entity, $input)
71    {
72        if ($entity->hasProcessCredentials()) {
73            $this->testProcessData($entity);
74            $entity->addData($input);
75            $process = (new Query())->updateEntity(
76                $entity,
77                \App::$now,
78                0,
79                null,
80                $workstation->getUseraccount()
81            );
82        } elseif ($entity->hasQueueNumber()) {
83            // Allow waitingnumbers over 1000 with the fourth parameter
84            $process = ProcessStatusQueued::init()
85                ->readByQueueNumberAndScope($entity['queue']['number'], $workstation->scope['id'], 0, 100000000);
86            if (! $process->id) {
87                $workstation = (new \BO\Zmsdb\Workstation())->readResolvedReferences($workstation, 1);
88                $process = (new Query())->writeNewPickup(
89                    $workstation->scope,
90                    \App::$now,
91                    $entity['queue']['number'],
92                    $workstation->getUseraccount()
93                );
94            }
95            $process->testValid();
96        } else {
97            $entity->testValid();
98            throw new Exception\Process\ProcessInvalid();
99        }
100        return $process;
101    }
102}