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\Zmsbackend\Availability\Api;
9
10use BO\Slim\Render;
11use BO\Mellon\Validator;
12use BO\Zmsbackend\Availability\Service\Availability as Query;
13use BO\Zmsentities\Collection\AvailabilityList as Collection;
14
15/**
16 * @SuppressWarnings(Coupling)
17 */
18class AvailabilityListByScope extends \BO\Zmsbackend\Api\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\Zmsbackend\Scope\Service\Scope())->readEntity($args['id'], $resolveReferences);
38        $this->validateScope($scope);
39        $this->validateAccessRights($request, $scope);
40
41        $message = \BO\Zmsbackend\Api\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(\BO\Zmsentities\Scope|null $scope, \BO\Zmsentities\Helper\DateTime|null $startDate, \BO\Zmsentities\Helper\DateTime|null $endDate)
51    {
52        $availabilityList = (new Query())->readList($scope->getId(), 0, $startDate, $endDate);
53        $this->validateAvailabilityList($availabilityList);
54        return $availabilityList->withScope($scope);
55    }
56
57    /**
58     * @return void
59     */
60    protected function validateScope(\BO\Zmsentities\Scope|null $scope)
61    {
62        if (! $scope) {
63            throw new \BO\Zmsbackend\Scope\Exception\ScopeNotFound();
64        }
65    }
66
67    /**
68     * @return void
69     */
70    protected function validateAvailabilityList($availabilityList)
71    {
72        if (! $availabilityList->count()) {
73            throw new \BO\Zmsbackend\Availability\Exception\AvailabilityNotFound();
74        }
75    }
76
77    /**
78     * @return void
79     */
80    protected function validateAccessRights(\Psr\Http\Message\RequestInterface $request, \BO\Zmsentities\Scope|null $scope)
81    {
82        try {
83            (new \BO\Zmsbackend\Helper\User($request, 2))->checkPermissions(
84                'availability',
85                new \BO\Zmsentities\Useraccount\EntityAccess($scope)
86            );
87        } catch (\Exception $exception) {
88            $token = $request->getHeader('X-Token');
89            if (\App::SECURE_TOKEN != current($token)) {
90                throw $exception;
91            }
92        }
93    }
94}