Complex classes like EventController 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 EventController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
53 | class EventController extends AbstractController |
||
54 | { |
||
55 | /** |
||
56 | * @var EventCacheService |
||
57 | */ |
||
58 | protected $eventCacheService; |
||
59 | |||
60 | /** |
||
61 | * @param EventCacheService $cacheService |
||
62 | */ |
||
63 | public function injectEventCacheService(EventCacheService $cacheService) |
||
67 | |||
68 | /** |
||
69 | * Assign contentObjectData and pageData to earch view |
||
70 | * |
||
71 | * @param \TYPO3\CMS\Extbase\Mvc\View\ViewInterface $view |
||
72 | */ |
||
73 | protected function initializeView(\TYPO3\CMS\Extbase\Mvc\View\ViewInterface $view) |
||
81 | |||
82 | /** |
||
83 | * Initializes the current action |
||
84 | */ |
||
85 | public function initializeAction() |
||
97 | |||
98 | /** |
||
99 | * Creates an event demand object with the given settings |
||
100 | * |
||
101 | * @param array $settings The settings |
||
102 | * |
||
103 | * @return \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand |
||
104 | */ |
||
105 | public function createEventDemandObjectFromSettings(array $settings) |
||
125 | |||
126 | /** |
||
127 | * Creates a foreign record demand object with the given settings |
||
128 | * |
||
129 | * @param array $settings The settings |
||
130 | * |
||
131 | * @return \DERHANSEN\SfEventMgt\Domain\Model\Dto\ForeignRecordDemand |
||
132 | */ |
||
133 | public function createForeignRecordDemandObjectFromSettings(array $settings) |
||
142 | 1 | ||
143 | 1 | /** |
|
144 | 1 | * Creates a category demand object with the given settings |
|
145 | 1 | * |
|
146 | 1 | * @param array $settings The settings |
|
147 | 1 | * |
|
148 | 1 | * @return \DERHANSEN\SfEventMgt\Domain\Model\Dto\CategoryDemand |
|
149 | 1 | */ |
|
150 | 1 | public function createCategoryDemandObjectFromSettings(array $settings) |
|
151 | { |
||
152 | /** @var \DERHANSEN\SfEventMgt\Domain\Model\Dto\CategoryDemand $demand */ |
||
153 | $demand = $this->objectManager->get(CategoryDemand::class); |
||
154 | $demand->setStoragePage(Page::extendPidListByChildren($settings['storagePage'], $settings['recursive'])); |
||
155 | $demand->setRestrictToStoragePage((bool)$settings['restrictForeignRecordsToStoragePage']); |
||
156 | $demand->setCategories($settings['categoryMenu']['categories']); |
||
157 | $demand->setIncludeSubcategories($settings['categoryMenu']['includeSubcategories']); |
||
158 | |||
159 | return $demand; |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * Hook into request processing and catch exceptions |
||
164 | * |
||
165 | * @param RequestInterface $request |
||
166 | * @param ResponseInterface $response |
||
167 | * @throws \Exception |
||
168 | */ |
||
169 | public function processRequest(RequestInterface $request, ResponseInterface $response) |
||
170 | { |
||
171 | try { |
||
172 | parent::processRequest($request, $response); |
||
173 | } catch (\Exception $exception) { |
||
174 | $this->handleKnownExceptionsElseThrowAgain($exception); |
||
175 | } |
||
176 | 1 | } |
|
177 | |||
178 | /** |
||
179 | 1 | * Handle known exceptions |
|
180 | 1 | * |
|
181 | 1 | * @param \Exception $exception |
|
182 | 1 | * @throws \Exception |
|
183 | 1 | */ |
|
184 | 1 | private function handleKnownExceptionsElseThrowAgain(\Exception $exception) |
|
196 | |||
197 | 2 | /** |
|
198 | 2 | * Initialize list action and set format |
|
199 | 2 | */ |
|
200 | public function initializeListAction() |
||
206 | |||
207 | /** |
||
208 | * List view |
||
209 | * |
||
210 | * @param array $overwriteDemand OverwriteDemand |
||
211 | */ |
||
212 | public function listAction(array $overwriteDemand = []) |
||
213 | { |
||
214 | $eventDemand = $this->createEventDemandObjectFromSettings($this->settings); |
||
215 | $foreignRecordDemand = $this->createForeignRecordDemandObjectFromSettings($this->settings); |
||
216 | $categoryDemand = $this->createCategoryDemandObjectFromSettings($this->settings); |
||
217 | if ($this->isOverwriteDemand($overwriteDemand)) { |
||
218 | $eventDemand = $this->overwriteEventDemandObject($eventDemand, $overwriteDemand); |
||
219 | } |
||
220 | $events = $this->eventRepository->findDemanded($eventDemand); |
||
221 | $categories = $this->categoryRepository->findDemanded($categoryDemand); |
||
222 | $locations = $this->locationRepository->findDemanded($foreignRecordDemand); |
||
223 | $organisators = $this->organisatorRepository->findDemanded($foreignRecordDemand); |
||
224 | $speakers = $this->speakerRepository->findDemanded($foreignRecordDemand); |
||
225 | |||
226 | 3 | $modifyListViewVariablesEvent = new ModifyListViewVariablesEvent( |
|
227 | [ |
||
228 | 3 | 'events' => $events, |
|
229 | 3 | 'categories' => $categories, |
|
230 | 3 | 'locations' => $locations, |
|
231 | 3 | 'organisators' => $organisators, |
|
232 | 1 | 'speakers' => $speakers, |
|
233 | 1 | 'overwriteDemand' => $overwriteDemand, |
|
234 | 3 | 'eventDemand' => $eventDemand |
|
235 | 3 | ], |
|
236 | 3 | $this |
|
237 | 3 | ); |
|
238 | 3 | $this->eventDispatcher->dispatch($modifyListViewVariablesEvent); |
|
239 | 3 | $variables = $modifyListViewVariablesEvent->getVariables(); |
|
240 | 3 | $this->view->assignMultiple($variables); |
|
241 | 3 | ||
242 | $this->eventCacheService->addPageCacheTagsByEventDemandObject($eventDemand); |
||
243 | } |
||
244 | |||
245 | /** |
||
246 | * Calendar view |
||
247 | * |
||
248 | * @param array $overwriteDemand OverwriteDemand |
||
249 | */ |
||
250 | 1 | public function calendarAction(array $overwriteDemand = []) |
|
311 | 11 | ||
312 | /** |
||
313 | 11 | * Changes the given event demand object to select a date range for a calendar month including days of the previous |
|
314 | 11 | * month for the first week and they days for the next month for the last week |
|
315 | 11 | * |
|
316 | * @param EventDemand $eventDemand |
||
317 | * @return EventDemand |
||
318 | 11 | */ |
|
319 | 4 | protected function changeEventDemandToFullMonthDateRange(EventDemand $eventDemand) |
|
320 | 4 | { |
|
321 | 4 | $calendarDateRange = $this->calendarService->getCalendarDateRange( |
|
322 | 4 | $eventDemand->getMonth(), |
|
323 | 4 | $eventDemand->getYear(), |
|
324 | 4 | $this->settings['calendar']['firstDayOfWeek'] |
|
325 | ); |
||
326 | 4 | ||
327 | 4 | $eventDemand->setMonth(0); |
|
328 | 4 | $eventDemand->setYear(0); |
|
329 | 4 | ||
330 | $startDate = new \DateTime(); |
||
331 | 4 | $startDate->setTimestamp($calendarDateRange['firstDayOfCalendar']); |
|
332 | 4 | $endDate = new \DateTime(); |
|
333 | 4 | $endDate->setTimestamp($calendarDateRange['lastDayOfCalendar']); |
|
334 | 4 | $endDate->setTime(23, 59, 59); |
|
335 | 4 | ||
336 | 4 | $searchDemand = new SearchDemand(); |
|
337 | 4 | $searchDemand->setStartDate($startDate); |
|
338 | 4 | $searchDemand->setEndDate($endDate); |
|
339 | $eventDemand->setSearchDemand($searchDemand); |
||
340 | |||
341 | 4 | return $eventDemand; |
|
342 | } |
||
343 | |||
344 | 4 | /** |
|
345 | 1 | * Detail view for an event |
|
346 | 1 | * |
|
347 | 1 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
|
348 | 3 | * @return mixed string|void |
|
349 | 3 | */ |
|
350 | public function detailAction(Event $event = null) |
||
351 | 4 | { |
|
352 | $event = $this->evaluateSingleEventSetting($event); |
||
353 | $event = $this->evaluateIsShortcutSetting($event); |
||
354 | 4 | if (is_a($event, Event::class) && $this->settings['detail']['checkPidOfEventRecord']) { |
|
355 | 3 | $event = $this->checkPidOfEventRecord($event); |
|
356 | 3 | } |
|
357 | 3 | ||
358 | 3 | if (is_null($event) && isset($this->settings['event']['errorHandling'])) { |
|
359 | return $this->handleEventNotFoundError($this->settings); |
||
360 | 3 | } |
|
361 | 3 | ||
362 | 3 | $modifyDetailViewVariablesEvent = new ModifyDetailViewVariablesEvent(['event' => $event], $this); |
|
363 | 3 | $this->eventDispatcher->dispatch($modifyDetailViewVariablesEvent); |
|
364 | 3 | $variables = $modifyDetailViewVariablesEvent->getVariables(); |
|
365 | |||
366 | 3 | $this->view->assignMultiple($variables); |
|
367 | 3 | if ($event !== null) { |
|
368 | $this->eventCacheService->addCacheTagsByEventRecords([$event]); |
||
369 | } |
||
370 | 4 | } |
|
371 | 1 | ||
372 | 1 | /** |
|
373 | * Error handling if event is not found |
||
374 | * |
||
375 | 4 | * @param array $settings |
|
376 | 4 | * @return string |
|
377 | */ |
||
378 | 11 | protected function handleEventNotFoundError($settings) |
|
379 | 1 | { |
|
380 | 1 | if (empty($settings['event']['errorHandling'])) { |
|
381 | 1 | return null; |
|
382 | 1 | } |
|
383 | |||
384 | 1 | $configuration = GeneralUtility::trimExplode(',', $settings['event']['errorHandling'], true); |
|
385 | 1 | ||
386 | 1 | switch ($configuration[0]) { |
|
387 | 1 | case 'redirectToListView': |
|
388 | 1 | $listPid = (int)$settings['listPid'] > 0 ? (int)$settings['listPid'] : 1; |
|
389 | 10 | $this->redirect('list', null, null, null, $listPid); |
|
390 | 10 | break; |
|
391 | 10 | case 'pageNotFoundHandler': |
|
392 | 10 | $response = GeneralUtility::makeInstance(ErrorController::class)->pageNotFoundAction( |
|
393 | 10 | $GLOBALS['TYPO3_REQUEST'], |
|
394 | 10 | 'Event not found.' |
|
395 | ); |
||
396 | 11 | throw new ImmediateResponseException($response, 1549896549734); |
|
397 | break; |
||
398 | case 'showStandaloneTemplate': |
||
399 | if (isset($configuration[2])) { |
||
400 | $statusCode = constant(HttpUtility::class . '::HTTP_STATUS_' . $configuration[2]); |
||
401 | HttpUtility::setResponseCode($statusCode); |
||
402 | } |
||
403 | $standaloneTemplate = $this->objectManager->get(StandaloneView::class); |
||
404 | $standaloneTemplate->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName($configuration[1])); |
||
405 | 9 | ||
406 | return $standaloneTemplate->render(); |
||
407 | break; |
||
408 | 9 | default: |
|
409 | 1 | } |
|
410 | 1 | } |
|
411 | 1 | ||
412 | 8 | /** |
|
413 | * Initiates the iCalendar download for the given event |
||
414 | * |
||
415 | * @param Event $event The event |
||
416 | 8 | * |
|
417 | 1 | * @return string|false |
|
418 | 1 | */ |
|
419 | 1 | public function icalDownloadAction(Event $event = null) |
|
420 | 7 | { |
|
421 | 1 | if (is_a($event, Event::class) && $this->settings['detail']['checkPidOfEventRecord']) { |
|
422 | 1 | $event = $this->checkPidOfEventRecord($event); |
|
423 | 1 | } |
|
424 | 6 | if (is_null($event) && isset($this->settings['event']['errorHandling'])) { |
|
425 | 1 | return $this->handleEventNotFoundError($this->settings); |
|
426 | 1 | } |
|
427 | 1 | $this->icalendarService->downloadiCalendarFile($event); |
|
428 | 5 | exit(); |
|
429 | 1 | } |
|
430 | 1 | ||
431 | 1 | /** |
|
432 | 4 | * Registration view for an event |
|
433 | 1 | * |
|
434 | 1 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
|
435 | 1 | * |
|
436 | 3 | * @return mixed string|void |
|
437 | 1 | */ |
|
438 | 1 | public function registrationAction(Event $event = null) |
|
439 | 1 | { |
|
440 | 2 | $event = $this->evaluateSingleEventSetting($event); |
|
441 | 1 | if (is_a($event, Event::class) && $this->settings['registration']['checkPidOfEventRecord']) { |
|
442 | 1 | $event = $this->checkPidOfEventRecord($event); |
|
443 | 1 | } |
|
444 | 1 | if (is_null($event) && isset($this->settings['event']['errorHandling'])) { |
|
445 | 1 | return $this->handleEventNotFoundError($this->settings); |
|
446 | 1 | } |
|
447 | 1 | if ($event->getRestrictPaymentMethods()) { |
|
448 | $paymentMethods = $this->paymentService->getRestrictedPaymentMethods($event); |
||
449 | 9 | } else { |
|
450 | 9 | $paymentMethods = $this->paymentService->getPaymentMethods(); |
|
451 | 9 | } |
|
452 | |||
453 | $modifyRegistrationViewVariablesEvent = new ModifyRegistrationViewVariablesEvent( |
||
454 | [ |
||
455 | 'event' => $event, |
||
456 | 'paymentMethods' => $paymentMethods, |
||
457 | ], |
||
458 | $this |
||
459 | ); |
||
460 | $this->eventDispatcher->dispatch($modifyRegistrationViewVariablesEvent); |
||
461 | 3 | $variables = $modifyRegistrationViewVariablesEvent->getVariables(); |
|
462 | $this->view->assignMultiple($variables); |
||
463 | } |
||
464 | 3 | ||
465 | /** |
||
466 | 3 | * Removes all possible spamcheck fields (which do not belong to the domain model) from arguments. |
|
467 | 2 | */ |
|
468 | 2 | protected function removePossibleSpamCheckFieldsFromArguments() |
|
488 | |||
489 | /** |
||
490 | 2 | * Processes incoming registrations fields and adds field values to arguments |
|
491 | 2 | */ |
|
492 | 2 | protected function setRegistrationFieldValuesToArguments() |
|
493 | 2 | { |
|
494 | $arguments = $this->request->getArguments(); |
||
495 | if (!isset($arguments['event'])) { |
||
496 | 3 | return; |
|
497 | 3 | } |
|
498 | |||
499 | /** @var Event $event */ |
||
500 | $event = $this->eventRepository->findByUid((int)$this->request->getArgument('event')); |
||
501 | if (!$event || $event->getRegistrationFields()->count() === 0) { |
||
502 | return; |
||
503 | } |
||
504 | |||
505 | $registrationMvcArgument = $this->arguments->getArgument('registration'); |
||
506 | $propertyMapping = $registrationMvcArgument->getPropertyMappingConfiguration(); |
||
507 | $propertyMapping->allowProperties('fieldValues'); |
||
508 | $propertyMapping->allowCreationForSubProperty('fieldValues'); |
||
509 | $propertyMapping->allowModificationForSubProperty('fieldValues'); |
||
510 | |||
511 | // allow creation of new objects (for validation) |
||
512 | $propertyMapping->setTypeConverterOptions( |
||
513 | PersistentObjectConverter::class, |
||
514 | 3 | [ |
|
515 | 3 | PersistentObjectConverter::CONFIGURATION_CREATION_ALLOWED => true, |
|
516 | 3 | PersistentObjectConverter::CONFIGURATION_MODIFICATION_ALLOWED => true |
|
517 | ] |
||
518 | ); |
||
519 | |||
520 | // Set event to registration (required for validation) |
||
521 | $propertyMapping->allowProperties('event'); |
||
522 | $propertyMapping->allowCreationForSubProperty('event'); |
||
523 | $propertyMapping->allowModificationForSubProperty('event'); |
||
524 | $arguments['registration']['event'] = (int)$this->request->getArgument('event'); |
||
525 | |||
526 | 2 | $index = 0; |
|
527 | foreach ((array)$arguments['registration']['fields'] as $fieldUid => $value) { |
||
528 | // Only accept registration fields of the current event |
||
529 | 2 | if (!in_array((int)$fieldUid, $event->getRegistrationFieldsUids(), true)) { |
|
530 | continue; |
||
531 | 2 | } |
|
532 | |||
533 | 1 | // allow subvalues in new property mapper |
|
534 | 1 | $propertyMapping->forProperty('fieldValues')->allowProperties($index); |
|
535 | 1 | $propertyMapping->forProperty('fieldValues.' . $index)->allowAllProperties(); |
|
536 | 1 | $propertyMapping->allowCreationForSubProperty('fieldValues.' . $index); |
|
537 | $propertyMapping->allowModificationForSubProperty('fieldValues.' . $index); |
||
538 | 1 | ||
539 | 1 | if (is_array($value)) { |
|
540 | 1 | if (empty($value)) { |
|
541 | 1 | $value = ''; |
|
542 | 1 | } else { |
|
543 | $value = json_encode($value); |
||
544 | 1 | } |
|
545 | } |
||
546 | |||
547 | 1 | /** @var Registration\Field $field */ |
|
548 | 1 | $field = $this->fieldRepository->findByUid((int)$fieldUid); |
|
549 | 1 | ||
550 | $arguments['registration']['fieldValues'][$index] = [ |
||
551 | 'pid' => $field->getPid(), |
||
552 | 1 | 'value' => $value, |
|
553 | 'field' => (string)$fieldUid, |
||
554 | 'valueType' => $field->getValueType() |
||
555 | 1 | ]; |
|
556 | 1 | ||
557 | 2 | $index++; |
|
558 | 2 | } |
|
559 | 2 | ||
560 | // Remove temporary "fields" field |
||
561 | if (isset($arguments['registration']['fields'])) { |
||
562 | $arguments = ArrayUtility::removeByPath($arguments, 'registration/fields'); |
||
563 | } |
||
564 | $this->request->setArguments($arguments); |
||
565 | } |
||
566 | 1 | ||
567 | /** |
||
568 | 1 | * Set date format for field dateOfBirth |
|
569 | 1 | */ |
|
570 | 1 | public function initializeSaveRegistrationAction() |
|
571 | 1 | { |
|
572 | 1 | $this->arguments->getArgument('registration') |
|
573 | 1 | ->getPropertyMappingConfiguration()->forProperty('dateOfBirth') |
|
574 | 1 | ->setTypeConverterOption( |
|
575 | 1 | DateTimeConverter::class, |
|
576 | 1 | DateTimeConverter::CONFIGURATION_DATE_FORMAT, |
|
577 | 1 | $this->settings['registration']['formatDateOfBirth'] |
|
578 | 1 | ); |
|
579 | 1 | $this->removePossibleSpamCheckFieldsFromArguments(); |
|
580 | 1 | $this->setRegistrationFieldValuesToArguments(); |
|
581 | 1 | } |
|
582 | 1 | ||
583 | 1 | /** |
|
584 | 1 | * Saves the registration |
|
585 | * |
||
586 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
||
587 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
||
588 | * @Extbase\Validate("DERHANSEN\SfEventMgt\Validation\Validator\RegistrationFieldValidator", param="registration") |
||
589 | * @Extbase\Validate("DERHANSEN\SfEventMgt\Validation\Validator\RegistrationValidator", param="registration") |
||
590 | * |
||
591 | * @return mixed string|void |
||
592 | */ |
||
593 | public function saveRegistrationAction(Registration $registration, Event $event) |
||
700 | |||
701 | /** |
||
702 | * Shows the result of the saveRegistrationAction |
||
703 | * |
||
704 | * @param int $result Result |
||
705 | * @param int $eventuid |
||
706 | * @param string $hmac |
||
707 | */ |
||
708 | public function saveRegistrationResultAction($result, $eventuid, $hmac) |
||
767 | |||
768 | /** |
||
769 | * Confirms the registration if possible and sends emails to admin and user |
||
770 | * |
||
771 | * @param int $reguid UID of registration |
||
772 | * @param string $hmac HMAC for parameters |
||
773 | */ |
||
774 | public function confirmRegistrationAction($reguid, $hmac) |
||
848 | |||
849 | /** |
||
850 | * Cancels the registration if possible and sends emails to admin and user |
||
851 | * |
||
852 | * @param int $reguid UID of registration |
||
853 | * @param string $hmac HMAC for parameters |
||
854 | */ |
||
855 | public function cancelRegistrationAction($reguid, $hmac) |
||
908 | |||
909 | /** |
||
910 | * Set date format for field startDate and endDate |
||
911 | */ |
||
912 | public function initializeSearchAction() |
||
941 | |||
942 | /** |
||
943 | * Search view |
||
944 | * |
||
945 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\SearchDemand $searchDemand SearchDemand |
||
946 | * @param array $overwriteDemand OverwriteDemand |
||
947 | */ |
||
948 | public function searchAction(SearchDemand $searchDemand = null, array $overwriteDemand = []) |
||
993 | |||
994 | /** |
||
995 | * Returns if a demand object can be overwritten with the given overwriteDemand array |
||
996 | * |
||
997 | * @param array $overwriteDemand |
||
998 | * @return bool |
||
999 | */ |
||
1000 | protected function isOverwriteDemand($overwriteDemand) |
||
1004 | |||
1005 | /** |
||
1006 | * If no event is given and the singleEvent setting is set, the configured single event is returned |
||
1007 | * |
||
1008 | * @param Event|null $event |
||
1009 | * @return Event|null |
||
1010 | */ |
||
1011 | protected function evaluateSingleEventSetting($event) |
||
1019 | |||
1020 | /** |
||
1021 | * If no event is given and the isShortcut setting is set, the event is displayed using the "Insert Record" |
||
1022 | * content element and should be loaded from contect object data |
||
1023 | * |
||
1024 | * @param Event|null $event |
||
1025 | * @return Event|null |
||
1026 | */ |
||
1027 | protected function evaluateIsShortcutSetting($event) |
||
1036 | |||
1037 | /** |
||
1038 | * Checks if the event pid could be found in the storagePage settings of the detail plugin and |
||
1039 | * if the pid could not be found it return null instead of the event object. |
||
1040 | * |
||
1041 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event |
||
1042 | * @return \DERHANSEN\SfEventMgt\Domain\Model\Event|null |
||
1043 | */ |
||
1044 | protected function checkPidOfEventRecord(Event $event) |
||
1061 | |||
1062 | /** |
||
1063 | * Returns the current sys_language_uid |
||
1064 | * |
||
1065 | * @return int |
||
1066 | */ |
||
1067 | protected function getSysLanguageUid() |
||
1073 | } |
||
1074 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.