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
 testScope
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 testAvailabilityList
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 testAccessRights
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 String
23     */
24    public function readResponse(
25        \Psr\Http\Message\RequestInterface $request,
26        \Psr\Http\Message\ResponseInterface $response,
27        array $args
28    ) {
29        $resolveReferences = Validator::param('resolveReferences')->isNumber()->setDefault(2)->getValue();
30        $startDateFormatted = Validator::param('startDate')->isString()->getValue();
31        $endDateFormatted = Validator::param('endDate')->isString()->getValue();
32        $graphQl = Validator::param('gql')->isString()->getValue();
33        $startDate = ($startDateFormatted) ? new \BO\Zmsentities\Helper\DateTime($startDateFormatted) : null;
34        $endDate = ($endDateFormatted) ? new \BO\Zmsentities\Helper\DateTime($endDateFormatted) : null;
35
36        $scope = (new \BO\Zmsdb\Scope())->readEntity($args['id'], $resolveReferences);
37        $this->testScope($scope);
38        $this->testAccessRights($request, $scope);
39
40        $message = Response\Message::create($request);
41        $message->meta->reducedData = $graphQl ? true : false;
42        $message->data = $this->getAvailabilityList($scope, $startDate, $endDate);
43
44        $response = Render::withLastModified($response, time(), '0');
45        $response = Render::withJson($response, $message->setUpdatedMetaData(), 200);
46        return $response;
47    }
48
49    protected function getAvailabilityList($scope, $startDate, $endDate)
50    {
51        $availabilityList = (new Query())->readList($scope->getId(), 0, $startDate, $endDate);
52        $this->testAvailabilityList($availabilityList);
53        return $availabilityList->withScope($scope);
54    }
55
56    protected function testScope($scope)
57    {
58        if (! $scope) {
59            throw new Exception\Scope\ScopeNotFound();
60        }
61    }
62
63    protected function testAvailabilityList($availabilityList)
64    {
65        if (! $availabilityList->count()) {
66            throw new Exception\Availability\AvailabilityNotFound();
67        }
68    }
69
70    protected function testAccessRights($request, $scope)
71    {
72        try {
73            (new Helper\User($request, 2))->checkRights(
74                'availability',
75                new \BO\Zmsentities\Useraccount\EntityAccess($scope)
76            );
77        } catch (\Exception $exception) {
78            $token = $request->getHeader('X-Token');
79            if (\App::SECURE_TOKEN != current($token)) {
80                throw $exception;
81            }
82        }
83    }
84}