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 | */ |
||
79 | public function injectHashService(\TYPO3\CMS\Extbase\Security\Cryptography\HashService $hashService) |
||
83 | |||
84 | /** |
||
85 | * DI for $objectManager |
||
86 | * |
||
87 | * @param \TYPO3\CMS\Extbase\Object\ObjectManager $objectManager |
||
88 | */ |
||
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 | /** |
||
105 | * DI for $registrationRepository |
||
106 | * |
||
107 | * @param \DERHANSEN\SfEventMgt\Domain\Repository\RegistrationRepository $registrationRepository |
||
108 | */ |
||
109 | public function injectRegistrationRepository( |
||
114 | |||
115 | /** |
||
116 | * Duplicates (all public accessable properties) the given registration the |
||
117 | * 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) |
||
139 | |||
140 | /** |
||
141 | * Confirms all depending registrations based on the given main registration |
||
142 | * |
||
143 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
||
144 | * |
||
145 | * @return void |
||
146 | */ |
||
147 | public function confirmDependingRegistrations($registration) |
||
156 | |||
157 | /** |
||
158 | * Checks if the registration can be confirmed and returns an array of variables |
||
159 | * |
||
160 | * @param int $reguid UID of registration |
||
161 | * @param string $hmac HMAC for parameters |
||
162 | * |
||
163 | * @return array |
||
164 | */ |
||
165 | public function checkConfirmRegistration($reguid, $hmac) |
||
211 | |||
212 | /** |
||
213 | * Cancels all depending registrations based on the given main registration |
||
214 | * |
||
215 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
||
216 | * |
||
217 | * @return void |
||
218 | */ |
||
219 | public function cancelDependingRegistrations($registration) |
||
226 | |||
227 | /** |
||
228 | * Checks if the registration can be cancelled and returns an array of variables |
||
229 | * |
||
230 | * @param int $reguid UID of registration |
||
231 | * @param string $hmac HMAC for parameters |
||
232 | * |
||
233 | * @return array |
||
234 | */ |
||
235 | public function checkCancelRegistration($reguid, $hmac) |
||
284 | |||
285 | /** |
||
286 | * Returns the current frontend user object if available |
||
287 | * |
||
288 | * @return mixed \TYPO3\CMS\Extbase\Domain\Model\FrontendUser|null |
||
289 | */ |
||
290 | public function getCurrentFeUserObject() |
||
298 | |||
299 | /** |
||
300 | * Checks, if the registration can successfully be created. Note, that |
||
301 | * $result is passed by reference! |
||
302 | * |
||
303 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
||
304 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
||
305 | * @param int $result Result |
||
306 | * |
||
307 | * @return array |
||
308 | */ |
||
309 | public function checkRegistrationSuccess($event, $registration, $result) |
||
347 | |||
348 | /** |
||
349 | * Returns if the given e-mail is registered to the given event |
||
350 | * |
||
351 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event |
||
352 | * @param string $email |
||
353 | * @return bool |
||
354 | */ |
||
355 | protected function emailNotUnique($event, $email) |
||
377 | |||
378 | /** |
||
379 | * Returns, if payment redirect for the payment method is enabled |
||
380 | * |
||
381 | * @param Registration $registration |
||
382 | * @return bool |
||
383 | */ |
||
384 | public function redirectPaymentEnabled($registration) |
||
398 | |||
399 | /** |
||
400 | * Returns if the given amount of registrations for the event will be registrations for the waitlist |
||
401 | * (depending on the total amount of registrations and free places) |
||
402 | * |
||
403 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event |
||
404 | * @param int $amountOfRegistrations |
||
405 | * @return bool |
||
406 | */ |
||
407 | public function isWaitlistRegistration($event, $amountOfRegistrations) |
||
421 | |||
422 | /** |
||
423 | * Fixes the event uid of a registration if the event has been saved as a child of a translated event. |
||
424 | * |
||
425 | * Since TYPO3 9.5 (#82363), registrations for events are saved to the translated event record |
||
426 | * |
||
427 | * Example: |
||
428 | * |
||
429 | * When a registration is saved for a translated event, the registration $registration->setEvent($event) will |
||
430 | * now save the UID of the translated event instead of the uid of the event in default language. |
||
431 | * |
||
432 | * This behavior breaks limitations on events (e.g. max participants). Therefore, the registration must always |
||
433 | * be related to the default event language (Extbase behavior before TYPO3 9.5) |
||
434 | * |
||
435 | * @param Registration $registration |
||
436 | * @param Event $event |
||
437 | * @return void |
||
438 | */ |
||
439 | public function fixRegistrationEvent(Registration $registration, Event $event) |
||
448 | |||
449 | /** |
||
450 | * Sets the "event" field of the given registration to the uid of the given event |
||
451 | * |
||
452 | * @param Registration $registration |
||
453 | * @param Event $event |
||
454 | * @return void |
||
455 | */ |
||
456 | protected function updateRegistrationEventUid(Registration $registration, Event $event) |
||
470 | |||
471 | /** |
||
472 | * Updates registration/waitlist registration counters for the given event |
||
473 | * |
||
474 | * @param Event $event |
||
475 | * @return void |
||
476 | */ |
||
477 | protected function updateEventRegistrationCounters(Event $event) |
||
502 | |||
503 | /** |
||
504 | * Returns the total amount of registrations/waitlist registrations for an event |
||
505 | * |
||
506 | * @param Event $event |
||
507 | * @param int $waitlist |
||
508 | * @return mixed |
||
509 | */ |
||
510 | protected function getEventRegistrationCount(Event $event, int $waitlist = 0) |
||
530 | } |
||
531 |
If you implement
__call
and 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
__call
is implemented by a parent class and only the child class knows which methods exist: