Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
30 / 30
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
AvailabilityListByScope
100.00% covered (success)
100.00%
30 / 30
100.00% covered (success)
100.00%
5 / 5
12
100.00% covered (success)
100.00%
1 / 1
 readResponse
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
4
 getAvailabilityList
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 validateScope
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 validateAvailabilityList
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 validateAccessRights
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
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\Availability as Query;
13use BO\Zmsentities\Collection\AvailabilityList as Collection;
14
15/**
16 * @SuppressWarnings(Coupling)
17 */
18class AvailabilityListByScope extends BaseController
19{
20    /**
21     * @SuppressWarnings(Param)
22     * @return \Psr\Http\Message\ResponseInterface
23     */
24    #[\Override]
25    public function readResponse(
26        \Psr\Http\Message\RequestInterface $request,
27        \Psr\Http\Message\ResponseInterface $response,
28        array $args
29    ) {
30        $resolveReferences = Validator::param('resolveReferences')->isNumber()->setDefault(2)->getValue();
31        $startDateFormatted = Validator::param('startDate')->isString()->getValue();
32        $endDateFormatted = Validator::param('endDate')->isString()->getValue();
33        $graphQl = Validator::param('gql')->isString()->getValue();
34        $startDate = ($startDateFormatted) ? new \BO\Zmsentities\Helper\DateTime($startDateFormatted) : null;
35        $endDate = ($endDateFormatted) ? new \BO\Zmsentities\Helper\DateTime($endDateFormatted) : null;
36
37        $scope = (new \BO\Zmsdb\Scope())->readEntity($args['id'], $resolveReferences);
38        $this->validateScope($scope);
39        $this->validateAccessRights($request, $scope);
40
41        $message = Response\Message::create($request);
42        $message->meta->reducedData = $graphQl ? true : false;
43        $message->data = $this->getAvailabilityList($scope, $startDate, $endDate);
44
45        $response = Render::withLastModified($response, time(), '0');
46        $response = Render::withJson($response, $message->setUpdatedMetaData(), 200);
47        return $response;
48    }
49
50    protected function getAvailabilityList($scope, $startDate, $endDate)
51    {
52        $availabilityList = (new Query())->readList($scope->getId(), 0, $startDate, $endDate);
53        $this->validateAvailabilityList($availabilityList);
54        return $availabilityList->withScope($scope);
55    }
56
57    protected function validateScope($scope)
58    {
59        if (! $scope) {
60            throw new Exception\Scope\ScopeNotFound();
61        }
62    }
63
64    protected function validateAvailabilityList($availabilityList)
65    {
66        if (! $availabilityList->count()) {
67            throw new Exception\Availability\AvailabilityNotFound();
68        }
69    }
70
71    protected function validateAccessRights($request, $scope)
72    {
73        try {
74            (new Helper\User($request, 2))->checkRights(
75                'availability',
76                new \BO\Zmsentities\Useraccount\EntityAccess($scope)
77            );
78        } catch (\Exception $exception) {
79            $token = $request->getHeader('X-Token');
80            if (\App::SECURE_TOKEN != current($token)) {
81                throw $exception;
82            }
83        }
84    }
85}