Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
98.10% |
103 / 105 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
| ReportCapacityIndex | |
98.10% |
103 / 105 |
|
33.33% |
1 / 3 |
11 | |
0.00% |
0 / 1 |
| readResponse | |
98.39% |
61 / 62 |
|
0.00% |
0 / 1 |
6 | |||
| handleDownloadRequest | |
95.00% |
19 / 20 |
|
0.00% |
0 / 1 |
4 | |||
| renderHtmlResponse | |
100.00% |
23 / 23 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @package Zmsstatistic |
| 5 | * @copyright BerlinOnline Stadtportal GmbH & Co. KG |
| 6 | **/ |
| 7 | |
| 8 | namespace BO\Zmsstatistic; |
| 9 | |
| 10 | use BO\Slim\Render; |
| 11 | use BO\Zmsentities\Exchange; |
| 12 | use BO\Zmsstatistic\Helper\ReportHelper; |
| 13 | use BO\Zmsstatistic\Service\ReportCapacityService; |
| 14 | use Psr\Http\Message\RequestInterface; |
| 15 | use Psr\Http\Message\ResponseInterface; |
| 16 | |
| 17 | class ReportCapacityIndex extends BaseController |
| 18 | { |
| 19 | protected $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 | ) { |
| 31 | $this->workstation->getUseraccount()->testPermissions(['statistic', 'capacityreport']); |
| 32 | |
| 33 | $validator = $request->getAttribute('validator'); |
| 34 | $reportCapacityService = new ReportCapacityService(); |
| 35 | $reportHelper = new ReportHelper(); |
| 36 | |
| 37 | $selectedScopes = $reportHelper->extractSelectedScopes( |
| 38 | $validator->getParameter('scopes')->isArray()->getValue() ?? [] |
| 39 | ); |
| 40 | |
| 41 | $workstationScopeId = $reportHelper->getWorkstationScopeId($this->workstation); |
| 42 | $scopeId = $reportHelper->resolveScopeIdParam($selectedScopes, $workstationScopeId); |
| 43 | |
| 44 | $capacityPeriod = $workstationScopeId !== null |
| 45 | ? $reportCapacityService->getCapacityPeriod((string) $workstationScopeId) |
| 46 | : null; |
| 47 | $scopeDateBounds = $reportCapacityService->getScopeDateBoundsByScopeId(); |
| 48 | |
| 49 | $dateRange = $reportHelper->extractDateRange( |
| 50 | $validator->getParameter('from')->isString()->getValue(), |
| 51 | $validator->getParameter('to')->isString()->getValue() |
| 52 | ); |
| 53 | |
| 54 | $exchangeCapacity = $reportCapacityService->getExchangeCapacityData($scopeId, $dateRange, $args); |
| 55 | $exchangeCapacityChart = null; |
| 56 | $exchangeCapacityChartSparse = null; |
| 57 | |
| 58 | if ($exchangeCapacity instanceof Exchange) { |
| 59 | $period = $args['period'] ?? null; |
| 60 | $exchangeCapacityChartSparse = $reportCapacityService->buildSparseChartExchange( |
| 61 | $exchangeCapacity, |
| 62 | $dateRange, |
| 63 | $period |
| 64 | ); |
| 65 | $exchangeCapacityChart = $reportCapacityService->buildChartExchange( |
| 66 | $exchangeCapacity, |
| 67 | $dateRange, |
| 68 | $period |
| 69 | ); |
| 70 | } |
| 71 | |
| 72 | $type = $validator->getParameter('type')->isString()->getValue(); |
| 73 | if ($type) { |
| 74 | return $this->handleDownloadRequest( |
| 75 | $request, |
| 76 | $response, |
| 77 | $args, |
| 78 | $scopeId, |
| 79 | $exchangeCapacity, |
| 80 | $dateRange, |
| 81 | $selectedScopes, |
| 82 | $reportCapacityService |
| 83 | ); |
| 84 | } |
| 85 | |
| 86 | $displayScopeIds = $selectedScopes; |
| 87 | if ($displayScopeIds === [] && $workstationScopeId !== null) { |
| 88 | $displayScopeIds = [(string) $workstationScopeId]; |
| 89 | } |
| 90 | $scopeSlotTimeHint = $reportCapacityService->formatScopeSlotTimeHint( |
| 91 | $reportCapacityService->getSelectedScopeSlotTimes($displayScopeIds) |
| 92 | ); |
| 93 | |
| 94 | return $this->renderHtmlResponse( |
| 95 | $response, |
| 96 | $args, |
| 97 | $capacityPeriod, |
| 98 | $dateRange, |
| 99 | $exchangeCapacity, |
| 100 | $exchangeCapacityChart, |
| 101 | $exchangeCapacityChartSparse, |
| 102 | $selectedScopes, |
| 103 | $scopeDateBounds, |
| 104 | $scopeSlotTimeHint |
| 105 | ); |
| 106 | } |
| 107 | |
| 108 | private function handleDownloadRequest( |
| 109 | RequestInterface $request, |
| 110 | ResponseInterface $response, |
| 111 | array $args, |
| 112 | string $scopeId, |
| 113 | mixed $exchangeCapacity, |
| 114 | ?array $dateRange, |
| 115 | array $selectedScopes = [], |
| 116 | ?ReportCapacityService $reportCapacityService = null |
| 117 | ): ResponseInterface { |
| 118 | if ($reportCapacityService === null) { |
| 119 | $reportCapacityService = new ReportCapacityService(); |
| 120 | } |
| 121 | |
| 122 | $validator = $request->getAttribute('validator'); |
| 123 | $valueMode = $validator->getParameter('valueMode')->isString()->getValue(); |
| 124 | $valueMode = $valueMode === 'minutes' ? 'minutes' : 'slots'; |
| 125 | $channelMode = $validator->getParameter('channelMode')->isString()->getValue(); |
| 126 | $channelMode = in_array($channelMode, ['total', 'public', 'intern_only'], true) |
| 127 | ? $channelMode |
| 128 | : 'total'; |
| 129 | |
| 130 | $args = $reportCapacityService->prepareDownloadArgs( |
| 131 | $args, |
| 132 | $scopeId, |
| 133 | $exchangeCapacity, |
| 134 | $dateRange, |
| 135 | $selectedScopes, |
| 136 | $valueMode, |
| 137 | $channelMode |
| 138 | ); |
| 139 | |
| 140 | return (new Download\CapacityReport(\App::$slim->getContainer())) |
| 141 | ->readResponse($request, $response, $args); |
| 142 | } |
| 143 | |
| 144 | private function renderHtmlResponse( |
| 145 | $response, |
| 146 | $args, |
| 147 | $capacityPeriod, |
| 148 | $dateRange, |
| 149 | $exchangeCapacity, |
| 150 | $exchangeCapacityChart, |
| 151 | $exchangeCapacityChartSparse, |
| 152 | $selectedScopes = [], |
| 153 | array $scopeDateBounds = [], |
| 154 | ?string $scopeSlotTimeHint = null |
| 155 | ): ResponseInterface { |
| 156 | return Render::withHtml( |
| 157 | $response, |
| 158 | 'page/reportCapacityIndex.twig', |
| 159 | [ |
| 160 | 'title' => 'Terminkapazität Standort', |
| 161 | 'activeScope' => 'active', |
| 162 | 'menuActive' => 'capacity', |
| 163 | 'department' => $this->department, |
| 164 | 'organisation' => $this->organisation, |
| 165 | 'capacityPeriod' => $capacityPeriod, |
| 166 | 'scopeDateBounds' => $scopeDateBounds, |
| 167 | 'showAll' => 1, |
| 168 | 'period' => $args['period'] ?? null, |
| 169 | 'dateRange' => $dateRange, |
| 170 | 'exchangeCapacity' => $exchangeCapacity, |
| 171 | 'exchangeCapacityChart' => $exchangeCapacityChart, |
| 172 | 'exchangeCapacityChartSparse' => $exchangeCapacityChartSparse, |
| 173 | 'source' => ['entity' => 'CapacityIndex'], |
| 174 | 'selectedScopeIds' => $selectedScopes, |
| 175 | 'scopeSlotTimeHint' => $scopeSlotTimeHint, |
| 176 | 'workstation' => $this->workstation->getArrayCopy(), |
| 177 | ] |
| 178 | ); |
| 179 | } |
| 180 | } |