Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.77% covered (success)
96.77%
60 / 62
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ReportRequestIndex
96.77% covered (success)
96.77%
60 / 62
33.33% covered (danger)
33.33%
1 / 3
7
0.00% covered (danger)
0.00%
0 / 1
 readResponse
97.14% covered (success)
97.14%
34 / 35
0.00% covered (danger)
0.00%
0 / 1
3
 handleDownloadRequest
87.50% covered (warning)
87.50%
7 / 8
0.00% covered (danger)
0.00%
0 / 1
2.01
 renderHtmlResponse
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3/**
4 * @package Zmsadmin
5 * @copyright BerlinOnline Stadtportal GmbH & Co. KG
6 **/
7
8namespace BO\Zmsstatistic;
9
10use BO\Slim\Render;
11use BO\Zmsstatistic\Helper\ReportHelper;
12use BO\Zmsstatistic\Service\ReportRequestService;
13use Psr\Http\Message\RequestInterface;
14use Psr\Http\Message\ResponseInterface;
15
16class ReportRequestIndex extends BaseController
17{
18    protected int $resolveLevel = 2;
19
20    /**
21     * @SuppressWarnings(Param)
22     * @return ResponseInterface
23     */
24    #[\Override]
25    public function readResponse(
26        RequestInterface $request,
27        ResponseInterface $response,
28        array $args
29    ): mixed {
30        /** @var \Psr\Http\Message\ServerRequestInterface $request */
31        $validator = $request->getAttribute('validator');
32        $reportRequestService = new ReportRequestService();
33        $reportHelper = new ReportHelper();
34
35        $selectedScopes = $reportHelper->extractSelectedScopes(
36            $validator->getParameter('scopes')->isArray()->getValue() ?? []
37        );
38
39        $workstationScopeId = $reportHelper->getWorkstationScopeId($this->workstation);
40        $scopeIds = $reportHelper->resolveScopeIdParam($selectedScopes, $workstationScopeId);
41
42        $requestPeriod = $workstationScopeId !== null
43            ? $reportRequestService->getRequestPeriod((string) $workstationScopeId)
44            : null;
45
46        $dateRange = $reportHelper->extractDateRange(
47            $validator->getParameter('from')->isString()->getValue(),
48            $validator->getParameter('to')->isString()->getValue()
49        );
50
51        $exchangeRequest = $reportRequestService->getExchangeRequestData($scopeIds, $dateRange, $args);
52
53        $type = $validator->getParameter('type')->isString()->getValue();
54        if ($type) {
55            return $this->handleDownloadRequest(
56                $request,
57                $response,
58                $args,
59                $exchangeRequest,
60                $dateRange,
61                $selectedScopes,
62                $reportRequestService
63            );
64        }
65
66        return $this->renderHtmlResponse(
67            $response,
68            $args,
69            $requestPeriod,
70            $dateRange,
71            $exchangeRequest,
72            $selectedScopes
73        );
74    }
75
76    /**
77     * Handle download request and return Excel file
78     */
79    private function handleDownloadRequest(
80        RequestInterface $request,
81        ResponseInterface $response,
82        array $args,
83        mixed $exchangeRequest,
84        array|null $dateRange,
85        array $selectedScopes = [],
86        ReportRequestService|null $reportRequestService = null
87    ): ResponseInterface {
88        if ($reportRequestService === null) {
89            $reportRequestService = new ReportRequestService();
90        }
91
92        $args = $reportRequestService->prepareDownloadArgs($args, $exchangeRequest, $dateRange, $selectedScopes);
93
94        $args['scope'] = $this->workstation->getScope();
95        $args['department'] = $this->department;
96        $args['organisation'] = $this->organisation;
97
98        /** @var mixed $container */
99        $container = \App::$slim->getContainer();
100        return (new Download\RequestReport($container))->readResponse($request, $response, $args);
101    }
102
103    /**
104     * Render HTML response for the report page
105     */
106    private function renderHtmlResponse(
107        ResponseInterface $response,
108        array $args,
109        mixed $requestPeriod,
110        array|null $dateRange,
111        mixed $exchangeRequest,
112        array $selectedScopes = []
113    ): ResponseInterface {
114        return Render::withHtml(
115            $response,
116            'page/reportRequestIndex.twig',
117            array(
118                'title' => 'Dienstleistungsstatistik Standort',
119                'activeScope' => 'active',
120                'menuActive' => 'request',
121                'department' => $this->department,
122                'organisation' => $this->organisation,
123                'requestPeriod' => $requestPeriod,
124                'showAll' => 1,
125                'period' => isset($args['period']) ? $args['period'] : null,
126                'dateRange' => $dateRange,
127                'exchangeRequest' => $exchangeRequest,
128                'source' => ['entity' => 'RequestIndex'],
129                'selectedScopeIds' => $selectedScopes,
130                'workstation' => $this->workstation->getArrayCopy()
131            )
132        );
133    }
134}