Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
98.18% covered (success)
98.18%
54 / 55
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ReportClientIndex
98.18% covered (success)
98.18%
54 / 55
66.67% covered (warning)
66.67%
2 / 3
7
0.00% covered (danger)
0.00%
0 / 1
 readResponse
100.00% covered (success)
100.00%
28 / 28
100.00% covered (success)
100.00%
1 / 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\ReportClientService;
13use Psr\Http\Message\RequestInterface;
14use Psr\Http\Message\ResponseInterface;
15
16class ReportClientIndex 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        $reportClientService = new ReportClientService();
33        $reportHelper = new ReportHelper();
34
35        $selectedScopes = $reportHelper->extractSelectedScopes(
36            $validator->getParameter('scopes')->isArray()->getValue() ?? []
37        );
38
39        $workstationScopeId = $reportHelper->getWorkstationScopeId($this->workstation);
40        $scopeId = $reportHelper->resolveScopeIdParam($selectedScopes, $workstationScopeId);
41
42        $clientPeriod = $workstationScopeId !== null
43            ? $reportClientService->getClientPeriod((string) $workstationScopeId)
44            : null;
45
46        $dateRange = $reportHelper->extractDateRange(
47            $validator->getParameter('from')->isString()->getValue(),
48            $validator->getParameter('to')->isString()->getValue()
49        );
50
51        $exchangeClient = $reportClientService->getExchangeClientData($scopeId, $dateRange, $args);
52
53        $type = $validator->getParameter('type')->isString()->getValue();
54        if ($type) {
55            return $this->handleDownloadRequest(
56                $request,
57                $response,
58                $args,
59                $exchangeClient,
60                $dateRange,
61                $selectedScopes,
62                $reportClientService
63            );
64        }
65
66        return $this->renderHtmlResponse($response, $args, $clientPeriod, $dateRange, $exchangeClient, $selectedScopes);
67    }
68
69    /**
70     * Handle download request and return Excel file
71     */
72    private function handleDownloadRequest(
73        RequestInterface $request,
74        ResponseInterface $response,
75        array $args,
76        mixed $exchangeClient,
77        array|null $dateRange,
78        array $selectedScopes = [],
79        ReportClientService|null $reportClientService = null
80    ): ResponseInterface {
81        if ($reportClientService === null) {
82            $reportClientService = new ReportClientService();
83        }
84
85        $args = $reportClientService->prepareDownloadArgs($args, $exchangeClient, $dateRange, $selectedScopes);
86
87        $args['scope'] = $this->workstation->getScope();
88        $args['department'] = $this->department;
89        $args['organisation'] = $this->organisation;
90
91        /** @var mixed $container */
92        $container = \App::$slim->getContainer();
93        return (new Download\ClientReport($container))->readResponse($request, $response, $args);
94    }
95
96    /**
97     * Render HTML response for the report page
98     */
99    private function renderHtmlResponse(
100        ResponseInterface $response,
101        array $args,
102        mixed $clientPeriod,
103        array|null $dateRange,
104        mixed $exchangeClient,
105        array $selectedScopes = []
106    ): ResponseInterface {
107        return Render::withHtml(
108            $response,
109            'page/reportClientIndex.twig',
110            array(
111                'title' => 'Kundenstatistik Standort',
112                'activeScope' => 'active',
113                'menuActive' => 'client',
114                'department' => $this->department,
115                'organisation' => $this->organisation,
116                'clientPeriod' => $clientPeriod,
117                'showAll' => 1,
118                'period' => isset($args['period']) ? $args['period'] : null,
119                'dateRange' => $dateRange,
120                'exchangeClient' => $exchangeClient,
121                'source' => ['entity' => 'ClientIndex'],
122                'selectedScopeIds' => $selectedScopes,
123                'workstation' => $this->workstation->getArrayCopy()
124            )
125        );
126    }
127}