Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.99% covered (success)
96.99%
129 / 133
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ReportCapacityIndex
96.99% covered (success)
96.99%
129 / 133
33.33% covered (danger)
33.33%
1 / 3
16
0.00% covered (danger)
0.00%
0 / 1
 readResponse
96.51% covered (success)
96.51%
83 / 86
0.00% covered (danger)
0.00%
0 / 1
11
 handleDownloadRequest
95.24% covered (success)
95.24%
20 / 21
0.00% covered (danger)
0.00%
0 / 1
4
 renderHtmlResponse
100.00% covered (success)
100.00%
26 / 26
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * @package Zmsstatistic
5 * @copyright BerlinOnline Stadtportal GmbH & Co. KG
6 **/
7
8namespace BO\Zmsstatistic;
9
10use BO\Slim\Render;
11use BO\Zmsentities\Exchange;
12use BO\Zmsstatistic\Helper\ReportHelper;
13use BO\Zmsstatistic\Service\ReportCapacityService;
14use Psr\Http\Message\RequestInterface;
15use Psr\Http\Message\ResponseInterface;
16
17class ReportCapacityIndex extends BaseController
18{
19    protected int $resolveLevel = 2;
20
21    /**
22     * @SuppressWarnings(Param)
23     * @return ResponseInterface
24     */
25    #[\Override]
26    public function readResponse(
27        RequestInterface $request,
28        ResponseInterface $response,
29        array $args
30    ): mixed {
31        $this->workstation->getUseraccount()->testPermissions(['statistic', 'capacityreport']);
32
33        /** @var \Psr\Http\Message\ServerRequestInterface $request */
34        $validator = $request->getAttribute('validator');
35        $reportCapacityService = new ReportCapacityService();
36        $reportHelper = new ReportHelper();
37
38        $selectedScopes = $reportHelper->extractSelectedScopes(
39            $validator->getParameter('scopes')->isArray()->getValue() ?? []
40        );
41
42        $workstationScopeId = $reportHelper->getWorkstationScopeId($this->workstation);
43        $scopeId = $reportHelper->resolveScopeIdParam($selectedScopes, $workstationScopeId);
44
45        $capacityPeriod = $workstationScopeId !== null
46            ? $reportCapacityService->getCapacityPeriod((string) $workstationScopeId)
47            : null;
48        $scopeDateBounds = $reportCapacityService->getScopeDateBoundsByScopeId();
49
50        $dateRange = $reportHelper->extractDateRange(
51            $validator->getParameter('from')->isString()->getValue(),
52            $validator->getParameter('to')->isString()->getValue()
53        );
54
55        $fetchedCapacity = $reportCapacityService->getExchangeCapacityData($scopeId, $dateRange, $args);
56        $exchangeCapacityDaily = null;
57        $exchangeCapacityChartDaily = null;
58        $exchangeCapacityChartSparseDaily = null;
59        $exchangeCapacityHourly = null;
60        $exchangeCapacityChartHourly = null;
61        $exchangeCapacityChartSparseHourly = null;
62        $displayExchanges = null;
63
64        if ($fetchedCapacity instanceof Exchange) {
65            $period = $args['period'] ?? null;
66            $displayExchanges = $reportCapacityService->buildCapacityDisplayExchanges(
67                $fetchedCapacity,
68                $dateRange,
69                $period
70            );
71            $exchangeCapacityDaily = $displayExchanges['dailyTable'];
72            $exchangeCapacityChartSparseDaily = $displayExchanges['dailyChartSparse'];
73            $exchangeCapacityChartDaily = $displayExchanges['dailyChartFull'];
74            $exchangeCapacityHourly = $displayExchanges['hourlyTable'];
75            $exchangeCapacityChartSparseHourly = $displayExchanges['hourlyChartSparse'];
76            $exchangeCapacityChartHourly = $displayExchanges['hourlyChartFull'];
77        }
78
79        $type = $validator->getParameter('type')->isString()->getValue();
80        if ($type) {
81            $granularity = $validator->getParameter('granularity')->isString()->getValue();
82            $timeline = $validator->getParameter('timeline')->isString()->getValue();
83            $downloadExchange = is_array($displayExchanges)
84                ? $reportCapacityService->selectDownloadExchange(
85                    $displayExchanges,
86                    $granularity,
87                    $timeline
88                )
89                : null;
90
91            return $this->handleDownloadRequest(
92                $request,
93                $response,
94                $args,
95                $scopeId,
96                $downloadExchange,
97                $dateRange,
98                $selectedScopes,
99                $reportCapacityService
100            );
101        }
102
103        $displayScopeIds = $selectedScopes;
104        if ($displayScopeIds === [] && $workstationScopeId !== null) {
105            $displayScopeIds = [(string) $workstationScopeId];
106        }
107        $scopeSlotTimes = [];
108        $visualization = $fetchedCapacity instanceof Exchange
109            ? ($fetchedCapacity['visualization'] ?? null)
110            : null;
111        if (is_array($visualization) && is_array($visualization['scopeSlotTimes'] ?? null)) {
112            $scopeSlotTimes = $visualization['scopeSlotTimes'];
113        }
114        if ($scopeSlotTimes === []) {
115            $scopeSlotTimes = $reportCapacityService->getSelectedScopeSlotTimes($displayScopeIds);
116        }
117        $scopeSlotTimeHint = $reportCapacityService->formatScopeSlotTimeHint($scopeSlotTimes);
118
119        return $this->renderHtmlResponse(
120            $response,
121            $args,
122            $capacityPeriod,
123            $dateRange,
124            $exchangeCapacityDaily,
125            $exchangeCapacityChartDaily,
126            $exchangeCapacityChartSparseDaily,
127            $selectedScopes,
128            $scopeDateBounds,
129            $scopeSlotTimeHint,
130            $exchangeCapacityHourly,
131            $exchangeCapacityChartHourly,
132            $exchangeCapacityChartSparseHourly
133        );
134    }
135
136    private function handleDownloadRequest(
137        RequestInterface $request,
138        ResponseInterface $response,
139        array $args,
140        string $scopeId,
141        mixed $downloadExchange,
142        ?array $dateRange,
143        array $selectedScopes = [],
144        ?ReportCapacityService $reportCapacityService = null
145    ): ResponseInterface {
146        if ($reportCapacityService === null) {
147            $reportCapacityService = new ReportCapacityService();
148        }
149
150        /** @var \Psr\Http\Message\ServerRequestInterface $request */
151        $validator = $request->getAttribute('validator');
152        $valueMode = $validator->getParameter('valueMode')->isString()->getValue();
153        $valueMode = $valueMode === 'minutes' ? 'minutes' : 'slots';
154        $channelMode = $validator->getParameter('channelMode')->isString()->getValue();
155        $channelMode = in_array($channelMode, ['total', 'public', 'intern_only'], true)
156            ? $channelMode
157            : 'total';
158
159        $args = $reportCapacityService->prepareDownloadArgs(
160            $args,
161            $scopeId,
162            $downloadExchange,
163            $dateRange,
164            $selectedScopes,
165            $valueMode,
166            $channelMode
167        );
168
169        /** @var mixed $container */
170        $container = \App::$slim->getContainer();
171        return (new Download\CapacityReport($container))
172            ->readResponse($request, $response, $args);
173    }
174
175    private function renderHtmlResponse(
176        ResponseInterface $response,
177        array $args,
178        mixed $capacityPeriod,
179        array|null $dateRange,
180        mixed $exchangeCapacityDaily,
181        Exchange|null $exchangeCapacityChartDaily,
182        Exchange|null $exchangeCapacityChartSparseDaily,
183        array $selectedScopes = [],
184        array $scopeDateBounds = [],
185        ?string $scopeSlotTimeHint = null,
186        Exchange|null $exchangeCapacityHourly = null,
187        Exchange|null $exchangeCapacityChartHourly = null,
188        Exchange|null $exchangeCapacityChartSparseHourly = null
189    ): ResponseInterface {
190        return Render::withHtml(
191            $response,
192            'page/reportCapacityIndex.twig',
193            [
194                'title' => 'Terminkapazität Standort',
195                'activeScope' => 'active',
196                'menuActive' => 'capacity',
197                'department' => $this->department,
198                'organisation' => $this->organisation,
199                'capacityPeriod' => $capacityPeriod,
200                'scopeDateBounds' => $scopeDateBounds,
201                'showAll' => 1,
202                'period' => $args['period'] ?? null,
203                'dateRange' => $dateRange,
204                'exchangeCapacityDaily' => $exchangeCapacityDaily,
205                'exchangeCapacityChartDaily' => $exchangeCapacityChartDaily,
206                'exchangeCapacityChartSparseDaily' => $exchangeCapacityChartSparseDaily,
207                'exchangeCapacityHourly' => $exchangeCapacityHourly,
208                'exchangeCapacityChartHourly' => $exchangeCapacityChartHourly,
209                'exchangeCapacityChartSparseHourly' => $exchangeCapacityChartSparseHourly,
210                'source' => ['entity' => 'CapacityIndex'],
211                'selectedScopeIds' => $selectedScopes,
212                'scopeSlotTimeHint' => $scopeSlotTimeHint,
213                'workstation' => $this->workstation->getArrayCopy(),
214            ]
215        );
216    }
217}