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