Lines
100.00%
24 / 24
Functions and Methods
100.00%
1 / 1
Classes and Traits
100.00%
1 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| AvailabilityClosureRead | 100.00% | 24 / 24 | 100.00% | 1 / 1 | 3 | 100.00% | 1 / 1 | |||
| readResponse | 100.00% | 24 / 24 | 100.00% | 1 / 1 | 3 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | namespace BO\Zmsbackend\Availability\Api; | |
| 4 | ||
| 5 | use BO\Slim\Render; | |
| 6 | use BO\Mellon\Validator; | |
| 7 | use BO\Zmsbackend\Availability\Service\Closure as ClosureQuery; | |
| 8 | use DateTimeImmutable; | |
| 9 | ||
| 10 | class AvailabilityClosureRead extends \BO\Zmsbackend\Api\BaseController | |
| 11 | { | |
| 12 | /** | |
| 13 | * @return \Psr\Http\Message\ResponseInterface | |
| 14 | */ | |
| 15 | #[\Override] | |
| 16 | public function readResponse( | |
| 17 | \Psr\Http\Message\RequestInterface $request, | |
| 18 | \Psr\Http\Message\ResponseInterface $response, | |
| 19 | array $args | |
| 20 | ) { | |
| 21 | (new \BO\Zmsbackend\Helper\User($request))->checkAnyPermission('availability', 'overviewcalendar'); | |
| 22 | ||
| 23 | try { | |
| 24 | $scopeIdCsv = Validator::param('scopeIds') | |
| 25 | ->isString()->isMatchOf('/^\d+(,\d+)*$/')->assertValid()->getValue(); | |
| 26 | $scopeIds = array_values(array_unique(array_map('intval', explode(',', $scopeIdCsv)))); | |
| 27 | ||
| 28 | $dateFrom = Validator::param('dateFrom')->isDate('Y-m-d')->assertValid()->getValue(); | |
| 29 | $dateUntil = Validator::param('dateUntil')->isDate('Y-m-d')->assertValid()->getValue(); | |
| 30 | } catch (\BO\Mellon\Failure\Exception $e) { | |
| 31 | $payload = ['error' => true, 'message' => $e->getMessage()]; | |
| 32 | return Render::withJson($response->withStatus(400), $payload, 400); | |
| 33 | } | |
| 34 | ||
| 35 | $dtFrom = new DateTimeImmutable($dateFrom); | |
| 36 | $dtUntil = new DateTimeImmutable($dateUntil); | |
| 37 | if ($dtFrom > $dtUntil) { | |
| 38 | $payload = ['error' => true, 'message' => 'dateFrom must be before or equal to dateUntil']; | |
| 39 | return Render::withJson($response->withStatus(400), $payload, 400); | |
| 40 | } | |
| 41 | ||
| 42 | $items = (new ClosureQuery())->readByScopesInRange( | |
| 43 | $scopeIds, | |
| 44 | $dtFrom, | |
| 45 | $dtUntil | |
| 46 | ); | |
| 47 | ||
| 48 | $msg = \BO\Zmsbackend\Api\Response\Message::create($request); | |
| 49 | $msg->data = ['items' => $items]; | |
| 50 | ||
| 51 | $lastModified = (new DateTimeImmutable())->getTimestamp(); | |
| 52 | $response = Render::withLastModified($response, $lastModified, '0'); | |
| 53 | ||
| 54 | return Render::withJson($response, $msg->setUpdatedMetaData(), $msg->getStatuscode()); | |
| 55 | } | |
| 56 | } |