Lines
100.00%
33 / 33
Methods
100.00%
6 / 6
Classes
100.00%
1 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| readResponse | 100.00% | 15 / 15 | 100.00% | 1 / 1 | 4 | ||
| getAvailabilityList | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 1 | ||
| validateScope | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 2 | ||
| validateAvailabilityList | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 2 | ||
| validateAccessRights | 100.00% | 8 / 8 | 100.00% | 1 / 1 | 3 | ||
| [BO\Zmsbackend\Api\BaseController] __invoke | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 1 | ||
| 18 | class 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 | } |
Inherited from BO\Zmsbackend\Api\BaseController
| 20 | public function __invoke(RequestInterface $request, ResponseInterface $response, array $args) | |
| 21 | { | |
| 22 | $request = $this->initRequest($request); | |
| 23 | $noCacheResponse = \BO\Slim\Render::withLastModified($response, time(), '0'); | |
| 24 | return $this->readResponse($request, $noCacheResponse, $args); | |
| 25 | } |