Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
33 / 33
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
ProcessReserve
100.00% covered (success)
100.00%
33 / 33
100.00% covered (success)
100.00%
1 / 1
10
100.00% covered (success)
100.00%
1 / 1
 readResponse
100.00% covered (success)
100.00%
33 / 33
100.00% covered (success)
100.00%
1 / 1
10
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\ProcessStatusFree;
14
15/**
16 * @SuppressWarnings(Coupling)
17 */
18class ProcessReserve extends BaseController
19{
20    /**
21     * @SuppressWarnings(Param)
22     * @SuppressWarnings(Complexity)
23     * @return \Psr\Http\Message\ResponseInterface
24     */
25    #[\Override]
26    public function readResponse(
27        \Psr\Http\Message\RequestInterface $request,
28        \Psr\Http\Message\ResponseInterface $response,
29        array $args
30    ) {
31        $slotsRequired = Validator::param('slotsRequired')->isNumber()->getValue();
32        $slotType = Validator::param('slotType')->isString()->getValue();
33        $clientKey = Validator::param('clientkey')->isString()->getValue();
34        $resolveReferences = Validator::param('resolveReferences')->isNumber()->setDefault(0)->getValue();
35        $input = Validator::input()->isJson()->assertValid()->getValue();
36        $process = new \BO\Zmsentities\Process($input);
37        if ($process->hasId()) {
38            throw new Exception\Process\ProcessAlreadyExists();
39        }
40
41        \BO\Zmsdb\Connection\Select::setCriticalReadSession();
42
43        if ($slotType || $slotsRequired) {
44            $workstation = (new Helper\User($request))->checkPermissions();
45            Helper\Matching::testCurrentScopeHasRequest($process);
46        } elseif ($clientKey) {
47            $apiClient = (new \BO\Zmsdb\Apiclient())->readEntity($clientKey);
48            if (!$apiClient || !isset($apiClient->accesslevel) || $apiClient->accesslevel == 'blocked') {
49                throw new Exception\Process\ApiclientInvalid();
50            }
51            $slotType = $apiClient->accesslevel;
52            if ($apiClient->accesslevel != 'intern') {
53                $slotsRequired = 0;
54                $slotType = $apiClient->accesslevel;
55                $process = (new Process())->readSlotCount($process);
56            }
57            $process->apiclient = $apiClient;
58        } else {
59            $slotsRequired = 0;
60            $slotType = 'public';
61            $process = (new Process())->readSlotCount($process);
62        }
63
64        $userAccount = (isset($workstation)) ? $workstation->getUseraccount() : null;
65        $process = (new ProcessStatusFree())
66            ->writeEntityReserved($process, \App::$now, $slotType, $slotsRequired, $resolveReferences, $userAccount);
67
68        $message = Response\Message::create($request);
69        $message->data = $process;
70
71        $response = Render::withLastModified($response, time(), '0');
72        $response = Render::withJson($response, $message->setUpdatedMetaData(), $message->getStatuscode());
73        return $response;
74    }
75}