Lines
78.92%
176 / 223
Methods
62.50%
15 / 24
Classes
0.00%
0 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| readResponse | 87.50% | 7 / 8 | 0.00% | 0 / 1 | 3.02 | ||
| writeReport | 100.00% | 8 / 8 | 100.00% | 1 / 1 | 1 | ||
| getReportDates | 20.00% | 2 / 10 | 0.00% | 0 / 1 | 12.19 | ||
| writeHeader | 100.00% | 10 / 10 | 100.00% | 1 / 1 | 2 | ||
| writeReportData | 95.34% | 41 / 43 | 0.00% | 0 / 1 | 13 | ||
| [BO\Zmsstatistic\Download\Base] sanitizeDownloadFilenamePart | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsstatistic\Download\Base] resolveDictionaryHeader | 33.33% | 2 / 6 | 0.00% | 0 / 1 | 5.67 | ||
| [BO\Zmsstatistic\Download\Base] writeRawReport | 100.00% | 9 / 9 | 100.00% | 1 / 1 | 5 | ||
| [BO\Zmsstatistic\Download\Base] writeFilteredExchangeReport | 93.75% | 15 / 16 | 0.00% | 0 / 1 | 6.01 | ||
| [BO\Zmsstatistic\Download\Base] writeInfoHeader | 92.59% | 25 / 27 | 0.00% | 0 / 1 | 10.04 | ||
| [BO\Zmsstatistic\Download\Base] setDateTime | 75.00% | 3 / 4 | 0.00% | 0 / 1 | 3.14 | ||
| [BO\Zmsstatistic\Download\Base] getFormatedDates | 100.00% | 9 / 9 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsstatistic\BaseController] __invoke | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 2 | ||
| [BO\Zmsstatistic\Helper\Access] initAccessRights | 100.00% | 6 / 6 | 100.00% | 1 / 1 | 4 | ||
| [BO\Zmsstatistic\Helper\Access] readWorkstation | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsstatistic\Helper\Access] readDepartment | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 2 | ||
| [BO\Zmsstatistic\Helper\Access] readOrganisation | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 2 | ||
| [BO\Zmsstatistic\Helper\Access] readOwner | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 2 | ||
| [BO\Zmsstatistic\Helper\Access] validateAccessRights | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsstatistic\Helper\Access] validateAccess | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 7 | ||
| [BO\Zmsstatistic\Helper\Access] validateScope | 66.66% | 2 / 3 | 0.00% | 0 / 1 | 4.59 | ||
| [BO\Zmsstatistic\Helper\Access] isPathWithoutScope | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 4 | ||
| [BO\Zmsstatistic\Helper\Access] testLogin | 0.00% | 0 / 27 | 0.00% | 0 / 1 | 42 | ||
| [BO\Zmsstatistic\Helper\Access] exceptionTemplateExists | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 1 | ||
| 17 | class RequestReport extends Base | |
| 18 | { | |
| 19 | public ?\DateTime $firstDayDate = null; | |
| 20 | ||
| 21 | public ?\DateTime $lastDayDate = null; | |
| 22 | ||
| 23 | protected array $dateFormatter = [ | |
| 24 | 'day' => 'Y-m-d', | |
| 25 | 'month' => 'Y-m' | |
| 26 | ]; | |
| 27 | ||
| 28 | /** | |
| 29 | * @SuppressWarnings(Param) | |
| 30 | * @return ResponseInterface | |
| 31 | */ | |
| 32 | #[\Override] | |
| 33 | public function readResponse( | |
| 34 | RequestInterface $request, | |
| 35 | ResponseInterface $response, | |
| 36 | array $args | |
| 37 | ): mixed { | |
| 38 | $title = 'requeststatistic_' . $args['period']; | |
| 39 | $download = (new Download($request))->setSpreadSheet($title); | |
| 40 | ||
| 41 | $this->writeInfoHeader($args, $download->getSpreadSheet()); | |
| 42 | foreach ($args['reports'] as $report) { | |
| 43 | if ('month' == $report->period) { | |
| 44 | $this->writeReport($report, $download->getSpreadSheet(), 'MMMM'); | |
| 45 | } else { | |
| 46 | $this->writeReport($report, $download->getSpreadSheet()); | |
| 47 | } | |
| 48 | } | |
| 49 | ||
| 50 | return $download->writeDownload($response); | |
| 51 | } | |
| 52 | ||
| 53 | public function writeReport( | |
| 54 | ReportEntity $report, | |
| 55 | Spreadsheet $spreadsheet, | |
| 56 | string $datePatternCol = 'dd.MM.yyyy' | |
| 57 | ): Spreadsheet { | |
| 58 | $sheet = $spreadsheet->getActiveSheet(); | |
| 59 | ||
| 60 | $firstDay = $report->firstDay->year . '-' . $report->firstDay->month . '-' . $report->firstDay->day; | |
| 61 | $lastDay = $report->lastDay->year . '-' . $report->lastDay->month . '-' . $report->lastDay->day; | |
| 62 | $this->firstDayDate = $this->setDateTime($firstDay); | |
| 63 | $this->lastDayDate = $this->setDateTime($lastDay); | |
| 64 | ||
| 65 | $this->writeHeader($report, $sheet, $datePatternCol); | |
| 66 | $this->writeReportData($report, $sheet); | |
| 67 | ||
| 68 | return $spreadsheet; | |
| 69 | } | |
| 70 | ||
| 71 | private function getReportDates(ReportEntity $report): array | |
| 72 | { | |
| 73 | if ($report->period === 'day') { | |
| 74 | return $report->getDatesWithRequests(); | |
| 75 | } | |
| 76 | ||
| 77 | $dates = []; | |
| 78 | if (!$this->firstDayDate instanceof \DateTime || !$this->lastDayDate instanceof \DateTime) { | |
| 79 | throw new \RuntimeException('Report date range is not initialized'); | |
| 80 | } | |
| 81 | $dateTime = clone $this->firstDayDate; | |
| 82 | ||
| 83 | do { | |
| 84 | $dates[] = $dateTime->format($this->dateFormatter[$report->period]); | |
| 85 | $dateTime->modify('+1 ' . $report->period); | |
| 86 | } while ($dateTime <= $this->lastDayDate); | |
| 87 | ||
| 88 | return $dates; | |
| 89 | } | |
| 90 | ||
| 91 | public function writeHeader( | |
| 92 | ReportEntity $report, | |
| 93 | \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet $sheet, | |
| 94 | string $datePatternCol | |
| 95 | ): void { | |
| 96 | $reportHeader = []; | |
| 97 | $reportHeader[] = 'Dienstleistung'; | |
| 98 | $reportHeader[] = 'Ø Bearbeitungsdauer'; | |
| 99 | $reportHeader[] = 'Summe'; | |
| 100 | foreach ($this->getReportDates($report) as $date) { | |
| 101 | $reportHeader[] = $this->getFormatedDates( | |
| 102 | $this->setDateTime($date), | |
| 103 | $datePatternCol | |
| 104 | ); | |
| 105 | } | |
| 106 | $sheet->fromArray($reportHeader, null, 'A' . ($sheet->getHighestRow() + 2)); | |
| 107 | } | |
| 108 | ||
| 109 | /** | |
| 110 | * @SuppressWarnings(Unused) | |
| 111 | */ | |
| 112 | public function writeReportData(ReportEntity $report, \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet $sheet): void | |
| 113 | { | |
| 114 | $reportData = []; | |
| 115 | $firstDataRow = $sheet->getHighestRow() + 1; | |
| 116 | $totalSum = 0; | |
| 117 | $reportDates = $this->getReportDates($report); | |
| 118 | $dateSums = array_fill(0, count($reportDates), 0); | |
| 119 | ||
| 120 | foreach ($report->data as $name => $entry) { | |
| 121 | if ($name !== 'sum' && $name !== 'average_processingtime' && $name !== 'average_processingtime_overall') { | |
| 122 | $rowData = []; | |
| 123 | if ($name === ReportEntity::REQUEST_STAT_NAME_UNCATEGORIZED) { | |
| 124 | $rowData[] = 'Dienstleistung wurde nicht erfasst'; | |
| 125 | } elseif ($name === ReportEntity::REQUEST_STAT_NAME_NONEXISTENT) { | |
| 126 | $rowData[] = 'Dienstleistung konnte nicht erbracht werden'; | |
| 127 | } else { | |
| 128 | $rowData[] = $name; | |
| 129 | } | |
| 130 | $rowData[] = isset($report->data['average_processingtime'][$name]) | |
| 131 | && is_numeric($report->data['average_processingtime'][$name]) | |
| 132 | ? ReportHelper::formatTimeValue($report->data['average_processingtime'][$name]) | |
| 133 | : "0"; | |
| 134 | $rowData[] = $report->data['sum'][$name]; | |
| 135 | $totalSum += (int)($report->data['sum'][$name] ?? 0); | |
| 136 | ||
| 137 | foreach ($reportDates as $dateColumn => $dateString) { | |
| 138 | $requestCount = isset($entry[$dateString]) | |
| 139 | ? (int) $entry[$dateString]['requestscount'] | |
| 140 | : 0; | |
| 141 | ||
| 142 | $rowData[] = $requestCount; | |
| 143 | $dateSums[$dateColumn] += $requestCount; | |
| 144 | } | |
| 145 | ||
| 146 | $reportData[] = $rowData; | |
| 147 | } | |
| 148 | } | |
| 149 | ||
| 150 | $sheet->fromArray($reportData, null, 'A' . $firstDataRow); | |
| 151 | $overallProcessingTime = | |
| 152 | isset( | |
| 153 | $report->data['average_processingtime_overall'] | |
| 154 | ) | |
| 155 | && is_numeric($report->data['average_processingtime_overall']) | |
| 156 | ? ReportHelper::formatTimeValue($report->data['average_processingtime_overall']) : '0'; | |
| 157 | $sumRowIndex = $sheet->getHighestRow() + 2; | |
| 158 | $sumRow = array_merge( | |
| 159 | [ | |
| 160 | 'Ø Bearbeitungsdauer (unabhängig von DL) / Summe', | |
| 161 | $overallProcessingTime, | |
| 162 | $totalSum | |
| 163 | ], | |
| 164 | $dateSums | |
| 165 | ); | |
| 166 | ||
| 167 | $sheet->fromArray($sumRow, null, 'A' . $sumRowIndex); | |
| 168 | } | |
| 169 | } |
Inherited from BO\Zmsstatistic\Download\Base
| 70 | protected function sanitizeDownloadFilenamePart(string $part): string | |
| 71 | { | |
| 72 | return str_replace(',', '_', $part); | |
| 73 | } |
| 75 | protected function resolveDictionaryHeader(array $item): string | |
| 76 | { | |
| 77 | if (!empty($item['description'])) { | |
| 78 | return (string) $item['description']; | |
| 79 | } | |
| 80 | ||
| 81 | $variable = $item['variable'] ?? ''; | |
| 82 | if (isset(static::$headlines[$variable])) { | |
| 83 | return static::$headlines[$variable]; | |
| 84 | } | |
| 85 | ||
| 86 | return (string) $variable; | |
| 87 | } |
| 89 | protected function writeRawReport(Exchange $report, Spreadsheet $spreadsheet): Spreadsheet | |
| 90 | { | |
| 91 | $sheet = $spreadsheet->getActiveSheet(); | |
| 92 | $reportData = []; | |
| 93 | foreach ($report->dictionary as $item) { | |
| 94 | $reportData['header'][] = $this->resolveDictionaryHeader($item); | |
| 95 | } | |
| 96 | foreach ($report->data as $row => $entry) { | |
| 97 | foreach ($entry as $item) { | |
| 98 | $reportData[$row][] = is_numeric($item) ? (string) $item : $item; | |
| 99 | } | |
| 100 | } | |
| 101 | $sheet->fromArray($reportData, null, 'A' . ($sheet->getHighestRow())); | |
| 102 | ||
| 103 | return $spreadsheet; | |
| 104 | } |
| 106 | protected function writeFilteredExchangeReport( | |
| 107 | Exchange $report, | |
| 108 | Spreadsheet $spreadsheet, | |
| 109 | array $skipVariables = [], | |
| 110 | int $rowGap = 0 | |
| 111 | ): Spreadsheet { | |
| 112 | $sheet = $spreadsheet->getActiveSheet(); | |
| 113 | $columnIndexes = []; | |
| 114 | $reportData = []; | |
| 115 | ||
| 116 | foreach ($report->dictionary as $index => $item) { | |
| 117 | $variable = $item['variable'] ?? ''; | |
| 118 | if (in_array($variable, $skipVariables, true)) { | |
| 119 | continue; | |
| 120 | } | |
| 121 | $columnIndexes[] = $index; | |
| 122 | $reportData['header'][] = $this->resolveDictionaryHeader($item); | |
| 123 | } | |
| 124 | ||
| 125 | foreach ($report->data as $row => $entry) { | |
| 126 | foreach ($columnIndexes as $index) { | |
| 127 | $value = $entry[$index] ?? ''; | |
| 128 | $reportData[$row][] = is_numeric($value) ? (string) $value : $value; | |
| 129 | } | |
| 130 | } | |
| 131 | ||
| 132 | $startRow = $sheet->getHighestRow() + $rowGap; | |
| 133 | $sheet->fromArray($reportData, null, 'A' . $startRow); | |
| 134 | ||
| 135 | return $spreadsheet; | |
| 136 | } |
| 138 | protected function writeInfoHeader(array $args, Spreadsheet $spreadsheet): Spreadsheet|null | |
| 139 | { | |
| 140 | $sheet = $spreadsheet->getActiveSheet(); | |
| 141 | $infoData = []; | |
| 142 | $infoData[] = static::$subjectTranslations[$args['category']]; | |
| 143 | if (isset($args['selectedScopes'])) { | |
| 144 | try { | |
| 145 | $scopesResult = \App::http()->readGetResult('/scope/')->getData(); | |
| 146 | $scopeMap = []; | |
| 147 | foreach ($scopesResult ?? [] as $scope) { | |
| 148 | $scopeMap[$scope->id] = $scope; | |
| 149 | } | |
| 150 | foreach ($args['selectedScopes'] as $scopeId) { | |
| 151 | if (isset($scopeMap[$scopeId])) { | |
| 152 | $infoData[] = $scopeMap[$scopeId]->provider->name . " " . $scopeMap[$scopeId]->shortName; | |
| 153 | } | |
| 154 | } | |
| 155 | } catch (Exception $exception) { | |
| 156 | return null; | |
| 157 | } | |
| 158 | } else { | |
| 159 | if (isset($args['organisation'])) { | |
| 160 | $infoData[] = $args['organisation']['name'] ; | |
| 161 | } | |
| 162 | if (isset($args['department'])) { | |
| 163 | $infoData[] = $args['department']['name']; | |
| 164 | } | |
| 165 | if (isset($args['scope'])) { | |
| 166 | $infoData[] = $args['scope']['contact']['name'] . ' ' . $args['scope']['shortName']; | |
| 167 | } | |
| 168 | } | |
| 169 | ||
| 170 | $infoData = array_chunk($infoData, 1); | |
| 171 | $sheet->fromArray($infoData, null, 'A' . $sheet->getHighestRow()); | |
| 172 | ||
| 173 | if (isset($args['reports'][0]->firstDay)) { | |
| 174 | $firstDay = $args['reports'][0]->firstDay->toDateTime()->format('d.m.Y'); | |
| 175 | $lastDay = $args['reports'][0]->lastDay->toDateTime()->format('d.m.Y'); | |
| 176 | $range = array('Zeitraum:', $firstDay, 'bis', $lastDay); | |
| 177 | $sheet->fromArray($range, null, 'A' . ($sheet->getHighestRow() + 1)); | |
| 178 | } | |
| 179 | ||
| 180 | return $spreadsheet; | |
| 181 | } |
| 184 | protected function setDateTime(string $dateString): \DateTime | |
| 185 | { | |
| 186 | $dateArr = explode('-', $dateString); | |
| 187 | if (2 == count($dateArr)) { | |
| 188 | $dateString = $dateString . '-01'; | |
| 189 | } | |
| 190 | /* ignore because not in use now */ | |
| 191 | //@codeCoverageIgnoreStart | |
| 192 | if (1 == count($dateArr)) { | |
| 193 | $dateString = $dateString . '-01-01'; | |
| 194 | } | |
| 195 | //@codeCoverageIgnoreEnd | |
| 196 | return new \DateTime($dateString); | |
| 197 | } |
| 203 | protected function getFormatedDates(\DateTimeInterface $date, string $pattern = 'MMMM'): string|false | |
| 204 | { | |
| 205 | $dateFormatter = new \IntlDateFormatter( | |
| 206 | 'de-DE', | |
| 207 | \IntlDateFormatter::MEDIUM, | |
| 208 | \IntlDateFormatter::MEDIUM, | |
| 209 | \App::TIMEZONE, | |
| 210 | \IntlDateFormatter::GREGORIAN, | |
| 211 | $pattern | |
| 212 | ); | |
| 213 | ||
| 214 | return $dateFormatter->format($date->getTimestamp()); | |
| 215 | } |
Inherited from BO\Zmsstatistic\BaseController
| 20 | public function __invoke(RequestInterface $request, ResponseInterface $response, array $args) | |
| 21 | { | |
| 22 | $request = $this->initRequest($request); | |
| 23 | if ($this->withAccess) { | |
| 24 | $this->initAccessRights($request); | |
| 25 | } | |
| 26 | $noCacheResponse = \BO\Slim\Render::withLastModified($response, time(), '0'); | |
| 27 | return $this->readResponse($request, $noCacheResponse, $args); | |
| 28 | } |
Inherited from BO\Zmsstatistic\Helper\Access
| 33 | protected function initAccessRights(RequestInterface $request): void | |
| 34 | { | |
| 35 | $this->workstation = $this->readWorkstation(); | |
| 36 | if ($this->workstation && isset($this->workstation->scope['id']) && $this->workstation->scope['id'] > 0) { | |
| 37 | $this->department = $this->readDepartment(); | |
| 38 | $this->organisation = $this->readOrganisation(); | |
| 39 | $this->owner = $this->readOwner(); | |
| 40 | } | |
| 41 | $this->validateAccessRights($request); | |
| 42 | } |
| 44 | protected function readWorkstation(): mixed | |
| 45 | { | |
| 46 | $workstation = \App::http()->readGetResult('/workstation/', ['resolveReferences' => $this->resolveLevel]); | |
| 47 | return $workstation->getEntity(); | |
| 48 | } |
| 50 | protected function readDepartment(): mixed | |
| 51 | { | |
| 52 | if ($this->workstation->getUseraccount()->hasPermissions(['statistic'])) { | |
| 53 | return \App::http() | |
| 54 | ->readGetResult('/scope/' . $this->workstation->scope['id'] . '/department/') | |
| 55 | ->getEntity(); | |
| 56 | } | |
| 57 | return null; | |
| 58 | } |
| 60 | protected function readOrganisation(): mixed | |
| 61 | { | |
| 62 | if ($this->workstation->getUseraccount()->isSuperUser()) { | |
| 63 | return \App::http() | |
| 64 | ->readGetResult('/department/' . $this->department->getId() . '/organisation/') | |
| 65 | ->getEntity(); | |
| 66 | } | |
| 67 | return null; | |
| 68 | } |
| 70 | protected function readOwner(): mixed | |
| 71 | { | |
| 72 | if ($this->workstation->getUseraccount()->isSuperUser()) { | |
| 73 | return \App::http() | |
| 74 | ->readGetResult('/organisation/' . $this->organisation->getId() . '/owner/') | |
| 75 | ->getEntity(); | |
| 76 | } | |
| 77 | return null; | |
| 78 | } |
| 80 | protected function validateAccessRights(RequestInterface $request): void | |
| 81 | { | |
| 82 | $path = $request->getUri()->getPath(); | |
| 83 | $this->validateAccess($path); | |
| 84 | $this->validateScope($path); | |
| 85 | } |
| 87 | protected function validateAccess(string $path): void | |
| 88 | { | |
| 89 | if ( | |
| 90 | (false !== strpos($path, 'owner') && ! $this->owner) || | |
| 91 | (false !== strpos($path, 'organisation') && ! $this->organisation) || | |
| 92 | (false !== strpos($path, 'department') && ! $this->department) | |
| 93 | ) { | |
| 94 | throw new UserAccountAccessRightsFailed(); | |
| 95 | } | |
| 96 | } |
| 98 | protected function validateScope(string $path): void | |
| 99 | { | |
| 100 | if ( | |
| 101 | $this->isPathWithoutScope($path) | |
| 102 | && (! isset($this->workstation['scope']) || ! isset($this->workstation['scope']['id'])) | |
| 103 | ) { | |
| 104 | throw new WorkstationMissingScope(); | |
| 105 | } | |
| 106 | } |
| 108 | protected function isPathWithoutScope(string $path): bool | |
| 109 | { | |
| 110 | // TODO: refactor to integrate these access rules in the controller to make them visible | |
| 111 | return (false === strpos($path, 'select') | |
| 112 | && false === strpos($path, 'warehouse') | |
| 113 | && false === strpos($path, 'logout') | |
| 114 | && false === strpos($path, 'report') | |
| 115 | ); | |
| 116 | } |
| 121 | protected function testLogin(mixed $input): mixed | |
| 122 | { | |
| 123 | $userAccount = new Useraccount(array( | |
| 124 | 'id' => $input['loginName'], | |
| 125 | 'password' => $input['password'], | |
| 126 | 'departments' => array('id' => 0) // required in schema validation | |
| 127 | )); | |
| 128 | try { | |
| 129 | /** @var Workstation $workstation */ | |
| 130 | $workstation = \App::http()->readPostResult('/workstation/login/', $userAccount)->getEntity(); | |
| 131 | return $workstation; | |
| 132 | } catch (\BO\Zmsclient\Exception $exception) { | |
| 133 | $template = TwigExceptionHandler::getExceptionTemplate($exception); | |
| 134 | if ('BO\Zmsentities\Exception\SchemaValidation' == $exception->template) { | |
| 135 | $exceptionData = [ | |
| 136 | 'template' => 'exception/bo/zmsbackend/useraccount/exception/invalidcredentials.twig' | |
| 137 | ]; | |
| 138 | $exceptionData['data']['password']['messages'] = [ | |
| 139 | 'Der Nutzername oder das Passwort wurden falsch eingegeben' | |
| 140 | ]; | |
| 141 | } elseif ('BO\Zmsbackend\Useraccount\Exception\UserAlreadyLoggedIn' == $exception->template) { | |
| 142 | Auth::setKey($exception->data['authkey'], time() + \App::SESSION_DURATION); | |
| 143 | throw $exception; | |
| 144 | } elseif ( | |
| 145 | '' != $exception->template | |
| 146 | && $this->exceptionTemplateExists($template) | |
| 147 | ) { | |
| 148 | $exceptionData = [ | |
| 149 | 'template' => $template, | |
| 150 | 'data' => $exception->data | |
| 151 | ]; | |
| 152 | } else { | |
| 153 | throw $exception; | |
| 154 | } | |
| 155 | } | |
| 156 | return $exceptionData; | |
| 157 | } |
| 159 | protected function exceptionTemplateExists(string $template): bool | |
| 160 | { | |
| 161 | /** @var mixed $container */ | |
| 162 | $container = \App::$slim->getContainer(); | |
| 163 | return $container->get('view')->getLoader()->exists($template); | |
| 164 | } |