Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
WarehousePeriodGet
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
1 / 1
5
100.00% covered (success)
100.00%
1 / 1
 readResponse
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
1 / 1
5
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;
12
13class WarehousePeriodGet extends BaseController
14{
15    /**
16     * @SuppressWarnings(Param)
17     * @return \Psr\Http\Message\ResponseInterface
18     */
19    #[\Override]
20    public function readResponse(
21        \Psr\Http\Message\RequestInterface $request,
22        \Psr\Http\Message\ResponseInterface $response,
23        array $args
24    ) {
25        $workstation = (new Helper\User($request, 2))->checkPermissions('statistic');
26        $subject = Validator::value($args['subject'])->isString()->getValue();
27        $subjectId = Validator::value($args['subjectId'])->isString()->getValue();
28        $period = Validator::value($args['period'])->isString()->isBiggerThan(2)->setDefault('_')->getValue();
29        $validator = $request->getAttribute('validator');
30        $groupby = $validator->getParameter('groupby')->isString()->isBiggerThan(2)->getValue();
31        $fromDate = $validator->getParameter('fromDate')->isString()->getValue();
32        $toDate = $validator->getParameter('toDate')->isString()->getValue();
33
34        $exchangeClass = '\BO\Zmsdb\Exchange' . ucfirst($subject ?? '');
35        if (! class_exists($exchangeClass)) {
36            throw new Exception\Warehouse\UnknownReportType();
37        }
38
39        $periodHelper = new Helper\ExchangePeriod($period);
40        $start = $periodHelper->getStartDateTime();
41        $end = $periodHelper->getEndDateTime();
42
43        $subjectPeriod = (new $exchangeClass())->readEntity(
44            $subjectId,
45            $fromDate ? new \DateTime($fromDate) : $start,
46            $toDate ? new \DateTime($toDate) : $end,
47            $periodHelper->getPeriodIdentifier($groupby)
48        );
49        if (0 == count($subjectPeriod['data'])) {
50            throw new Exception\Warehouse\ReportNotFound();
51        }
52
53        $message = Response\Message::create($request);
54        $message->data = (new Helper\ExchangeAccessFilter($subjectPeriod, $workstation))->getFilteredEntity();
55
56        $response = Render::withLastModified($response, time(), '0');
57        $response = Render::withJson($response, $message, $message->getStatuscode());
58        return $response;
59    }
60}