Lines
77.16%
98 / 127
Methods
80.00%
12 / 15
Classes
0.00%
0 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| readResponse | 100.00% | 28 / 28 | 100.00% | 1 / 1 | 3 | ||
| handleDownloadRequest | 87.50% | 7 / 8 | 0.00% | 0 / 1 | 2.01 | ||
| renderHtmlResponse | 100.00% | 19 / 19 | 100.00% | 1 / 1 | 2 | ||
| [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 | ||
| 16 | class 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 | } |
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 | } |