Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
84.38% covered (warning)
84.38%
54 / 64
50.00% covered (danger)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
WorkstationProcess
84.38% covered (warning)
84.38%
54 / 64
50.00% covered (danger)
50.00%
2 / 4
21.53
0.00% covered (danger)
0.00%
0 / 1
 readResponse
100.00% covered (success)
100.00%
33 / 33
100.00% covered (success)
100.00%
1 / 1
4
 validateProcessCurrentDate
50.00% covered (danger)
50.00%
9 / 18
0.00% covered (danger)
0.00%
0 / 1
13.12
 testProcessData
83.33% covered (warning)
83.33%
5 / 6
0.00% covered (danger)
0.00%
0 / 1
4.07
 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\Workstation;
13use BO\Zmsdb\Process as Query;
14
15/**
16 * @SuppressWarnings(Coupling)
17 */
18class WorkstationProcess 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        \BO\Zmsdb\Connection\Select::getWriteConnection();
30        $workstation = (new Helper\User($request, 1))->checkRights();
31        $input = Validator::input()->isJson()->assertValid()->getValue();
32        $allowClusterWideCall = Validator::param('allowClusterWideCall')->isBool()->setDefault(true)->getValue();
33        if ($workstation->process && $workstation->process->hasId() && $workstation->process->getId() != $input['id']) {
34            $exception = new Exception\Workstation\WorkstationHasAssignedProcess();
35            $exception->data = ['process' => $workstation->process];
36            throw $exception;
37        }
38
39        $entity = new \BO\Zmsentities\Process($input);
40        $entity->testValid();
41        $this->testProcessData($entity);
42        $process = (new Query())->readEntity($entity['id'], $entity['authKey'], 1);
43
44        $this->validateProcessCurrentDate($process);
45
46        $previousStatus = $process->status;
47        $process->status = 'called';
48        $process = (new Query())->updateEntity(
49            $process,
50            \App::$now,
51            0,
52            $previousStatus,
53            $workstation->getUseraccount()
54        );
55
56        $process = new \BO\Zmsentities\Process($input);
57        $this->testProcess($process, $workstation, $allowClusterWideCall);
58        $process->setCallTime(\App::$now);
59        $process->queue['callCount']++;
60
61        $process->status = 'called';
62
63        $workstation->process = (new Workstation())->writeAssignedProcess($workstation, $process, \App::$now);
64
65        $message = Response\Message::create($request);
66        $message->data = $workstation;
67
68        $response = Render::withLastModified($response, time(), '0');
69        $response = Render::withJson($response, $message->setUpdatedMetaData(), $message->getStatuscode());
70        return $response;
71    }
72
73    protected function validateProcessCurrentDate($process)
74    {
75        if (!$process || !$process->hasId() || !$process->isWithAppointment()) {
76            return;
77        }
78
79        $appointment = $process->getFirstAppointment();
80        if (!$appointment || !$appointment->date) {
81            return;
82        }
83
84        $now = \App::getNow();
85        $today = $now->setTime(0, 0, 0);
86        $appointmentDateTime = new \DateTimeImmutable();
87        $appointmentDateTime = $appointmentDateTime->setTimestamp($appointment->date);
88        $appointmentDate = $appointmentDateTime->setTime(0, 0, 0);
89
90        if ($appointmentDate != $today) {
91            $exception = new Exception\Process\ProcessNotCurrentDate();
92            $exception->data = [
93                'processId' => $process->getId(),
94                'appointmentDate' => $appointmentDateTime->format('d.m.Y'),
95                'appointmentTime' => $appointmentDateTime->format('H:i') . ' Uhr'
96            ];
97            throw $exception;
98        }
99    }
100
101    protected function testProcessData($entity)
102    {
103        $authCheck = (new Query())->readAuthKeyByProcessId($entity->id);
104        if (! $authCheck) {
105            throw new Exception\Process\ProcessNotFound();
106        } elseif ($authCheck['authKey'] != $entity->authKey && $authCheck['authName'] != $entity->authKey) {
107            throw new Exception\Process\AuthKeyMatchFailed();
108        }
109        Helper\Matching::testCurrentScopeHasRequest($entity);
110    }
111
112    protected function testProcess($process, $workstation, $allowClusterWideCall)
113    {
114        if ('called' == $process->status || 'processing' == $process->status) {
115            throw new Exception\Process\ProcessAlreadyCalled();
116        }
117        if ('reserved' == $process->getStatus()) {
118            throw new Exception\Process\ProcessReservedNotCallable();
119        }
120        if (! $allowClusterWideCall) {
121            $workstation->validateProcessScopeAccess($workstation->getScopeList(), $process);
122        }
123        $process->testValid();
124    }
125}