Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.56% covered (success)
95.56%
43 / 45
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
WorkstationProcess
95.56% covered (success)
95.56%
43 / 45
66.67% covered (warning)
66.67%
2 / 3
13
0.00% covered (danger)
0.00%
0 / 1
 readResponse
100.00% covered (success)
100.00%
32 / 32
100.00% covered (success)
100.00%
1 / 1
4
 testProcessData
66.67% covered (warning)
66.67%
4 / 6
0.00% covered (danger)
0.00%
0 / 1
4.59
 testProcess
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
5
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\Log;
13use BO\Zmsdb\Workstation;
14use BO\Zmsdb\Process;
15use BO\Zmsdb\Process as Query;
16
17/**
18 * @SuppressWarnings(Coupling)
19 */
20class WorkstationProcess extends BaseController
21{
22    /**
23     * @SuppressWarnings(Param)
24     * @return String
25     */
26    public function readResponse(
27        \Psr\Http\Message\RequestInterface $request,
28        \Psr\Http\Message\ResponseInterface $response,
29        array $args
30    ) {
31        \BO\Zmsdb\Connection\Select::getWriteConnection();
32        $workstation = (new Helper\User($request, 1))->checkRights();
33        $input = Validator::input()->isJson()->assertValid()->getValue();
34        $allowClusterWideCall = Validator::param('allowClusterWideCall')->isBool()->setDefault(true)->getValue();
35        if ($workstation->process && $workstation->process->hasId() && $workstation->process->getId() != $input['id']) {
36            $exception = new Exception\Workstation\WorkstationHasAssignedProcess();
37            $exception->data = ['process' => $workstation->process];
38            throw $exception;
39        }
40
41        $entity = new \BO\Zmsentities\Process($input);
42        $entity->testValid();
43        $this->testProcessData($entity);
44        $process = (new Query())->readEntity($entity['id'], $entity['authKey'], 1);
45        $previousStatus = $process->status;
46        $process->status = 'called';
47        $process = (new Query())->updateEntity(
48            $process,
49            \App::$now,
50            0,
51            $previousStatus,
52            $workstation->getUseraccount()
53        );
54
55        $process = new \BO\Zmsentities\Process($input);
56        $this->testProcess($process, $workstation, $allowClusterWideCall);
57        $process->setCallTime(\App::$now);
58        $process->queue['callCount']++;
59
60        $process->status = 'called';
61
62        $workstation->process = (new Workstation())->writeAssignedProcess($workstation, $process, \App::$now);
63
64        $message = Response\Message::create($request);
65        $message->data = $workstation;
66
67        $response = Render::withLastModified($response, time(), '0');
68        $response = Render::withJson($response, $message->setUpdatedMetaData(), $message->getStatuscode());
69        return $response;
70    }
71
72    protected function testProcessData($entity)
73    {
74        $authCheck = (new Query())->readAuthKeyByProcessId($entity->id);
75        if (! $authCheck) {
76            throw new Exception\Process\ProcessNotFound();
77        } elseif ($authCheck['authKey'] != $entity->authKey && $authCheck['authName'] != $entity->authKey) {
78            throw new Exception\Process\AuthKeyMatchFailed();
79        }
80        Helper\Matching::testCurrentScopeHasRequest($entity);
81    }
82
83    protected function testProcess($process, $workstation, $allowClusterWideCall)
84    {
85        if ('called' == $process->status || 'processing' == $process->status) {
86            throw new Exception\Process\ProcessAlreadyCalled();
87        }
88        if ('reserved' == $process->getStatus()) {
89            throw new Exception\Process\ProcessReservedNotCallable();
90        }
91        if (! $allowClusterWideCall) {
92            $workstation->testMatchingProcessScope($workstation->getScopeList(), $process);
93        }
94        $process->testValid();
95    }
96}