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 |
||
| 21 | class RegistrationService |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * The object manager |
||
| 25 | * |
||
| 26 | * @var \TYPO3\CMS\Extbase\Object\ObjectManager |
||
| 27 | * */ |
||
| 28 | protected $objectManager; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * RegistrationRepository |
||
| 32 | * |
||
| 33 | * @var \DERHANSEN\SfEventMgt\Domain\Repository\RegistrationRepository |
||
| 34 | * */ |
||
| 35 | protected $registrationRepository; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * FrontendUserRepository |
||
| 39 | * |
||
| 40 | * @var \TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository |
||
| 41 | * */ |
||
| 42 | protected $frontendUserRepository; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Hash Service |
||
| 46 | * |
||
| 47 | * @var \TYPO3\CMS\Extbase\Security\Cryptography\HashService |
||
| 48 | * */ |
||
| 49 | protected $hashService; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Payment Service |
||
| 53 | * |
||
| 54 | * @var \DERHANSEN\SfEventMgt\Service\PaymentService |
||
| 55 | * */ |
||
| 56 | protected $paymentService; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * DI for $frontendUserRepository |
||
| 60 | * |
||
| 61 | * @param \TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository $frontendUserRepository |
||
| 62 | */ |
||
| 63 | public function injectFrontendUserRepository( |
||
| 68 | |||
| 69 | /** |
||
| 70 | * DI for $hashService |
||
| 71 | * |
||
| 72 | * @param \TYPO3\CMS\Extbase\Security\Cryptography\HashService $hashService |
||
| 73 | */ |
||
| 74 | public function injectHashService(\TYPO3\CMS\Extbase\Security\Cryptography\HashService $hashService) |
||
| 78 | 2 | ||
| 79 | /** |
||
| 80 | 2 | * DI for $objectManager |
|
| 81 | 2 | * |
|
| 82 | 2 | * @param \TYPO3\CMS\Extbase\Object\ObjectManager $objectManager |
|
| 83 | */ |
||
| 84 | 2 | public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManager $objectManager) |
|
| 88 | 1 | ||
| 89 | /** |
||
| 90 | 2 | * DI for $paymentService |
|
| 91 | 2 | * |
|
| 92 | 2 | * @param \DERHANSEN\SfEventMgt\Service\PaymentService $paymentService |
|
| 93 | */ |
||
| 94 | public function injectPaymentService(\DERHANSEN\SfEventMgt\Service\PaymentService $paymentService) |
||
| 98 | |||
| 99 | /** |
||
| 100 | * DI for $registrationRepository |
||
| 101 | * |
||
| 102 | 1 | * @param \DERHANSEN\SfEventMgt\Domain\Repository\RegistrationRepository $registrationRepository |
|
| 103 | */ |
||
| 104 | 1 | public function injectRegistrationRepository( |
|
| 109 | 1 | ||
| 110 | 1 | /** |
|
| 111 | 1 | * Handles expired registrations. If the $delete parameter is set, then |
|
| 112 | 1 | * registrations are deleted, else just hidden |
|
| 113 | 1 | * |
|
| 114 | 1 | * @param bool $delete Delete |
|
| 115 | 1 | * |
|
| 116 | 1 | * @return void |
|
| 117 | 1 | */ |
|
| 118 | public function handleExpiredRegistrations($delete = false) |
||
| 133 | 1 | ||
| 134 | 1 | /** |
|
| 135 | * Duplicates (all public accessable properties) the given registration the |
||
| 136 | * amount of times configured in amountOfRegistrations |
||
| 137 | * |
||
| 138 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
||
| 139 | * |
||
| 140 | * @return void |
||
| 141 | */ |
||
| 142 | public function createDependingRegistrations($registration) |
||
| 158 | |||
| 159 | /** |
||
| 160 | 4 | * Confirms all depending registrations based on the given main registration |
|
| 161 | 1 | * |
|
| 162 | 1 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
|
| 163 | 1 | * |
|
| 164 | 1 | * @return void |
|
| 165 | */ |
||
| 166 | 4 | public function confirmDependingRegistrations($registration) |
|
| 175 | 1 | ||
| 176 | 1 | /** |
|
| 177 | * Checks if the registration can be confirmed and returns an array of variables |
||
| 178 | 4 | * |
|
| 179 | * @param int $reguid UID of registration |
||
| 180 | * @param string $hmac HMAC for parameters |
||
| 181 | * |
||
| 182 | * @return array |
||
| 183 | */ |
||
| 184 | 4 | public function checkConfirmRegistration($reguid, $hmac) |
|
| 230 | 4 | ||
| 231 | 1 | /** |
|
| 232 | 1 | * Cancels all depending registrations based on the given main registration |
|
| 233 | 1 | * |
|
| 234 | 1 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
|
| 235 | * |
||
| 236 | 4 | * @return void |
|
| 237 | 1 | */ |
|
| 238 | 1 | public function cancelDependingRegistrations($registration) |
|
| 245 | 1 | ||
| 246 | 1 | /** |
|
| 247 | 1 | * Checks if the registration can be cancelled and returns an array of variables |
|
| 248 | 1 | * |
|
| 249 | * @param int $reguid UID of registration |
||
| 250 | * @param string $hmac HMAC for parameters |
||
| 251 | 4 | * |
|
| 252 | 4 | * @return array |
|
| 253 | 4 | */ |
|
| 254 | public function checkCancelRegistration($reguid, $hmac) |
||
| 303 | 2 | ||
| 304 | 11 | /** |
|
| 305 | 2 | * Returns the current frontend user object if available |
|
| 306 | 2 | * |
|
| 307 | 9 | * @return mixed \TYPO3\CMS\Extbase\Domain\Model\FrontendUser|null |
|
| 308 | 2 | */ |
|
| 309 | 7 | public function getCurrentFeUserObject() |
|
| 317 | 19 | ||
| 318 | /** |
||
| 319 | * Checks, if the registration can successfully be created. Note, that |
||
| 320 | * $result is passed by reference! |
||
| 321 | * |
||
| 322 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
||
| 323 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
||
| 324 | * @param int $result Result |
||
| 325 | * |
||
| 326 | * @return bool |
||
| 327 | 2 | */ |
|
| 328 | public function checkRegistrationSuccess($event, $registration, &$result) |
||
| 366 | |||
| 367 | /** |
||
| 368 | 4 | * Returns if the given e-mail is registered to the given event |
|
| 369 | 4 | * |
|
| 370 | 1 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event |
|
| 371 | 4 | * @param string $email |
|
| 372 | 2 | * @return bool |
|
| 373 | 2 | */ |
|
| 374 | 4 | protected function emailNotUnique($event, $email) |
|
| 380 | |||
| 381 | /** |
||
| 382 | * Returns, if payment redirect for the payment method is enabled |
||
| 383 | * |
||
| 384 | * @param Registration $registration |
||
| 385 | * @return bool |
||
| 386 | */ |
||
| 387 | public function redirectPaymentEnabled($registration) |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Returns if the given amount of registrations for the event will be registrations for the waitlist |
||
| 404 | * (depending on the total amount of registrations and free places) |
||
| 405 | * |
||
| 406 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event |
||
| 407 | * @param int $amountOfRegistrations |
||
| 408 | * @return bool |
||
| 409 | */ |
||
| 410 | public function isWaitlistRegistration($event, $amountOfRegistrations) |
||
| 424 | } |
||
| 425 |
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: