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