Total Complexity | 51 |
Total Lines | 502 |
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 | { |
||
88 | $this->moduleTemplateFactory = $moduleTemplateFactory; |
||
89 | } |
||
90 | |||
91 | public function injectPageRenderer(PageRenderer $pageRenderer): void |
||
92 | { |
||
93 | $this->pageRenderer = $pageRenderer; |
||
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'], IconSize::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 |
||
181 | { |
||
182 | $moduleTemplate = $this->moduleTemplateFactory->create($this->request); |
||
183 | $this->pageRenderer->addCssFile('EXT:sf_event_mgt/Resources/Public/Css/administration.css'); |
||
184 | |||
185 | $this->pageRenderer->loadJavaScriptModule('@derhansen/sf_event_mgt/administration-module.js'); |
||
186 | |||
187 | $this->registerDocHeaderButtons($moduleTemplate); |
||
188 | |||
189 | $moduleTemplate->setFlashMessageQueue($this->getFlashMessageQueue()); |
||
190 | |||
191 | $initAdministrationModuleTemplateEvent = new InitAdministrationModuleTemplateEvent( |
||
192 | $moduleTemplate, |
||
193 | $this->uriBuilder, |
||
194 | $this, |
||
195 | $this->request |
||
196 | ); |
||
197 | $this->eventDispatcher->dispatch($initAdministrationModuleTemplateEvent); |
||
198 | |||
199 | $variables['settings'] = $this->settings; |
||
200 | $moduleTemplate->assignMultiple($variables); |
||
201 | |||
202 | return $moduleTemplate->renderResponse($templateFileName); |
||
203 | } |
||
204 | |||
205 | public function initializeAction(): void |
||
206 | { |
||
207 | $this->pid = (int)($this->request->getQueryParams()['id'] ?? 0); |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * Set date format for fields startDate and endDate |
||
212 | */ |
||
213 | public function initializeListAction(): void |
||
214 | { |
||
215 | // Static format needed for date picker (flatpickr), see BackendController::generateJavascript() and #91606 |
||
216 | if (!empty($this->settings)) { |
||
217 | $this->settings['search']['dateFormat'] = 'H:i d-m-Y'; |
||
218 | } |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * List action for backend module |
||
223 | */ |
||
224 | public function listAction(?SearchDemand $searchDemand = null, array $overwriteDemand = []): ResponseInterface |
||
297 | } |
||
298 | |||
299 | /** |
||
300 | * Resolves the page UIDs to search in respecting the given recursive option and additionally checking, if |
||
301 | * the current backend user is allowed to affected pages |
||
302 | */ |
||
303 | private function resolveSearchPageUids(int $recursive): string |
||
304 | { |
||
305 | $extendedPageUids = PageUtility::extendPidListByChildren( |
||
306 | (string)$this->pid, |
||
307 | $recursive |
||
308 | ); |
||
309 | $extendedPageUids = GeneralUtility::intExplode(',', $extendedPageUids, true); |
||
310 | |||
311 | $pageUids = []; |
||
312 | foreach ($extendedPageUids as $extendedPageUid) { |
||
313 | if (!in_array($extendedPageUid, $pageUids, true) && |
||
314 | $this->getBackendUser()->isInWebMount($extendedPageUid) |
||
315 | ) { |
||
316 | $pageUids[] = $extendedPageUid; |
||
317 | } |
||
318 | } |
||
319 | |||
320 | return implode(',', $pageUids); |
||
321 | } |
||
322 | |||
323 | /** |
||
324 | * Returns, if reset filter operation has been used |
||
325 | */ |
||
326 | private function isResetFilter(): bool |
||
327 | { |
||
328 | $resetFilter = false; |
||
329 | if ($this->request->hasArgument('operation')) { |
||
330 | $resetFilter = $this->request->getArgument('operation') === 'reset-filters'; |
||
331 | } |
||
332 | |||
333 | return $resetFilter; |
||
334 | } |
||
335 | |||
336 | /** |
||
337 | * Export registrations for a given event |
||
338 | */ |
||
339 | public function exportAction(int $eventUid): void |
||
340 | { |
||
341 | /** @var Event $event */ |
||
342 | $event = $this->eventRepository->findByUidIncludeHidden($eventUid); |
||
343 | if ($event !== null && $this->checkEventAccess($event)) { |
||
344 | $this->exportService->downloadRegistrationsCsv($eventUid, $this->settings['csvExport'] ?? []); |
||
345 | } |
||
346 | exit(); |
||
347 | } |
||
348 | |||
349 | /** |
||
350 | * Handles expired registrations |
||
351 | */ |
||
352 | public function handleExpiredRegistrationsAction(): ResponseInterface |
||
353 | { |
||
354 | $delete = (bool)($this->settings['registration']['deleteExpiredRegistrations'] ?? false); |
||
355 | $this->maintenanceService->handleExpiredRegistrations($delete); |
||
356 | |||
357 | $this->addFlashMessage( |
||
358 | $this->getLanguageService()->sL(self::LANG_FILE . 'administration.message-1.content'), |
||
359 | $this->getLanguageService()->sL(self::LANG_FILE . 'administration.message-1.title') |
||
360 | ); |
||
361 | |||
362 | return $this->redirect('list'); |
||
363 | } |
||
364 | |||
365 | /** |
||
366 | * The index notify action |
||
367 | */ |
||
368 | public function indexNotifyAction(Event $event): ResponseInterface |
||
369 | { |
||
370 | if (!$this->checkEventAccess($event)) { |
||
371 | return $this->redirect('list'); |
||
372 | } |
||
373 | |||
374 | $customNotification = GeneralUtility::makeInstance(CustomNotification::class); |
||
375 | $customNotifications = $this->settingsService->getCustomNotifications($this->settings); |
||
376 | $logEntries = $this->customNotificationLogRepository->findBy(['event' => $event]); |
||
377 | |||
378 | $modifyAdministrationIndexNotifyViewVariablesEvent = new ModifyAdministrationIndexNotifyViewVariablesEvent( |
||
379 | [ |
||
380 | 'event' => $event, |
||
381 | 'recipients' => $this->getNotificationRecipients(), |
||
382 | 'customNotification' => $customNotification, |
||
383 | 'customNotifications' => $customNotifications, |
||
384 | 'logEntries' => $logEntries, |
||
385 | ], |
||
386 | $this, |
||
387 | $this->request |
||
388 | ); |
||
389 | $this->eventDispatcher->dispatch($modifyAdministrationIndexNotifyViewVariablesEvent); |
||
390 | $variables = $modifyAdministrationIndexNotifyViewVariablesEvent->getVariables(); |
||
391 | |||
392 | return $this->initModuleTemplateAndReturnResponse('Administration/IndexNotify', $variables); |
||
393 | } |
||
394 | |||
395 | /** |
||
396 | * Returns an array of recipient option for the indexNotify action |
||
397 | */ |
||
398 | public function getNotificationRecipients(): array |
||
399 | { |
||
400 | return [ |
||
401 | [ |
||
402 | 'value' => CustomNotification::RECIPIENTS_ALL, |
||
403 | 'label' => $this->getLanguageService()->sL( |
||
404 | self::LANG_FILE . 'administration.notify.recipients.' . CustomNotification::RECIPIENTS_ALL |
||
405 | ), |
||
406 | ], |
||
407 | [ |
||
408 | 'value' => CustomNotification::RECIPIENTS_CONFIRMED, |
||
409 | 'label' => $this->getLanguageService()->sL( |
||
410 | self::LANG_FILE . 'administration.notify.recipients.' . CustomNotification::RECIPIENTS_CONFIRMED |
||
411 | ), |
||
412 | ], |
||
413 | [ |
||
414 | 'value' => CustomNotification::RECIPIENTS_UNCONFIRMED, |
||
415 | 'label' => $this->getLanguageService()->sL( |
||
416 | self::LANG_FILE . 'administration.notify.recipients.' . CustomNotification::RECIPIENTS_UNCONFIRMED |
||
417 | ), |
||
418 | ], |
||
419 | [ |
||
420 | 'value' => CustomNotification::RECIPIENTS_WAITLIST_CONFIRMED, |
||
421 | 'label' => $this->getLanguageService()->sL( |
||
422 | self::LANG_FILE . 'administration.notify.recipients.' . CustomNotification::RECIPIENTS_WAITLIST_CONFIRMED |
||
423 | ), |
||
424 | ], |
||
425 | [ |
||
426 | 'value' => CustomNotification::RECIPIENTS_WAITLIST_UNCONFIRMED, |
||
427 | 'label' => $this->getLanguageService()->sL( |
||
428 | self::LANG_FILE . 'administration.notify.recipients.' . CustomNotification::RECIPIENTS_WAITLIST_UNCONFIRMED |
||
429 | ), |
||
430 | ], |
||
431 | ]; |
||
432 | } |
||
433 | |||
434 | /** |
||
435 | * Notify action |
||
436 | */ |
||
437 | public function notifyAction(Event $event, CustomNotification $customNotification): ResponseInterface |
||
438 | { |
||
439 | if (!$this->checkEventAccess($event)) { |
||
440 | return $this->redirect('list'); |
||
441 | } |
||
442 | |||
443 | $customNotifications = $this->settingsService->getCustomNotifications($this->settings); |
||
444 | $result = $this->notificationService->sendCustomNotification( |
||
445 | $this->request, |
||
446 | $event, |
||
447 | $customNotification, |
||
448 | $this->settings |
||
449 | ); |
||
450 | $this->notificationService->createCustomNotificationLogentry( |
||
451 | $event, |
||
452 | $customNotifications[$customNotification->getTemplate()], |
||
453 | $result, |
||
454 | $customNotification |
||
455 | ); |
||
456 | $this->addFlashMessage( |
||
457 | $this->getLanguageService()->sL(self::LANG_FILE . 'administration.message-2.content'), |
||
458 | $this->getLanguageService()->sL(self::LANG_FILE . 'administration.message-2.title') |
||
459 | ); |
||
460 | return $this->redirect('list'); |
||
461 | } |
||
462 | |||
463 | /** |
||
464 | * Checks if the current backend user has access to the PID of the event and if not, enqueue an |
||
465 | * access denied flash message |
||
466 | */ |
||
467 | public function checkEventAccess(Event $event): bool |
||
468 | { |
||
469 | if ($this->getBackendUser()->isInWebMount($event->getPid()) === null) { |
||
470 | $this->addFlashMessage( |
||
471 | $this->getLanguageService()->sL(self::LANG_FILE . 'administration.accessdenied.content'), |
||
472 | $this->getLanguageService()->sL(self::LANG_FILE . 'administration.accessdenied.title'), |
||
473 | ContextualFeedbackSeverity::ERROR |
||
474 | ); |
||
475 | return false; |
||
476 | } |
||
477 | |||
478 | return true; |
||
479 | } |
||
480 | |||
481 | /** |
||
482 | * Shows the settings error view |
||
483 | */ |
||
484 | public function settingsErrorAction(): ResponseInterface |
||
485 | { |
||
486 | return $this->initModuleTemplateAndReturnResponse('Administration/SettingsError'); |
||
487 | } |
||
488 | |||
489 | /** |
||
490 | * Suppress default validation messages |
||
491 | */ |
||
492 | protected function getErrorFlashMessage(): bool |
||
493 | { |
||
494 | return false; |
||
495 | } |
||
496 | |||
497 | /** |
||
498 | * Returns an array with possible order directions |
||
499 | */ |
||
500 | public function getOrderDirections(): array |
||
501 | { |
||
502 | return [ |
||
503 | 'asc' => $this->getLanguageService()->sL(self::LANG_FILE . 'administration.sortOrder.asc'), |
||
504 | 'desc' => $this->getLanguageService()->sL(self::LANG_FILE . 'administration.sortOrder.desc'), |
||
505 | ]; |
||
506 | } |
||
507 | |||
508 | /** |
||
509 | * Returns an array with possible recursive levels |
||
510 | */ |
||
511 | public function getRecursiveLevels(): array |
||
512 | { |
||
513 | return [ |
||
514 | $this->getLanguageService()->sL(self::LANG_FILE . 'administration.recursiveLevel.current'), |
||
515 | $this->getLanguageService()->sL(self::LANG_FILE . 'administration.recursiveLevel.level1'), |
||
516 | $this->getLanguageService()->sL(self::LANG_FILE . 'administration.recursiveLevel.level2'), |
||
517 | $this->getLanguageService()->sL(self::LANG_FILE . 'administration.recursiveLevel.level3'), |
||
518 | $this->getLanguageService()->sL(self::LANG_FILE . 'administration.recursiveLevel.level4'), |
||
519 | $this->getLanguageService()->sL(self::LANG_FILE . 'administration.recursiveLevel.level5'), |
||
520 | ]; |
||
521 | } |
||
522 | |||
523 | /** |
||
524 | * Returns an array with possible orderBy fields |
||
525 | */ |
||
526 | public function getOrderByFields(): array |
||
527 | { |
||
528 | return [ |
||
529 | 'title' => $this->getLanguageService()->sL(self::LANG_FILE . 'administration.orderBy.title'), |
||
530 | 'startdate' => $this->getLanguageService()->sL(self::LANG_FILE . 'administration.orderBy.startdate'), |
||
531 | 'enddate' => $this->getLanguageService()->sL(self::LANG_FILE . 'administration.orderBy.enddate'), |
||
532 | ]; |
||
533 | } |
||
534 | |||
535 | protected function getLanguageService(): LanguageService |
||
538 | } |
||
539 | |||
540 | protected function getBackendUser(): BackendUserAuthentication |
||
541 | { |
||
542 | return $GLOBALS['BE_USER']; |
||
543 | } |
||
544 | } |
||
545 |
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