Total Complexity | 54 |
Total Lines | 516 |
Duplicated Lines | 0 % |
Changes | 3 | ||
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 |
||
87 | { |
||
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'], IconSize::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 |
||
162 | { |
||
163 | $disabledButtons = $this->settings['disableButtons'] ?? []; |
||
164 | $buttonIdentifier = str_replace('administration.', '', $buttonLabel); |
||
165 | return isset($disabledButtons[$buttonIdentifier]) && (int)($disabledButtons[$buttonIdentifier]) === 1; |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Returns the create new record URL for the given table |
||
170 | */ |
||
171 | private function getCreateNewRecordUri(string $table): string |
||
172 | { |
||
173 | $pid = $this->pid; |
||
174 | $tsConfig = BackendUtility::getPagesTSconfig(0); |
||
175 | if ($pid === 0 && isset($tsConfig['defaultPid.']) |
||
176 | && is_array($tsConfig['defaultPid.']) |
||
177 | && isset($tsConfig['defaultPid.'][$table]) |
||
178 | ) { |
||
179 | $pid = (int)$tsConfig['defaultPid.'][$table]; |
||
180 | } |
||
181 | |||
182 | $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class); |
||
183 | |||
184 | return (string)$uriBuilder->buildUriFromRoute('record_edit', [ |
||
185 | 'edit[' . $table . '][' . $pid . ']' => 'new', |
||
186 | 'returnUrl' => GeneralUtility::getIndpEnv('REQUEST_URI'), |
||
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 | $this->request |
||
210 | ); |
||
211 | $this->eventDispatcher->dispatch($initAdministrationModuleTemplateEvent); |
||
212 | |||
213 | $variables['settings'] = $this->settings; |
||
214 | $moduleTemplate->assignMultiple($variables); |
||
215 | |||
216 | return $moduleTemplate->renderResponse($templateFileName); |
||
217 | } |
||
218 | |||
219 | public function initializeAction(): void |
||
220 | { |
||
221 | $this->pid = (int)($this->request->getQueryParams()['id'] ?? 0); |
||
222 | } |
||
223 | |||
224 | /** |
||
225 | * Set date format for fields startDate and endDate |
||
226 | */ |
||
227 | public function initializeListAction(): void |
||
228 | { |
||
229 | // Static format needed for date picker (flatpickr), see BackendController::generateJavascript() and #91606 |
||
230 | if (!empty($this->settings)) { |
||
231 | $this->settings['search']['dateFormat'] = 'H:i d-m-Y'; |
||
232 | } |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * List action for backend module |
||
237 | */ |
||
238 | public function listAction(?SearchDemand $searchDemand = null, array $overwriteDemand = []): ResponseInterface |
||
311 | } |
||
312 | |||
313 | /** |
||
314 | * Resolves the page UIDs to search in respecting the given recursive option and additionally checking, if |
||
315 | * the current backend user is allowed to affected pages |
||
316 | */ |
||
317 | private function resolveSearchPageUids(int $recursive): string |
||
318 | { |
||
319 | $extendedPageUids = PageUtility::extendPidListByChildren( |
||
320 | (string)$this->pid, |
||
321 | $recursive |
||
322 | ); |
||
323 | $extendedPageUids = GeneralUtility::intExplode(',', $extendedPageUids, true); |
||
324 | |||
325 | $pageUids = []; |
||
326 | foreach ($extendedPageUids as $extendedPageUid) { |
||
327 | if (!in_array($extendedPageUid, $pageUids, true) && |
||
328 | $this->getBackendUser()->isInWebMount($extendedPageUid) |
||
329 | ) { |
||
330 | $pageUids[] = $extendedPageUid; |
||
331 | } |
||
332 | } |
||
333 | |||
334 | return implode(',', $pageUids); |
||
335 | } |
||
336 | |||
337 | /** |
||
338 | * Returns, if reset filter operation has been used |
||
339 | */ |
||
340 | private function isResetFilter(): bool |
||
341 | { |
||
342 | $resetFilter = false; |
||
343 | if ($this->request->hasArgument('operation')) { |
||
344 | $resetFilter = $this->request->getArgument('operation') === 'reset-filters'; |
||
345 | } |
||
346 | |||
347 | return $resetFilter; |
||
348 | } |
||
349 | |||
350 | /** |
||
351 | * Export registrations for a given event |
||
352 | */ |
||
353 | public function exportAction(int $eventUid): void |
||
354 | { |
||
355 | /** @var Event $event */ |
||
356 | $event = $this->eventRepository->findByUidIncludeHidden($eventUid); |
||
357 | if ($event !== null && $this->checkEventAccess($event)) { |
||
358 | $this->exportService->downloadRegistrationsCsv($eventUid, $this->settings['csvExport'] ?? []); |
||
359 | } |
||
360 | exit(); |
||
361 | } |
||
362 | |||
363 | /** |
||
364 | * Handles expired registrations |
||
365 | */ |
||
366 | public function handleExpiredRegistrationsAction(): ResponseInterface |
||
367 | { |
||
368 | $delete = (bool)($this->settings['registration']['deleteExpiredRegistrations'] ?? false); |
||
369 | $this->maintenanceService->handleExpiredRegistrations($delete); |
||
370 | |||
371 | $this->addFlashMessage( |
||
372 | $this->getLanguageService()->sL(self::LANG_FILE . 'administration.message-1.content'), |
||
373 | $this->getLanguageService()->sL(self::LANG_FILE . 'administration.message-1.title') |
||
374 | ); |
||
375 | |||
376 | return $this->redirect('list'); |
||
377 | } |
||
378 | |||
379 | /** |
||
380 | * The index notify action |
||
381 | */ |
||
382 | public function indexNotifyAction(Event $event): ResponseInterface |
||
383 | { |
||
384 | if (!$this->checkEventAccess($event)) { |
||
385 | return $this->redirect('list'); |
||
386 | } |
||
387 | |||
388 | $customNotification = GeneralUtility::makeInstance(CustomNotification::class); |
||
389 | $customNotifications = $this->settingsService->getCustomNotifications($this->settings); |
||
390 | $logEntries = $this->customNotificationLogRepository->findBy(['event' => $event]); |
||
391 | |||
392 | $modifyAdministrationIndexNotifyViewVariablesEvent = new ModifyAdministrationIndexNotifyViewVariablesEvent( |
||
393 | [ |
||
394 | 'event' => $event, |
||
395 | 'recipients' => $this->getNotificationRecipients(), |
||
396 | 'customNotification' => $customNotification, |
||
397 | 'customNotifications' => $customNotifications, |
||
398 | 'logEntries' => $logEntries, |
||
399 | ], |
||
400 | $this, |
||
401 | $this->request |
||
402 | ); |
||
403 | $this->eventDispatcher->dispatch($modifyAdministrationIndexNotifyViewVariablesEvent); |
||
404 | $variables = $modifyAdministrationIndexNotifyViewVariablesEvent->getVariables(); |
||
405 | |||
406 | return $this->initModuleTemplateAndReturnResponse('Administration/IndexNotify', $variables); |
||
407 | } |
||
408 | |||
409 | /** |
||
410 | * Returns an array of recipient option for the indexNotify action |
||
411 | */ |
||
412 | public function getNotificationRecipients(): array |
||
413 | { |
||
414 | return [ |
||
415 | [ |
||
416 | 'value' => CustomNotification::RECIPIENTS_ALL, |
||
417 | 'label' => $this->getLanguageService()->sL( |
||
418 | self::LANG_FILE . 'administration.notify.recipients.' . CustomNotification::RECIPIENTS_ALL |
||
419 | ), |
||
420 | ], |
||
421 | [ |
||
422 | 'value' => CustomNotification::RECIPIENTS_CONFIRMED, |
||
423 | 'label' => $this->getLanguageService()->sL( |
||
424 | self::LANG_FILE . 'administration.notify.recipients.' . CustomNotification::RECIPIENTS_CONFIRMED |
||
425 | ), |
||
426 | ], |
||
427 | [ |
||
428 | 'value' => CustomNotification::RECIPIENTS_UNCONFIRMED, |
||
429 | 'label' => $this->getLanguageService()->sL( |
||
430 | self::LANG_FILE . 'administration.notify.recipients.' . CustomNotification::RECIPIENTS_UNCONFIRMED |
||
431 | ), |
||
432 | ], |
||
433 | [ |
||
434 | 'value' => CustomNotification::RECIPIENTS_WAITLIST_CONFIRMED, |
||
435 | 'label' => $this->getLanguageService()->sL( |
||
436 | self::LANG_FILE . 'administration.notify.recipients.' . CustomNotification::RECIPIENTS_WAITLIST_CONFIRMED |
||
437 | ), |
||
438 | ], |
||
439 | [ |
||
440 | 'value' => CustomNotification::RECIPIENTS_WAITLIST_UNCONFIRMED, |
||
441 | 'label' => $this->getLanguageService()->sL( |
||
442 | self::LANG_FILE . 'administration.notify.recipients.' . CustomNotification::RECIPIENTS_WAITLIST_UNCONFIRMED |
||
443 | ), |
||
444 | ], |
||
445 | ]; |
||
446 | } |
||
447 | |||
448 | /** |
||
449 | * Notify action |
||
450 | */ |
||
451 | public function notifyAction(Event $event, CustomNotification $customNotification): ResponseInterface |
||
452 | { |
||
453 | if (!$this->checkEventAccess($event)) { |
||
454 | return $this->redirect('list'); |
||
455 | } |
||
456 | |||
457 | $customNotifications = $this->settingsService->getCustomNotifications($this->settings); |
||
458 | $result = $this->notificationService->sendCustomNotification( |
||
459 | $this->request, |
||
460 | $event, |
||
461 | $customNotification, |
||
462 | $this->settings |
||
463 | ); |
||
464 | $this->notificationService->createCustomNotificationLogentry( |
||
465 | $event, |
||
466 | $customNotifications[$customNotification->getTemplate()], |
||
467 | $result, |
||
468 | $customNotification |
||
469 | ); |
||
470 | $this->addFlashMessage( |
||
471 | $this->getLanguageService()->sL(self::LANG_FILE . 'administration.message-2.content'), |
||
472 | $this->getLanguageService()->sL(self::LANG_FILE . 'administration.message-2.title') |
||
473 | ); |
||
474 | return $this->redirect('list'); |
||
475 | } |
||
476 | |||
477 | /** |
||
478 | * Checks if the current backend user has access to the PID of the event and if not, enqueue an |
||
479 | * access denied flash message |
||
480 | */ |
||
481 | public function checkEventAccess(Event $event): bool |
||
482 | { |
||
483 | if ($this->getBackendUser()->isInWebMount($event->getPid()) === null) { |
||
484 | $this->addFlashMessage( |
||
485 | $this->getLanguageService()->sL(self::LANG_FILE . 'administration.accessdenied.content'), |
||
486 | $this->getLanguageService()->sL(self::LANG_FILE . 'administration.accessdenied.title'), |
||
487 | ContextualFeedbackSeverity::ERROR |
||
488 | ); |
||
489 | return false; |
||
490 | } |
||
491 | |||
492 | return true; |
||
493 | } |
||
494 | |||
495 | /** |
||
496 | * Shows the settings error view |
||
497 | */ |
||
498 | public function settingsErrorAction(): ResponseInterface |
||
499 | { |
||
500 | return $this->initModuleTemplateAndReturnResponse('Administration/SettingsError'); |
||
501 | } |
||
502 | |||
503 | /** |
||
504 | * Suppress default validation messages |
||
505 | */ |
||
506 | protected function getErrorFlashMessage(): bool |
||
507 | { |
||
508 | return false; |
||
509 | } |
||
510 | |||
511 | /** |
||
512 | * Returns an array with possible order directions |
||
513 | */ |
||
514 | public function getOrderDirections(): array |
||
515 | { |
||
516 | return [ |
||
517 | 'asc' => $this->getLanguageService()->sL(self::LANG_FILE . 'administration.sortOrder.asc'), |
||
518 | 'desc' => $this->getLanguageService()->sL(self::LANG_FILE . 'administration.sortOrder.desc'), |
||
519 | ]; |
||
520 | } |
||
521 | |||
522 | /** |
||
523 | * Returns an array with possible recursive levels |
||
524 | */ |
||
525 | public function getRecursiveLevels(): array |
||
526 | { |
||
527 | return [ |
||
528 | $this->getLanguageService()->sL(self::LANG_FILE . 'administration.recursiveLevel.current'), |
||
529 | $this->getLanguageService()->sL(self::LANG_FILE . 'administration.recursiveLevel.level1'), |
||
530 | $this->getLanguageService()->sL(self::LANG_FILE . 'administration.recursiveLevel.level2'), |
||
531 | $this->getLanguageService()->sL(self::LANG_FILE . 'administration.recursiveLevel.level3'), |
||
532 | $this->getLanguageService()->sL(self::LANG_FILE . 'administration.recursiveLevel.level4'), |
||
533 | $this->getLanguageService()->sL(self::LANG_FILE . 'administration.recursiveLevel.level5'), |
||
534 | ]; |
||
535 | } |
||
536 | |||
537 | /** |
||
538 | * Returns an array with possible orderBy fields |
||
539 | */ |
||
540 | public function getOrderByFields(): array |
||
541 | { |
||
542 | return [ |
||
543 | 'title' => $this->getLanguageService()->sL(self::LANG_FILE . 'administration.orderBy.title'), |
||
544 | 'startdate' => $this->getLanguageService()->sL(self::LANG_FILE . 'administration.orderBy.startdate'), |
||
545 | 'enddate' => $this->getLanguageService()->sL(self::LANG_FILE . 'administration.orderBy.enddate'), |
||
546 | ]; |
||
547 | } |
||
548 | |||
549 | protected function getLanguageService(): LanguageService |
||
552 | } |
||
553 | |||
554 | protected function getBackendUser(): BackendUserAuthentication |
||
555 | { |
||
556 | return $GLOBALS['BE_USER']; |
||
557 | } |
||
558 | } |
||
559 |
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