Total Complexity | 51 |
Total Lines | 494 |
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 | $title = $this->getLanguageService()->sL(self::LANG_FILE . $tableConfiguration['label']); |
||
139 | $icon = $this->iconFactory->getIcon($tableConfiguration['icon'], Icon::SIZE_SMALL); |
||
140 | $viewButton = $buttonBar->makeLinkButton() |
||
141 | ->setHref($tableConfiguration['link']) |
||
142 | ->setDataAttributes([ |
||
143 | 'toggle' => 'tooltip', |
||
144 | 'placement' => 'bottom', |
||
145 | 'title' => $title, |
||
146 | ]) |
||
147 | ->setTitle($title) |
||
148 | ->setIcon($icon); |
||
149 | $buttonBar->addButton($viewButton, ButtonBar::BUTTON_POSITION_LEFT, $tableConfiguration['group']); |
||
150 | } |
||
151 | } |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * Returns the create new record URL for the given table |
||
156 | */ |
||
157 | private function getCreateNewRecordUri(string $table): string |
||
158 | { |
||
159 | $pid = $this->pid; |
||
160 | $tsConfig = BackendUtility::getPagesTSconfig(0); |
||
161 | if ($pid === 0 && isset($tsConfig['defaultPid.']) |
||
162 | && is_array($tsConfig['defaultPid.']) |
||
163 | && isset($tsConfig['defaultPid.'][$table]) |
||
164 | ) { |
||
165 | $pid = (int)$tsConfig['defaultPid.'][$table]; |
||
166 | } |
||
167 | |||
168 | $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class); |
||
169 | |||
170 | return (string)$uriBuilder->buildUriFromRoute('record_edit', [ |
||
171 | 'edit[' . $table . '][' . $pid . ']' => 'new', |
||
172 | 'returnUrl' => GeneralUtility::getIndpEnv('REQUEST_URI'), |
||
173 | ]); |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * Initializes module template and returns a response which must be used as response for any extbase action |
||
178 | * that should render a view. |
||
179 | */ |
||
180 | protected function initModuleTemplateAndReturnResponse(string $templateFileName, array $variables = []): ResponseInterface |
||
202 | } |
||
203 | |||
204 | public function initializeAction(): void |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * Set date format for fields startDate and endDate |
||
211 | */ |
||
212 | public function initializeListAction(): void |
||
217 | } |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * List action for backend module |
||
222 | */ |
||
223 | public function listAction(?SearchDemand $searchDemand = null, array $overwriteDemand = []): ResponseInterface |
||
295 | } |
||
296 | |||
297 | /** |
||
298 | * Resolves the page UIDs to search in respecting the given recursive option and additionally checking, if |
||
299 | * the current backend user is allowed to affected pages |
||
300 | */ |
||
301 | private function resolveSearchPageUids(int $recursive): string |
||
302 | { |
||
303 | $extendedPageUids = PageUtility::extendPidListByChildren( |
||
304 | (string)$this->pid, |
||
305 | $recursive |
||
306 | ); |
||
307 | $extendedPageUids = GeneralUtility::intExplode(',', $extendedPageUids, true); |
||
308 | |||
309 | $pageUids = []; |
||
310 | foreach ($extendedPageUids as $extendedPageUid) { |
||
311 | if (!in_array($extendedPageUid, $pageUids, true) && |
||
312 | $this->getBackendUser()->isInWebMount($extendedPageUid) |
||
313 | ) { |
||
314 | $pageUids[] = $extendedPageUid; |
||
315 | } |
||
316 | } |
||
317 | |||
318 | return implode(',', $pageUids); |
||
319 | } |
||
320 | |||
321 | /** |
||
322 | * Returns, if reset filter operation has been used |
||
323 | */ |
||
324 | private function isResetFilter(): bool |
||
325 | { |
||
326 | $resetFilter = false; |
||
327 | if ($this->request->hasArgument('operation')) { |
||
328 | $resetFilter = $this->request->getArgument('operation') === 'reset-filters'; |
||
329 | } |
||
330 | |||
331 | return $resetFilter; |
||
332 | } |
||
333 | |||
334 | /** |
||
335 | * Export registrations for a given event |
||
336 | */ |
||
337 | public function exportAction(int $eventUid): void |
||
338 | { |
||
339 | /** @var Event $event */ |
||
340 | $event = $this->eventRepository->findByUidIncludeHidden($eventUid); |
||
341 | if ($event !== null && $this->checkEventAccess($event)) { |
||
342 | $this->exportService->downloadRegistrationsCsv($eventUid, $this->settings['csvExport'] ?? []); |
||
343 | } |
||
344 | exit(); |
||
345 | } |
||
346 | |||
347 | /** |
||
348 | * Handles expired registrations |
||
349 | */ |
||
350 | public function handleExpiredRegistrationsAction(): ResponseInterface |
||
351 | { |
||
352 | $delete = (bool)($this->settings['registration']['deleteExpiredRegistrations'] ?? false); |
||
353 | $this->maintenanceService->handleExpiredRegistrations($delete); |
||
354 | |||
355 | $this->addFlashMessage( |
||
356 | $this->getLanguageService()->sL(self::LANG_FILE . 'administration.message-1.content'), |
||
357 | $this->getLanguageService()->sL(self::LANG_FILE . 'administration.message-1.title') |
||
358 | ); |
||
359 | |||
360 | return $this->redirect('list'); |
||
361 | } |
||
362 | |||
363 | /** |
||
364 | * The index notify action |
||
365 | */ |
||
366 | public function indexNotifyAction(Event $event): ResponseInterface |
||
367 | { |
||
368 | if (!$this->checkEventAccess($event)) { |
||
369 | return $this->redirect('list'); |
||
370 | } |
||
371 | |||
372 | $customNotification = GeneralUtility::makeInstance(CustomNotification::class); |
||
373 | $customNotifications = $this->settingsService->getCustomNotifications($this->settings); |
||
374 | $logEntries = $this->customNotificationLogRepository->findBy(['event' => $event]); |
||
375 | |||
376 | $modifyAdministrationIndexNotifyViewVariablesEvent = new ModifyAdministrationIndexNotifyViewVariablesEvent( |
||
377 | [ |
||
378 | 'event' => $event, |
||
379 | 'recipients' => $this->getNotificationRecipients(), |
||
380 | 'customNotification' => $customNotification, |
||
381 | 'customNotifications' => $customNotifications, |
||
382 | 'logEntries' => $logEntries, |
||
383 | ], |
||
384 | $this |
||
385 | ); |
||
386 | $this->eventDispatcher->dispatch($modifyAdministrationIndexNotifyViewVariablesEvent); |
||
387 | $variables = $modifyAdministrationIndexNotifyViewVariablesEvent->getVariables(); |
||
388 | |||
389 | return $this->initModuleTemplateAndReturnResponse('Administration/IndexNotify', $variables); |
||
390 | } |
||
391 | |||
392 | /** |
||
393 | * Returns an array of recipient option for the indexNotify action |
||
394 | */ |
||
395 | public function getNotificationRecipients(): array |
||
396 | { |
||
397 | return [ |
||
398 | [ |
||
399 | 'value' => CustomNotification::RECIPIENTS_ALL, |
||
400 | 'label' => $this->getLanguageService()->sL( |
||
401 | self::LANG_FILE . 'administration.notify.recipients.' . CustomNotification::RECIPIENTS_ALL |
||
402 | ), |
||
403 | ], |
||
404 | [ |
||
405 | 'value' => CustomNotification::RECIPIENTS_CONFIRMED, |
||
406 | 'label' => $this->getLanguageService()->sL( |
||
407 | self::LANG_FILE . 'administration.notify.recipients.' . CustomNotification::RECIPIENTS_CONFIRMED |
||
408 | ), |
||
409 | ], |
||
410 | [ |
||
411 | 'value' => CustomNotification::RECIPIENTS_UNCONFIRMED, |
||
412 | 'label' => $this->getLanguageService()->sL( |
||
413 | self::LANG_FILE . 'administration.notify.recipients.' . CustomNotification::RECIPIENTS_UNCONFIRMED |
||
414 | ), |
||
415 | ], |
||
416 | [ |
||
417 | 'value' => CustomNotification::RECIPIENTS_WAITLIST_CONFIRMED, |
||
418 | 'label' => $this->getLanguageService()->sL( |
||
419 | self::LANG_FILE . 'administration.notify.recipients.' . CustomNotification::RECIPIENTS_WAITLIST_CONFIRMED |
||
420 | ), |
||
421 | ], |
||
422 | [ |
||
423 | 'value' => CustomNotification::RECIPIENTS_WAITLIST_UNCONFIRMED, |
||
424 | 'label' => $this->getLanguageService()->sL( |
||
425 | self::LANG_FILE . 'administration.notify.recipients.' . CustomNotification::RECIPIENTS_WAITLIST_UNCONFIRMED |
||
426 | ), |
||
427 | ], |
||
428 | ]; |
||
429 | } |
||
430 | |||
431 | /** |
||
432 | * Notify action |
||
433 | */ |
||
434 | public function notifyAction(Event $event, CustomNotification $customNotification): ResponseInterface |
||
435 | { |
||
436 | if (!$this->checkEventAccess($event)) { |
||
437 | return $this->redirect('list'); |
||
438 | } |
||
439 | |||
440 | $customNotifications = $this->settingsService->getCustomNotifications($this->settings); |
||
441 | $result = $this->notificationService->sendCustomNotification($event, $customNotification, $this->settings); |
||
442 | $this->notificationService->createCustomNotificationLogentry( |
||
443 | $event, |
||
444 | $customNotifications[$customNotification->getTemplate()], |
||
445 | $result, |
||
446 | $customNotification |
||
447 | ); |
||
448 | $this->addFlashMessage( |
||
449 | $this->getLanguageService()->sL(self::LANG_FILE . 'administration.message-2.content'), |
||
450 | $this->getLanguageService()->sL(self::LANG_FILE . 'administration.message-2.title') |
||
451 | ); |
||
452 | return $this->redirect('list'); |
||
453 | } |
||
454 | |||
455 | /** |
||
456 | * Checks if the current backend user has access to the PID of the event and if not, enqueue an |
||
457 | * access denied flash message |
||
458 | */ |
||
459 | public function checkEventAccess(Event $event): bool |
||
460 | { |
||
461 | if ($this->getBackendUser()->isInWebMount($event->getPid()) === null) { |
||
462 | $this->addFlashMessage( |
||
463 | $this->getLanguageService()->sL(self::LANG_FILE . 'administration.accessdenied.content'), |
||
464 | $this->getLanguageService()->sL(self::LANG_FILE . 'administration.accessdenied.title'), |
||
465 | ContextualFeedbackSeverity::ERROR |
||
466 | ); |
||
467 | return false; |
||
468 | } |
||
469 | |||
470 | return true; |
||
471 | } |
||
472 | |||
473 | /** |
||
474 | * Shows the settings error view |
||
475 | */ |
||
476 | public function settingsErrorAction(): ResponseInterface |
||
477 | { |
||
478 | return $this->initModuleTemplateAndReturnResponse('Administration/SettingsError'); |
||
479 | } |
||
480 | |||
481 | /** |
||
482 | * Suppress default validation messages |
||
483 | */ |
||
484 | protected function getErrorFlashMessage(): bool |
||
485 | { |
||
486 | return false; |
||
487 | } |
||
488 | |||
489 | /** |
||
490 | * Returns an array with possible order directions |
||
491 | */ |
||
492 | public function getOrderDirections(): array |
||
493 | { |
||
494 | return [ |
||
495 | 'asc' => $this->getLanguageService()->sL(self::LANG_FILE . 'administration.sortOrder.asc'), |
||
496 | 'desc' => $this->getLanguageService()->sL(self::LANG_FILE . 'administration.sortOrder.desc'), |
||
497 | ]; |
||
498 | } |
||
499 | |||
500 | /** |
||
501 | * Returns an array with possible recursive levels |
||
502 | */ |
||
503 | public function getRecursiveLevels(): array |
||
504 | { |
||
505 | return [ |
||
506 | $this->getLanguageService()->sL(self::LANG_FILE . 'administration.recursiveLevel.current'), |
||
507 | $this->getLanguageService()->sL(self::LANG_FILE . 'administration.recursiveLevel.level1'), |
||
508 | $this->getLanguageService()->sL(self::LANG_FILE . 'administration.recursiveLevel.level2'), |
||
509 | $this->getLanguageService()->sL(self::LANG_FILE . 'administration.recursiveLevel.level3'), |
||
510 | $this->getLanguageService()->sL(self::LANG_FILE . 'administration.recursiveLevel.level4'), |
||
511 | $this->getLanguageService()->sL(self::LANG_FILE . 'administration.recursiveLevel.level5'), |
||
512 | ]; |
||
513 | } |
||
514 | |||
515 | /** |
||
516 | * Returns an array with possible orderBy fields |
||
517 | */ |
||
518 | public function getOrderByFields(): array |
||
519 | { |
||
520 | return [ |
||
521 | 'title' => $this->getLanguageService()->sL(self::LANG_FILE . 'administration.orderBy.title'), |
||
522 | 'startdate' => $this->getLanguageService()->sL(self::LANG_FILE . 'administration.orderBy.startdate'), |
||
523 | 'enddate' => $this->getLanguageService()->sL(self::LANG_FILE . 'administration.orderBy.enddate'), |
||
524 | ]; |
||
525 | } |
||
526 | |||
527 | protected function getLanguageService(): LanguageService |
||
530 | } |
||
531 | |||
532 | protected function getBackendUser(): BackendUserAuthentication |
||
533 | { |
||
534 | return $GLOBALS['BE_USER']; |
||
535 | } |
||
536 | } |
||
537 |
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