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 |
||
| 33 | class EventController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController |
||
| 34 | { |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Configuration Manager |
||
| 38 | * |
||
| 39 | * @var ConfigurationManagerInterface |
||
| 40 | */ |
||
| 41 | protected $configurationManager; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * EventRepository |
||
| 45 | * |
||
| 46 | * @var \DERHANSEN\SfEventMgt\Domain\Repository\EventRepository |
||
| 47 | * @inject |
||
| 48 | */ |
||
| 49 | protected $eventRepository = null; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Registration repository |
||
| 53 | * |
||
| 54 | * @var \DERHANSEN\SfEventMgt\Domain\Repository\RegistrationRepository |
||
| 55 | * @inject |
||
| 56 | */ |
||
| 57 | protected $registrationRepository = null; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Category repository |
||
| 61 | * |
||
| 62 | * @var \DERHANSEN\SfEventMgt\Domain\Repository\CategoryRepository |
||
| 63 | * @inject |
||
| 64 | */ |
||
| 65 | protected $categoryRepository = null; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Location repository |
||
| 69 | * |
||
| 70 | * @var \DERHANSEN\SfEventMgt\Domain\Repository\LocationRepository |
||
| 71 | * @inject |
||
| 72 | */ |
||
| 73 | protected $locationRepository = null; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Notification Service |
||
| 77 | * |
||
| 78 | * @var \DERHANSEN\SfEventMgt\Service\NotificationService |
||
| 79 | * @inject |
||
| 80 | */ |
||
| 81 | protected $notificationService = null; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * ICalendar Service |
||
| 85 | * |
||
| 86 | * @var \DERHANSEN\SfEventMgt\Service\ICalendarService |
||
| 87 | * @inject |
||
| 88 | */ |
||
| 89 | protected $icalendarService = null; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Hash Service |
||
| 93 | * |
||
| 94 | * @var \TYPO3\CMS\Extbase\Security\Cryptography\HashService |
||
| 95 | * @inject |
||
| 96 | */ |
||
| 97 | protected $hashService; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * RegistrationService |
||
| 101 | * |
||
| 102 | * @var \DERHANSEN\SfEventMgt\Service\RegistrationService |
||
| 103 | * @inject |
||
| 104 | */ |
||
| 105 | protected $registrationService = null; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * UtilityService |
||
| 109 | * |
||
| 110 | * @var \DERHANSEN\SfEventMgt\Service\UtilityService |
||
| 111 | * @inject |
||
| 112 | */ |
||
| 113 | protected $utilityService = null; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * PaymentMethodService |
||
| 117 | * |
||
| 118 | * @var \DERHANSEN\SfEventMgt\Service\PaymentService |
||
| 119 | * @inject |
||
| 120 | */ |
||
| 121 | protected $paymentService = null; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Properties in this array will be ignored by overwriteDemandObject() |
||
| 125 | * |
||
| 126 | * @var array |
||
| 127 | */ |
||
| 128 | protected $ignoredSettingsForOverwriteDemand = ['storagePage']; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Creates an event demand object with the given settings |
||
| 132 | * |
||
| 133 | * @param array $settings The settings |
||
| 134 | * |
||
| 135 | * @return \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand |
||
| 136 | */ |
||
| 137 | 1 | public function createEventDemandObjectFromSettings(array $settings) |
|
| 152 | |||
| 153 | /** |
||
| 154 | * Creates a foreign record demand object with the given settings |
||
| 155 | * |
||
| 156 | * @param array $settings The settings |
||
| 157 | * |
||
| 158 | * @return \DERHANSEN\SfEventMgt\Domain\Model\Dto\ForeignRecordDemand |
||
| 159 | */ |
||
| 160 | public function createForeignRecordDemandObjectFromSettings(array $settings) |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Creates a category demand object with the given settings |
||
| 171 | * |
||
| 172 | * @param array $settings The settings |
||
| 173 | * |
||
| 174 | * @return \DERHANSEN\SfEventMgt\Domain\Model\Dto\CategoryDemand |
||
| 175 | */ |
||
| 176 | 1 | public function createCategoryDemandObjectFromSettings(array $settings) |
|
| 186 | |||
| 187 | /** |
||
| 188 | * Overwrites a given demand object by an propertyName => $propertyValue array |
||
| 189 | * |
||
| 190 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand $demand Demand |
||
| 191 | * @param array $overwriteDemand OwerwriteDemand |
||
| 192 | * |
||
| 193 | * @return \DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand |
||
| 194 | */ |
||
| 195 | 2 | protected function overwriteEventDemandObject(EventDemand $demand, array $overwriteDemand) |
|
| 206 | |||
| 207 | /** |
||
| 208 | * List view |
||
| 209 | * |
||
| 210 | * @param array $overwriteDemand OverwriteDemand |
||
| 211 | * |
||
| 212 | * @return void |
||
| 213 | */ |
||
| 214 | 3 | public function listAction(array $overwriteDemand = []) |
|
| 230 | |||
| 231 | /** |
||
| 232 | * Detail view for an event |
||
| 233 | * |
||
| 234 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
||
| 235 | * |
||
| 236 | * @return void |
||
| 237 | */ |
||
| 238 | 1 | public function detailAction(Event $event = null) |
|
| 242 | |||
| 243 | /** |
||
| 244 | * Initiates the iCalendar download for the given event |
||
| 245 | * |
||
| 246 | * @param Event $event The event |
||
| 247 | * |
||
| 248 | * @return bool |
||
| 249 | */ |
||
| 250 | 1 | public function icalDownloadAction(Event $event) |
|
| 255 | |||
| 256 | /** |
||
| 257 | * Registration view for an event |
||
| 258 | * |
||
| 259 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
||
| 260 | * |
||
| 261 | * @return void |
||
| 262 | */ |
||
| 263 | 1 | public function registrationAction(Event $event) |
|
| 273 | |||
| 274 | /** |
||
| 275 | * Set date format for field dateOfBirth |
||
| 276 | * |
||
| 277 | * @return void |
||
| 278 | */ |
||
| 279 | 1 | public function initializeSaveRegistrationAction() |
|
| 289 | |||
| 290 | /** |
||
| 291 | * Saves the registration |
||
| 292 | * |
||
| 293 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
||
| 294 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
||
| 295 | * @validate $registration \DERHANSEN\SfEventMgt\Validation\Validator\RegistrationValidator |
||
| 296 | * |
||
| 297 | * @return void |
||
| 298 | */ |
||
| 299 | 11 | public function saveRegistrationAction(Registration $registration, Event $event) |
|
| 300 | { |
||
| 301 | 11 | $autoConfirmation = (bool)$this->settings['registration']['autoConfirmation']; |
|
| 302 | 11 | $result = RegistrationResult::REGISTRATION_SUCCESSFUL; |
|
| 303 | 11 | $success = $this->registrationService->checkRegistrationSuccess($event, $registration, $result); |
|
| 304 | |||
| 305 | // Save registration if no errors |
||
| 306 | 11 | if ($success) { |
|
| 307 | 4 | $isWaitlistRegistration = $this->registrationService->isWaitlistRegistration( |
|
| 308 | 4 | $event, |
|
| 309 | 4 | $registration->getAmountOfRegistrations() |
|
| 310 | 4 | ); |
|
| 311 | 4 | $linkValidity = (int)$this->settings['confirmation']['linkValidity']; |
|
| 312 | 4 | if ($linkValidity === 0) { |
|
| 313 | // Use 3600 seconds as default value if not set |
||
| 314 | 4 | $linkValidity = 3600; |
|
| 315 | 4 | } |
|
| 316 | 4 | $confirmationUntil = new \DateTime(); |
|
| 317 | 4 | $confirmationUntil->add(new \DateInterval('PT' . $linkValidity . 'S')); |
|
| 318 | |||
| 319 | 4 | $registration->setEvent($event); |
|
| 320 | 4 | $registration->setPid($event->getPid()); |
|
| 321 | 4 | $registration->setConfirmationUntil($confirmationUntil); |
|
| 322 | 4 | $registration->setLanguage($GLOBALS['TSFE']->config['config']['language']); |
|
| 323 | 4 | $registration->setFeUser($this->registrationService->getCurrentFeUserObject()); |
|
| 324 | 4 | $registration->setWaitlist($isWaitlistRegistration); |
|
| 325 | 4 | $registration->_setProperty('_languageUid', $GLOBALS['TSFE']->sys_language_uid); |
|
| 326 | 4 | $this->registrationRepository->add($registration); |
|
| 327 | |||
| 328 | // Persist registration, so we have an UID |
||
| 329 | 4 | $this->objectManager->get('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\PersistenceManager')->persistAll(); |
|
| 330 | |||
| 331 | // Add new registration (or waitlist registration) to event |
||
| 332 | 4 | if ($isWaitlistRegistration) { |
|
| 333 | 1 | $event->addRegistrationWaitlist($registration); |
|
| 334 | 1 | $messageType = MessageType::REGISTRATION_WAITLIST_NEW; |
|
| 335 | 1 | } else { |
|
| 336 | 3 | $event->addRegistration($registration); |
|
| 337 | 3 | $messageType = MessageType::REGISTRATION_NEW; |
|
| 338 | } |
||
| 339 | 4 | $this->eventRepository->update($event); |
|
| 340 | |||
| 341 | // Send notifications to user and admin if confirmation link should be sent |
||
| 342 | 4 | if (!$autoConfirmation) { |
|
| 343 | 3 | $this->notificationService->sendUserMessage( |
|
| 344 | 3 | $event, |
|
| 345 | 3 | $registration, |
|
| 346 | 3 | $this->settings, |
|
| 347 | $messageType |
||
| 348 | 3 | ); |
|
| 349 | 3 | $this->notificationService->sendAdminMessage( |
|
| 350 | 3 | $event, |
|
| 351 | 3 | $registration, |
|
| 352 | 3 | $this->settings, |
|
| 353 | $messageType |
||
| 354 | 3 | ); |
|
| 355 | 3 | } |
|
| 356 | |||
| 357 | // Create given amount of registrations if necessary |
||
| 358 | 4 | if ($registration->getAmountOfRegistrations() > 1) { |
|
| 359 | 1 | $this->registrationService->createDependingRegistrations($registration); |
|
| 360 | 1 | } |
|
| 361 | |||
| 362 | // Clear cache for configured pages |
||
| 363 | 4 | $this->utilityService->clearCacheForConfiguredUids($this->settings); |
|
| 364 | 4 | } |
|
| 365 | |||
| 366 | 11 | if ($autoConfirmation && $success) { |
|
| 367 | 1 | $this->redirect( |
|
| 368 | 1 | 'confirmRegistration', |
|
| 369 | 1 | null, |
|
| 370 | 1 | null, |
|
| 371 | [ |
||
| 372 | 1 | 'reguid' => $registration->getUid(), |
|
| 373 | 1 | 'hmac' => $this->hashService->generateHmac('reg-' . $registration->getUid()) |
|
| 374 | 1 | ] |
|
| 375 | 1 | ); |
|
| 376 | 1 | } else { |
|
| 377 | 10 | $this->redirect( |
|
| 378 | 10 | 'saveRegistrationResult', |
|
| 379 | 10 | null, |
|
| 380 | 10 | null, |
|
| 381 | 10 | ['result' => $result] |
|
| 382 | 10 | ); |
|
| 383 | } |
||
| 384 | 11 | } |
|
| 385 | |||
| 386 | /** |
||
| 387 | * Shows the result of the saveRegistrationAction |
||
| 388 | * |
||
| 389 | * @param int $result Result |
||
| 390 | * |
||
| 391 | * @return void |
||
| 392 | */ |
||
| 393 | 9 | public function saveRegistrationResultAction($result) |
|
| 394 | { |
||
| 395 | switch ($result) { |
||
| 396 | 9 | case RegistrationResult::REGISTRATION_SUCCESSFUL: |
|
| 397 | 1 | $messageKey = 'event.message.registrationsuccessful'; |
|
| 398 | 1 | $titleKey = 'registrationResult.title.successful'; |
|
| 399 | 1 | break; |
|
| 400 | 8 | case RegistrationResult::REGISTRATION_SUCCESSFUL_WAITLIST: |
|
| 401 | $messageKey = 'event.message.registrationwaitlistsuccessful'; |
||
| 402 | $titleKey = 'registrationWaitlistResult.title.successful'; |
||
| 403 | break; |
||
| 404 | 8 | case RegistrationResult::REGISTRATION_FAILED_EVENT_EXPIRED: |
|
| 405 | 1 | $messageKey = 'event.message.registrationfailedeventexpired'; |
|
| 406 | 1 | $titleKey = 'registrationResult.title.failed'; |
|
| 407 | 1 | break; |
|
| 408 | 7 | case RegistrationResult::REGISTRATION_FAILED_MAX_PARTICIPANTS: |
|
| 409 | 1 | $messageKey = 'event.message.registrationfailedmaxparticipants'; |
|
| 410 | 1 | $titleKey = 'registrationResult.title.failed'; |
|
| 411 | 1 | break; |
|
| 412 | 6 | case RegistrationResult::REGISTRATION_NOT_ENABLED: |
|
| 413 | 1 | $messageKey = 'event.message.registrationfailednotenabled'; |
|
| 414 | 1 | $titleKey = 'registrationResult.title.failed'; |
|
| 415 | 1 | break; |
|
| 416 | 5 | case RegistrationResult::REGISTRATION_FAILED_DEADLINE_EXPIRED: |
|
| 417 | 1 | $messageKey = 'event.message.registrationfaileddeadlineexpired'; |
|
| 418 | 1 | $titleKey = 'registrationResult.title.failed'; |
|
| 419 | 1 | break; |
|
| 420 | 4 | case RegistrationResult::REGISTRATION_FAILED_NOT_ENOUGH_FREE_PLACES: |
|
| 421 | 1 | $messageKey = 'event.message.registrationfailednotenoughfreeplaces'; |
|
| 422 | 1 | $titleKey = 'registrationResult.title.failed'; |
|
| 423 | 1 | break; |
|
| 424 | 3 | case RegistrationResult::REGISTRATION_FAILED_MAX_AMOUNT_REGISTRATIONS_EXCEEDED: |
|
| 425 | 1 | $messageKey = 'event.message.registrationfailedmaxamountregistrationsexceeded'; |
|
| 426 | 1 | $titleKey = 'registrationResult.title.failed'; |
|
| 427 | 1 | break; |
|
| 428 | 2 | case RegistrationResult::REGISTRATION_FAILED_EMAIL_NOT_UNIQUE: |
|
| 429 | 1 | $messageKey = 'event.message.registrationfailedemailnotunique'; |
|
| 430 | 1 | $titleKey = 'registrationResult.title.failed'; |
|
| 431 | 1 | break; |
|
| 432 | 1 | default: |
|
| 433 | 1 | $messageKey = ''; |
|
| 434 | 1 | $titleKey = ''; |
|
| 435 | 1 | } |
|
| 436 | |||
| 437 | 9 | $this->view->assign('messageKey', $messageKey); |
|
| 438 | 9 | $this->view->assign('titleKey', $titleKey); |
|
| 439 | 9 | } |
|
| 440 | |||
| 441 | /** |
||
| 442 | * Confirms the registration if possible and sends e-mails to admin and user |
||
| 443 | * |
||
| 444 | * @param int $reguid UID of registration |
||
| 445 | * @param string $hmac HMAC for parameters |
||
| 446 | * |
||
| 447 | * @return void |
||
| 448 | */ |
||
| 449 | 3 | public function confirmRegistrationAction($reguid, $hmac) |
|
| 450 | { |
||
| 451 | /* @var $registration Registration */ |
||
| 452 | 3 | list($failed, $registration, $messageKey, $titleKey) = $this->registrationService->checkConfirmRegistration($reguid, $hmac); |
|
| 453 | |||
| 454 | 3 | if ($failed === false) { |
|
| 455 | 2 | $registration->setConfirmed(true); |
|
| 456 | 2 | $this->registrationRepository->update($registration); |
|
| 457 | |||
| 458 | 2 | $messageType = MessageType::REGISTRATION_CONFIRMED; |
|
| 459 | 2 | if ($registration->getWaitlist()) { |
|
| 460 | 1 | $messageType = MessageType::REGISTRATION_WAITLIST_CONFIRMED; |
|
| 461 | 1 | } |
|
| 462 | |||
| 463 | // Send notifications to user and admin |
||
| 464 | 2 | $this->notificationService->sendUserMessage( |
|
| 465 | 2 | $registration->getEvent(), |
|
| 466 | 2 | $registration, |
|
| 467 | 2 | $this->settings, |
|
| 468 | $messageType |
||
| 469 | 2 | ); |
|
| 470 | 2 | $this->notificationService->sendAdminMessage( |
|
| 471 | 2 | $registration->getEvent(), |
|
| 472 | 2 | $registration, |
|
| 473 | 2 | $this->settings, |
|
| 474 | $messageType |
||
| 475 | 2 | ); |
|
| 476 | |||
| 477 | // Confirm registrations depending on main registration if necessary |
||
| 478 | 2 | if ($registration->getAmountOfRegistrations() > 1) { |
|
| 479 | 2 | $this->registrationService->confirmDependingRegistrations($registration); |
|
| 480 | 2 | } |
|
| 481 | 2 | } |
|
| 482 | |||
| 483 | // Redirect to payment provider if payment/redirect is enabled |
||
| 484 | 3 | $paymentPid = (int)$this->settings['paymentPid']; |
|
| 485 | 3 | if (!$failed && $paymentPid > 0 && $this->registrationService->redirectPaymentEnabled($registration)) { |
|
| 486 | $this->uriBuilder->reset() |
||
| 487 | ->setTargetPageUid($paymentPid) |
||
| 488 | ->setUseCacheHash(false); |
||
| 489 | $uri = $this->uriBuilder->uriFor( |
||
| 490 | 'redirect', |
||
| 491 | [ |
||
| 492 | 'registration' => $registration, |
||
| 493 | 'hmac' => $this->hashService->generateHmac('redirectAction-' . $registration->getUid()) |
||
| 494 | ], |
||
| 495 | 'Payment', |
||
| 496 | 'sfeventmgt', |
||
| 497 | 'Pipayment' |
||
| 498 | ); |
||
| 499 | $this->redirectToUri($uri); |
||
| 500 | } |
||
| 501 | |||
| 502 | 3 | $this->view->assign('messageKey', $messageKey); |
|
| 503 | 3 | $this->view->assign('titleKey', $titleKey); |
|
| 504 | 3 | } |
|
| 505 | |||
| 506 | /** |
||
| 507 | * Cancels the registration if possible and sends e-mails to admin and user |
||
| 508 | * |
||
| 509 | * @param int $reguid UID of registration |
||
| 510 | * @param string $hmac HMAC for parameters |
||
| 511 | * |
||
| 512 | * @return void |
||
| 513 | */ |
||
| 514 | 2 | public function cancelRegistrationAction($reguid, $hmac) |
|
| 548 | |||
| 549 | /** |
||
| 550 | * Set date format for field startDate and endDate |
||
| 551 | * |
||
| 552 | * @return void |
||
| 553 | */ |
||
| 554 | 1 | public function initializeSearchAction() |
|
| 573 | |||
| 574 | /** |
||
| 575 | * Search view |
||
| 576 | * |
||
| 577 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Dto\SearchDemand $searchDemand SearchDemand |
||
| 578 | * @param array $overwriteDemand OverwriteDemand |
||
| 579 | * |
||
| 580 | * @return void |
||
| 581 | */ |
||
| 582 | 6 | public function searchAction(SearchDemand $searchDemand = null, array $overwriteDemand = []) |
|
| 616 | |||
| 617 | /** |
||
| 618 | * Returns if a demand object can be overwritten with the given overwriteDemand array |
||
| 619 | * |
||
| 620 | * @param array $overwriteDemand |
||
| 621 | * @return bool |
||
| 622 | */ |
||
| 623 | 9 | protected function isOverwriteDemand($overwriteDemand) |
|
| 627 | |||
| 628 | } |
||
| 629 |