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