Complex classes like RegistrationService 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 RegistrationService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class RegistrationService |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * The object manager |
||
| 30 | * |
||
| 31 | * @var \TYPO3\CMS\Extbase\Object\ObjectManager |
||
| 32 | * */ |
||
| 33 | protected $objectManager; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * RegistrationRepository |
||
| 37 | * |
||
| 38 | * @var \DERHANSEN\SfEventMgt\Domain\Repository\RegistrationRepository |
||
| 39 | * */ |
||
| 40 | protected $registrationRepository; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * FrontendUserRepository |
||
| 44 | * |
||
| 45 | * @var \TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository |
||
| 46 | * */ |
||
| 47 | protected $frontendUserRepository; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Hash Service |
||
| 51 | * |
||
| 52 | * @var \TYPO3\CMS\Extbase\Security\Cryptography\HashService |
||
| 53 | * */ |
||
| 54 | protected $hashService; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Payment Service |
||
| 58 | * |
||
| 59 | * @var \DERHANSEN\SfEventMgt\Service\PaymentService |
||
| 60 | * */ |
||
| 61 | protected $paymentService; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * DI for $frontendUserRepository |
||
| 65 | * |
||
| 66 | * @param \TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository $frontendUserRepository |
||
| 67 | */ |
||
| 68 | public function injectFrontendUserRepository( |
||
| 73 | |||
| 74 | /** |
||
| 75 | * DI for $hashService |
||
| 76 | * |
||
| 77 | * @param \TYPO3\CMS\Extbase\Security\Cryptography\HashService $hashService |
||
| 78 | 2 | */ |
|
| 79 | public function injectHashService(\TYPO3\CMS\Extbase\Security\Cryptography\HashService $hashService) |
||
| 83 | |||
| 84 | 2 | /** |
|
| 85 | 1 | * DI for $objectManager |
|
| 86 | 1 | * |
|
| 87 | 1 | * @param \TYPO3\CMS\Extbase\Object\ObjectManager $objectManager |
|
| 88 | 1 | */ |
|
| 89 | public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManager $objectManager) |
||
| 93 | |||
| 94 | /** |
||
| 95 | * DI for $paymentService |
||
| 96 | * |
||
| 97 | * @param \DERHANSEN\SfEventMgt\Service\PaymentService $paymentService |
||
| 98 | */ |
||
| 99 | public function injectPaymentService(\DERHANSEN\SfEventMgt\Service\PaymentService $paymentService) |
||
| 103 | |||
| 104 | 1 | /** |
|
| 105 | 1 | * DI for $registrationRepository |
|
| 106 | * |
||
| 107 | 1 | * @param \DERHANSEN\SfEventMgt\Domain\Repository\RegistrationRepository $registrationRepository |
|
| 108 | 1 | */ |
|
| 109 | 1 | public function injectRegistrationRepository( |
|
| 114 | 1 | ||
| 115 | 1 | /** |
|
| 116 | 1 | * Duplicates (all public accessable properties) the given registration the |
|
| 117 | 1 | * amount of times configured in amountOfRegistrations |
|
| 118 | * |
||
| 119 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
||
| 120 | * |
||
| 121 | * @return void |
||
| 122 | */ |
||
| 123 | public function createDependingRegistrations($registration) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Confirms all depending registrations based on the given main registration |
||
| 143 | * |
||
| 144 | 4 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
|
| 145 | * |
||
| 146 | * @return void |
||
| 147 | 4 | */ |
|
| 148 | 4 | public function confirmDependingRegistrations($registration) |
|
| 157 | 3 | ||
| 158 | /** |
||
| 159 | * Checks if the registration can be confirmed and returns an array of variables |
||
| 160 | 4 | * |
|
| 161 | 1 | * @param int $reguid UID of registration |
|
| 162 | 1 | * @param string $hmac HMAC for parameters |
|
| 163 | 1 | * |
|
| 164 | 1 | * @return array |
|
| 165 | */ |
||
| 166 | 4 | public function checkConfirmRegistration($reguid, $hmac) |
|
| 212 | |||
| 213 | /** |
||
| 214 | 4 | * Cancels all depending registrations based on the given main registration |
|
| 215 | * |
||
| 216 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
||
| 217 | 4 | * |
|
| 218 | 4 | * @return void |
|
| 219 | 4 | */ |
|
| 220 | 4 | public function cancelDependingRegistrations($registration) |
|
| 227 | 3 | ||
| 228 | /** |
||
| 229 | * Checks if the registration can be cancelled and returns an array of variables |
||
| 230 | 4 | * |
|
| 231 | 1 | * @param int $reguid UID of registration |
|
| 232 | 1 | * @param string $hmac HMAC for parameters |
|
| 233 | 1 | * |
|
| 234 | 1 | * @return array |
|
| 235 | */ |
||
| 236 | 4 | public function checkCancelRegistration($reguid, $hmac) |
|
| 285 | 19 | ||
| 286 | 2 | /** |
|
| 287 | 2 | * Returns the current frontend user object if available |
|
| 288 | 19 | * |
|
| 289 | 2 | * @return mixed \TYPO3\CMS\Extbase\Domain\Model\FrontendUser|null |
|
| 290 | 2 | */ |
|
| 291 | 17 | public function getCurrentFeUserObject() |
|
| 299 | 13 | ||
| 300 | 11 | /** |
|
| 301 | 11 | * Checks, if the registration can successfully be created. Note, that |
|
| 302 | 2 | * $result is passed by reference! |
|
| 303 | 2 | * |
|
| 304 | 11 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
|
| 305 | 2 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
|
| 306 | 2 | * @param int $result Result |
|
| 307 | 9 | * |
|
| 308 | 2 | * @return array |
|
| 309 | 7 | */ |
|
| 310 | 2 | public function checkRegistrationSuccess($event, $registration, $result) |
|
| 348 | 1 | ||
| 349 | /** |
||
| 350 | * Returns if the given e-mail is registered to the given event |
||
| 351 | * |
||
| 352 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event |
||
| 353 | * @param string $email |
||
| 354 | * @return bool |
||
| 355 | */ |
||
| 356 | protected function emailNotUnique($event, $email) |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Returns, if payment redirect for the payment method is enabled |
||
| 381 | * |
||
| 382 | * @param Registration $registration |
||
| 383 | * @return bool |
||
| 384 | */ |
||
| 385 | public function redirectPaymentEnabled($registration) |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Returns if the given amount of registrations for the event will be registrations for the waitlist |
||
| 402 | * (depending on the total amount of registrations and free places) |
||
| 403 | * |
||
| 404 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event |
||
| 405 | * @param int $amountOfRegistrations |
||
| 406 | * @return bool |
||
| 407 | */ |
||
| 408 | public function isWaitlistRegistration($event, $amountOfRegistrations) |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Fixes the event uid of a registration if the event has been saved as a child of a translated event. |
||
| 425 | * |
||
| 426 | * Since TYPO3 9.5 (#82363), registrations for events are saved to the translated event record |
||
| 427 | * |
||
| 428 | * Example: |
||
| 429 | * |
||
| 430 | * When a registration is saved for a translated event, the registration $registration->setEvent($event) will |
||
| 431 | * now save the UID of the translated event instead of the uid of the event in default language. |
||
| 432 | * |
||
| 433 | * This behavior breaks limitations on events (e.g. max participants). Therefore, the registration must always |
||
| 434 | * be related to the default event language (Extbase behavior before TYPO3 9.5) |
||
| 435 | * |
||
| 436 | * @param Registration $registration |
||
| 437 | * @param Event $event |
||
| 438 | * @return void |
||
| 439 | */ |
||
| 440 | public function fixRegistrationEvent(Registration $registration, Event $event) |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Ensures, that the field "sys_language_uid" for registration fields values has the same value as the |
||
| 452 | * language of the registration and event. This is required, so emails include registration field values |
||
| 453 | * and correct registration field labels in their translated state. |
||
| 454 | * |
||
| 455 | * @param Registration $registration |
||
| 456 | * @param Event $event |
||
| 457 | * @return void |
||
| 458 | */ |
||
| 459 | public function fixRegistationFieldValueLanguage(Registration $registration, Event $event) |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Updates the field "sys_language_uid" for all registration field values of the given registration |
||
| 471 | * |
||
| 472 | * @param Registration $registration |
||
| 473 | * @param int $sysLanguageUid |
||
| 474 | * @return void |
||
| 475 | */ |
||
| 476 | protected function updateRegistrationFieldValueLanguage(Registration $registration, int $sysLanguageUid) |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Sets the "event" field of the given registration to the uid of the given event |
||
| 493 | * |
||
| 494 | * @param Registration $registration |
||
| 495 | * @param Event $event |
||
| 496 | * @return void |
||
| 497 | */ |
||
| 498 | protected function updateRegistrationEventUid(Registration $registration, Event $event) |
||
| 512 | |||
| 513 | /** |
||
| 514 | * Updates registration/waitlist registration counters for the given event |
||
| 515 | * |
||
| 516 | * @param Event $event |
||
| 517 | * @return void |
||
| 518 | */ |
||
| 519 | protected function updateEventRegistrationCounters(Event $event) |
||
| 544 | |||
| 545 | /** |
||
| 546 | * Returns the total amount of registrations/waitlist registrations for an event |
||
| 547 | * |
||
| 548 | * @param Event $event |
||
| 549 | * @param int $waitlist |
||
| 550 | * @return mixed |
||
| 551 | */ |
||
| 552 | protected function getEventRegistrationCount(Event $event, int $waitlist = 0) |
||
| 573 | } |
||
| 574 |
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: