| Total Complexity | 54 |
| Total Lines | 508 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
Complex classes like AdministrationController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AdministrationController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 41 | class AdministrationController extends AbstractController |
||
| 42 | { |
||
| 43 | private const LANG_FILE = 'LLL:EXT:sf_event_mgt/Resources/Private/Language/locallang_be.xlf:'; |
||
| 44 | |||
| 45 | protected ModuleTemplateFactory $moduleTemplateFactory; |
||
| 46 | protected CustomNotificationLogRepository $customNotificationLogRepository; |
||
| 47 | protected ExportService $exportService; |
||
| 48 | protected SettingsService $settingsService; |
||
| 49 | protected BeUserSessionService $beUserSessionService; |
||
| 50 | protected MaintenanceService $maintenanceService; |
||
| 51 | protected IconFactory $iconFactory; |
||
| 52 | protected PageRenderer $pageRenderer; |
||
| 53 | protected int $pid = 0; |
||
| 54 | |||
| 55 | public function injectCustomNotificationLogRepository( |
||
| 56 | CustomNotificationLogRepository $customNotificationLogRepository |
||
| 57 | ): void { |
||
| 58 | $this->customNotificationLogRepository = $customNotificationLogRepository; |
||
| 59 | } |
||
| 60 | |||
| 61 | public function injectExportService(ExportService $exportService): void |
||
| 62 | { |
||
| 63 | $this->exportService = $exportService; |
||
| 64 | } |
||
| 65 | |||
| 66 | public function injectSettingsService(SettingsService $settingsService): void |
||
| 67 | { |
||
| 68 | $this->settingsService = $settingsService; |
||
| 69 | } |
||
| 70 | |||
| 71 | public function injectBeUserSessionService(BeUserSessionService $beUserSessionService): void |
||
| 72 | { |
||
| 73 | $this->beUserSessionService = $beUserSessionService; |
||
| 74 | } |
||
| 75 | |||
| 76 | public function injectIconFactory(IconFactory $iconFactory): void |
||
| 79 | } |
||
| 80 | |||
| 81 | public function injectMaintenanceService(MaintenanceService $maintenanceService): void |
||
| 84 | } |
||
| 85 | |||
| 86 | public function injectModuleTemplateFactory(ModuleTemplateFactory $moduleTemplateFactory): void |
||
| 89 | } |
||
| 90 | |||
| 91 | public function injectPageRenderer(PageRenderer $pageRenderer): void |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Register docHeaderButtons |
||
| 98 | */ |
||
| 99 | protected function registerDocHeaderButtons(ModuleTemplate $moduleTemplate): void |
||
| 100 | { |
||
| 101 | $buttonBar = $moduleTemplate->getDocHeaderComponent()->getButtonBar(); |
||
| 102 | |||
| 103 | if ($this->request->getControllerActionName() === 'list') { |
||
| 104 | $buttons = [ |
||
| 105 | [ |
||
| 106 | 'label' => 'administration.newEvent', |
||
| 107 | 'link' => $this->getCreateNewRecordUri('tx_sfeventmgt_domain_model_event'), |
||
| 108 | 'icon' => 'ext-sfeventmgt-event', |
||
| 109 | 'group' => 1, |
||
| 110 | ], |
||
| 111 | [ |
||
| 112 | 'label' => 'administration.newLocation', |
||
| 113 | 'link' => $this->getCreateNewRecordUri('tx_sfeventmgt_domain_model_location'), |
||
| 114 | 'icon' => 'ext-sfeventmgt-location', |
||
| 115 | 'group' => 1, |
||
| 116 | ], |
||
| 117 | [ |
||
| 118 | 'label' => 'administration.newOrganisator', |
||
| 119 | 'link' => $this->getCreateNewRecordUri('tx_sfeventmgt_domain_model_organisator'), |
||
| 120 | 'icon' => 'ext-sfeventmgt-organisator', |
||
| 121 | 'group' => 1, |
||
| 122 | ], |
||
| 123 | [ |
||
| 124 | 'label' => 'administration.newSpeaker', |
||
| 125 | 'link' => $this->getCreateNewRecordUri('tx_sfeventmgt_domain_model_speaker'), |
||
| 126 | 'icon' => 'ext-sfeventmgt-speaker', |
||
| 127 | 'group' => 1, |
||
| 128 | ], |
||
| 129 | [ |
||
| 130 | 'label' => 'administration.handleExpiredRegistrations', |
||
| 131 | 'link' => $this->uriBuilder->reset()->setRequest($this->request) |
||
| 132 | ->uriFor('handleExpiredRegistrations', [], 'Administration'), |
||
| 133 | 'icon' => 'ext-sfeventmgt-action-handle-expired', |
||
| 134 | 'group' => 2, |
||
| 135 | ], |
||
| 136 | ]; |
||
| 137 | foreach ($buttons as $tableConfiguration) { |
||
| 138 | if ($this->isButtonDisabledBySetting($tableConfiguration['label'])) { |
||
| 139 | continue; |
||
| 140 | } |
||
| 141 | |||
| 142 | $title = $this->getLanguageService()->sL(self::LANG_FILE . $tableConfiguration['label']); |
||
| 143 | $icon = $this->iconFactory->getIcon($tableConfiguration['icon'], Icon::SIZE_SMALL); |
||
| 144 | $viewButton = $buttonBar->makeLinkButton() |
||
| 145 | ->setHref($tableConfiguration['link']) |
||
| 146 | ->setDataAttributes([ |
||
| 147 | 'toggle' => 'tooltip', |
||
| 148 | 'placement' => 'bottom', |
||
| 149 | 'title' => $title, |
||
| 150 | ]) |
||
| 151 | ->setTitle($title) |
||
| 152 | ->setIcon($icon); |
||
| 153 | $buttonBar->addButton($viewButton, ButtonBar::BUTTON_POSITION_LEFT, $tableConfiguration['group']); |
||
| 154 | } |
||
| 155 | } |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Returns, if the given button is disabled by the setting |
||
| 160 | */ |
||
| 161 | private function isButtonDisabledBySetting(string $buttonLabel): bool |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Returns the create new record URL for the given table |
||
| 170 | */ |
||
| 171 | private function getCreateNewRecordUri(string $table): string |
||
| 187 | ]); |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Initializes module template and returns a response which must be used as response for any extbase action |
||
| 192 | * that should render a view. |
||
| 193 | */ |
||
| 194 | protected function initModuleTemplateAndReturnResponse(string $templateFileName, array $variables = []): ResponseInterface |
||
| 195 | { |
||
| 196 | $moduleTemplate = $this->moduleTemplateFactory->create($this->request); |
||
| 197 | $this->pageRenderer->addCssFile('EXT:sf_event_mgt/Resources/Public/Css/administration.css'); |
||
| 198 | |||
| 199 | $this->pageRenderer->loadJavaScriptModule('@derhansen/sf_event_mgt/administration-module.js'); |
||
| 200 | |||
| 201 | $this->registerDocHeaderButtons($moduleTemplate); |
||
| 202 | |||
| 203 | $moduleTemplate->setFlashMessageQueue($this->getFlashMessageQueue()); |
||
| 204 | |||
| 205 | $initAdministrationModuleTemplateEvent = new InitAdministrationModuleTemplateEvent( |
||
| 206 | $moduleTemplate, |
||
| 207 | $this->uriBuilder, |
||
| 208 | $this |
||
| 209 | ); |
||
| 210 | $this->eventDispatcher->dispatch($initAdministrationModuleTemplateEvent); |
||
| 211 | |||
| 212 | $variables['settings'] = $this->settings; |
||
| 213 | $moduleTemplate->assignMultiple($variables); |
||
| 214 | |||
| 215 | return $moduleTemplate->renderResponse($templateFileName); |
||
| 216 | } |
||
| 217 | |||
| 218 | public function initializeAction(): void |
||
| 219 | { |
||
| 220 | $this->pid = (int)($this->request->getQueryParams()['id'] ?? 0); |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Set date format for fields startDate and endDate |
||
| 225 | */ |
||
| 226 | public function initializeListAction(): void |
||
| 227 | { |
||
| 228 | // Static format needed for date picker (flatpickr), see BackendController::generateJavascript() and #91606 |
||
| 229 | if (!empty($this->settings)) { |
||
| 230 | $this->settings['search']['dateFormat'] = 'H:i d-m-Y'; |
||
| 231 | } |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * List action for backend module |
||
| 236 | */ |
||
| 237 | public function listAction(?SearchDemand $searchDemand = null, array $overwriteDemand = []): ResponseInterface |
||
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Resolves the page UIDs to search in respecting the given recursive option and additionally checking, if |
||
| 313 | * the current backend user is allowed to affected pages |
||
| 314 | */ |
||
| 315 | private function resolveSearchPageUids(int $recursive): string |
||
| 316 | { |
||
| 317 | $extendedPageUids = PageUtility::extendPidListByChildren( |
||
| 318 | (string)$this->pid, |
||
| 319 | $recursive |
||
| 320 | ); |
||
| 321 | $extendedPageUids = GeneralUtility::intExplode(',', $extendedPageUids, true); |
||
| 322 | |||
| 323 | $pageUids = []; |
||
| 324 | foreach ($extendedPageUids as $extendedPageUid) { |
||
| 325 | if (!in_array($extendedPageUid, $pageUids, true) && |
||
| 326 | $this->getBackendUser()->isInWebMount($extendedPageUid) |
||
| 327 | ) { |
||
| 328 | $pageUids[] = $extendedPageUid; |
||
| 329 | } |
||
| 330 | } |
||
| 331 | |||
| 332 | return implode(',', $pageUids); |
||
| 333 | } |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Returns, if reset filter operation has been used |
||
| 337 | */ |
||
| 338 | private function isResetFilter(): bool |
||
| 339 | { |
||
| 340 | $resetFilter = false; |
||
| 341 | if ($this->request->hasArgument('operation')) { |
||
| 342 | $resetFilter = $this->request->getArgument('operation') === 'reset-filters'; |
||
| 343 | } |
||
| 344 | |||
| 345 | return $resetFilter; |
||
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Export registrations for a given event |
||
| 350 | */ |
||
| 351 | public function exportAction(int $eventUid): void |
||
| 352 | { |
||
| 353 | /** @var Event $event */ |
||
| 354 | $event = $this->eventRepository->findByUidIncludeHidden($eventUid); |
||
| 355 | if ($event !== null && $this->checkEventAccess($event)) { |
||
| 356 | $this->exportService->downloadRegistrationsCsv($eventUid, $this->settings['csvExport'] ?? []); |
||
| 357 | } |
||
| 358 | exit(); |
||
| 359 | } |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Handles expired registrations |
||
| 363 | */ |
||
| 364 | public function handleExpiredRegistrationsAction(): ResponseInterface |
||
| 365 | { |
||
| 366 | $delete = (bool)($this->settings['registration']['deleteExpiredRegistrations'] ?? false); |
||
| 367 | $this->maintenanceService->handleExpiredRegistrations($delete); |
||
| 368 | |||
| 369 | $this->addFlashMessage( |
||
| 370 | $this->getLanguageService()->sL(self::LANG_FILE . 'administration.message-1.content'), |
||
| 371 | $this->getLanguageService()->sL(self::LANG_FILE . 'administration.message-1.title') |
||
| 372 | ); |
||
| 373 | |||
| 374 | return $this->redirect('list'); |
||
| 375 | } |
||
| 376 | |||
| 377 | /** |
||
| 378 | * The index notify action |
||
| 379 | */ |
||
| 380 | public function indexNotifyAction(Event $event): ResponseInterface |
||
| 381 | { |
||
| 382 | if (!$this->checkEventAccess($event)) { |
||
| 383 | return $this->redirect('list'); |
||
| 384 | } |
||
| 385 | |||
| 386 | $customNotification = GeneralUtility::makeInstance(CustomNotification::class); |
||
| 387 | $customNotifications = $this->settingsService->getCustomNotifications($this->settings); |
||
| 388 | $logEntries = $this->customNotificationLogRepository->findBy(['event' => $event]); |
||
| 389 | |||
| 390 | $modifyAdministrationIndexNotifyViewVariablesEvent = new ModifyAdministrationIndexNotifyViewVariablesEvent( |
||
| 391 | [ |
||
| 392 | 'event' => $event, |
||
| 393 | 'recipients' => $this->getNotificationRecipients(), |
||
| 394 | 'customNotification' => $customNotification, |
||
| 395 | 'customNotifications' => $customNotifications, |
||
| 396 | 'logEntries' => $logEntries, |
||
| 397 | ], |
||
| 398 | $this |
||
| 399 | ); |
||
| 400 | $this->eventDispatcher->dispatch($modifyAdministrationIndexNotifyViewVariablesEvent); |
||
| 401 | $variables = $modifyAdministrationIndexNotifyViewVariablesEvent->getVariables(); |
||
| 402 | |||
| 403 | return $this->initModuleTemplateAndReturnResponse('Administration/IndexNotify', $variables); |
||
| 404 | } |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Returns an array of recipient option for the indexNotify action |
||
| 408 | */ |
||
| 409 | public function getNotificationRecipients(): array |
||
| 410 | { |
||
| 411 | return [ |
||
| 412 | [ |
||
| 413 | 'value' => CustomNotification::RECIPIENTS_ALL, |
||
| 414 | 'label' => $this->getLanguageService()->sL( |
||
| 415 | self::LANG_FILE . 'administration.notify.recipients.' . CustomNotification::RECIPIENTS_ALL |
||
| 416 | ), |
||
| 417 | ], |
||
| 418 | [ |
||
| 419 | 'value' => CustomNotification::RECIPIENTS_CONFIRMED, |
||
| 420 | 'label' => $this->getLanguageService()->sL( |
||
| 421 | self::LANG_FILE . 'administration.notify.recipients.' . CustomNotification::RECIPIENTS_CONFIRMED |
||
| 422 | ), |
||
| 423 | ], |
||
| 424 | [ |
||
| 425 | 'value' => CustomNotification::RECIPIENTS_UNCONFIRMED, |
||
| 426 | 'label' => $this->getLanguageService()->sL( |
||
| 427 | self::LANG_FILE . 'administration.notify.recipients.' . CustomNotification::RECIPIENTS_UNCONFIRMED |
||
| 428 | ), |
||
| 429 | ], |
||
| 430 | [ |
||
| 431 | 'value' => CustomNotification::RECIPIENTS_WAITLIST_CONFIRMED, |
||
| 432 | 'label' => $this->getLanguageService()->sL( |
||
| 433 | self::LANG_FILE . 'administration.notify.recipients.' . CustomNotification::RECIPIENTS_WAITLIST_CONFIRMED |
||
| 434 | ), |
||
| 435 | ], |
||
| 436 | [ |
||
| 437 | 'value' => CustomNotification::RECIPIENTS_WAITLIST_UNCONFIRMED, |
||
| 438 | 'label' => $this->getLanguageService()->sL( |
||
| 439 | self::LANG_FILE . 'administration.notify.recipients.' . CustomNotification::RECIPIENTS_WAITLIST_UNCONFIRMED |
||
| 440 | ), |
||
| 441 | ], |
||
| 442 | ]; |
||
| 443 | } |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Notify action |
||
| 447 | */ |
||
| 448 | public function notifyAction(Event $event, CustomNotification $customNotification): ResponseInterface |
||
| 449 | { |
||
| 450 | if (!$this->checkEventAccess($event)) { |
||
| 451 | return $this->redirect('list'); |
||
| 452 | } |
||
| 453 | |||
| 454 | $customNotifications = $this->settingsService->getCustomNotifications($this->settings); |
||
| 455 | $result = $this->notificationService->sendCustomNotification($event, $customNotification, $this->settings); |
||
| 456 | $this->notificationService->createCustomNotificationLogentry( |
||
| 457 | $event, |
||
| 458 | $customNotifications[$customNotification->getTemplate()], |
||
| 459 | $result, |
||
| 460 | $customNotification |
||
| 461 | ); |
||
| 462 | $this->addFlashMessage( |
||
| 463 | $this->getLanguageService()->sL(self::LANG_FILE . 'administration.message-2.content'), |
||
| 464 | $this->getLanguageService()->sL(self::LANG_FILE . 'administration.message-2.title') |
||
| 465 | ); |
||
| 466 | return $this->redirect('list'); |
||
| 467 | } |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Checks if the current backend user has access to the PID of the event and if not, enqueue an |
||
| 471 | * access denied flash message |
||
| 472 | */ |
||
| 473 | public function checkEventAccess(Event $event): bool |
||
| 474 | { |
||
| 475 | if ($this->getBackendUser()->isInWebMount($event->getPid()) === null) { |
||
| 476 | $this->addFlashMessage( |
||
| 477 | $this->getLanguageService()->sL(self::LANG_FILE . 'administration.accessdenied.content'), |
||
| 478 | $this->getLanguageService()->sL(self::LANG_FILE . 'administration.accessdenied.title'), |
||
| 479 | ContextualFeedbackSeverity::ERROR |
||
| 480 | ); |
||
| 481 | return false; |
||
| 482 | } |
||
| 483 | |||
| 484 | return true; |
||
| 485 | } |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Shows the settings error view |
||
| 489 | */ |
||
| 490 | public function settingsErrorAction(): ResponseInterface |
||
| 491 | { |
||
| 492 | return $this->initModuleTemplateAndReturnResponse('Administration/SettingsError'); |
||
| 493 | } |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Suppress default validation messages |
||
| 497 | */ |
||
| 498 | protected function getErrorFlashMessage(): bool |
||
| 499 | { |
||
| 500 | return false; |
||
| 501 | } |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Returns an array with possible order directions |
||
| 505 | */ |
||
| 506 | public function getOrderDirections(): array |
||
| 507 | { |
||
| 508 | return [ |
||
| 509 | 'asc' => $this->getLanguageService()->sL(self::LANG_FILE . 'administration.sortOrder.asc'), |
||
| 510 | 'desc' => $this->getLanguageService()->sL(self::LANG_FILE . 'administration.sortOrder.desc'), |
||
| 511 | ]; |
||
| 512 | } |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Returns an array with possible recursive levels |
||
| 516 | */ |
||
| 517 | public function getRecursiveLevels(): array |
||
| 518 | { |
||
| 519 | return [ |
||
| 520 | $this->getLanguageService()->sL(self::LANG_FILE . 'administration.recursiveLevel.current'), |
||
| 521 | $this->getLanguageService()->sL(self::LANG_FILE . 'administration.recursiveLevel.level1'), |
||
| 522 | $this->getLanguageService()->sL(self::LANG_FILE . 'administration.recursiveLevel.level2'), |
||
| 523 | $this->getLanguageService()->sL(self::LANG_FILE . 'administration.recursiveLevel.level3'), |
||
| 524 | $this->getLanguageService()->sL(self::LANG_FILE . 'administration.recursiveLevel.level4'), |
||
| 525 | $this->getLanguageService()->sL(self::LANG_FILE . 'administration.recursiveLevel.level5'), |
||
| 526 | ]; |
||
| 527 | } |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Returns an array with possible orderBy fields |
||
| 531 | */ |
||
| 532 | public function getOrderByFields(): array |
||
| 533 | { |
||
| 534 | return [ |
||
| 535 | 'title' => $this->getLanguageService()->sL(self::LANG_FILE . 'administration.orderBy.title'), |
||
| 536 | 'startdate' => $this->getLanguageService()->sL(self::LANG_FILE . 'administration.orderBy.startdate'), |
||
| 537 | 'enddate' => $this->getLanguageService()->sL(self::LANG_FILE . 'administration.orderBy.enddate'), |
||
| 538 | ]; |
||
| 539 | } |
||
| 540 | |||
| 541 | protected function getLanguageService(): LanguageService |
||
| 544 | } |
||
| 545 | |||
| 546 | protected function getBackendUser(): BackendUserAuthentication |
||
| 547 | { |
||
| 548 | return $GLOBALS['BE_USER']; |
||
| 549 | } |
||
| 550 | } |
||
| 551 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths