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 |
||
27 | class RegistrationService |
||
28 | { |
||
29 | /** |
||
30 | * The object manager |
||
31 | * |
||
32 | * @var \TYPO3\CMS\Extbase\Object\ObjectManager |
||
33 | * */ |
||
34 | protected $objectManager; |
||
35 | |||
36 | /** |
||
37 | * RegistrationRepository |
||
38 | * |
||
39 | * @var \DERHANSEN\SfEventMgt\Domain\Repository\RegistrationRepository |
||
40 | * */ |
||
41 | protected $registrationRepository; |
||
42 | |||
43 | /** |
||
44 | * FrontendUserRepository |
||
45 | * |
||
46 | * @var \TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository |
||
47 | * */ |
||
48 | protected $frontendUserRepository; |
||
49 | |||
50 | /** |
||
51 | * Hash Service |
||
52 | * |
||
53 | * @var \TYPO3\CMS\Extbase\Security\Cryptography\HashService |
||
54 | * */ |
||
55 | protected $hashService; |
||
56 | |||
57 | /** |
||
58 | * Payment Service |
||
59 | * |
||
60 | * @var \DERHANSEN\SfEventMgt\Service\PaymentService |
||
61 | * */ |
||
62 | protected $paymentService; |
||
63 | |||
64 | /** |
||
65 | * DI for $frontendUserRepository |
||
66 | * |
||
67 | * @param \TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository $frontendUserRepository |
||
68 | */ |
||
69 | public function injectFrontendUserRepository( |
||
74 | |||
75 | /** |
||
76 | * DI for $hashService |
||
77 | * |
||
78 | 2 | * @param \TYPO3\CMS\Extbase\Security\Cryptography\HashService $hashService |
|
79 | */ |
||
80 | 2 | public function injectHashService(\TYPO3\CMS\Extbase\Security\Cryptography\HashService $hashService) |
|
84 | 2 | ||
85 | 1 | /** |
|
86 | 1 | * DI for $objectManager |
|
87 | 1 | * |
|
88 | 1 | * @param \TYPO3\CMS\Extbase\Object\ObjectManager $objectManager |
|
89 | */ |
||
90 | 2 | public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManager $objectManager) |
|
94 | |||
95 | /** |
||
96 | * DI for $paymentService |
||
97 | * |
||
98 | * @param \DERHANSEN\SfEventMgt\Service\PaymentService $paymentService |
||
99 | */ |
||
100 | public function injectPaymentService(\DERHANSEN\SfEventMgt\Service\PaymentService $paymentService) |
||
104 | 1 | ||
105 | 1 | /** |
|
106 | * DI for $registrationRepository |
||
107 | 1 | * |
|
108 | 1 | * @param \DERHANSEN\SfEventMgt\Domain\Repository\RegistrationRepository $registrationRepository |
|
109 | 1 | */ |
|
110 | 1 | public function injectRegistrationRepository( |
|
115 | 1 | ||
116 | 1 | /** |
|
117 | 1 | * Handles expired registrations. If the $delete parameter is set, then |
|
118 | * registrations are deleted, else just hidden |
||
119 | * |
||
120 | * @param bool $delete Delete |
||
121 | * |
||
122 | * @return void |
||
123 | */ |
||
124 | public function handleExpiredRegistrations($delete = false) |
||
139 | |||
140 | /** |
||
141 | * Duplicates (all public accessable properties) the given registration the |
||
142 | * amount of times configured in amountOfRegistrations |
||
143 | * |
||
144 | 4 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
|
145 | * |
||
146 | * @return void |
||
147 | 4 | */ |
|
148 | 4 | public function createDependingRegistrations($registration) |
|
164 | 1 | ||
165 | /** |
||
166 | 4 | * Confirms all depending registrations based on the given main registration |
|
167 | 1 | * |
|
168 | 1 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
|
169 | 1 | * |
|
170 | 1 | * @return void |
|
171 | */ |
||
172 | 4 | public function confirmDependingRegistrations($registration) |
|
181 | |||
182 | /** |
||
183 | * Checks if the registration can be confirmed and returns an array of variables |
||
184 | 4 | * |
|
185 | 4 | * @param int $reguid UID of registration |
|
186 | 4 | * @param string $hmac HMAC for parameters |
|
187 | * |
||
188 | 4 | * @return array |
|
189 | */ |
||
190 | public function checkConfirmRegistration($reguid, $hmac) |
||
236 | 4 | ||
237 | 1 | /** |
|
238 | 1 | * Cancels all depending registrations based on the given main registration |
|
239 | 1 | * |
|
240 | 1 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
|
241 | * |
||
242 | 4 | * @return void |
|
243 | 4 | */ |
|
244 | 4 | public function cancelDependingRegistrations($registration) |
|
251 | 4 | ||
252 | 4 | /** |
|
253 | 4 | * Checks if the registration can be cancelled and returns an array of variables |
|
254 | * |
||
255 | 4 | * @param int $reguid UID of registration |
|
256 | * @param string $hmac HMAC for parameters |
||
257 | * |
||
258 | * @return array |
||
259 | */ |
||
260 | public function checkCancelRegistration($reguid, $hmac) |
||
309 | 7 | ||
310 | 2 | /** |
|
311 | 2 | * Returns the current frontend user object if available |
|
312 | 7 | * |
|
313 | 5 | * @return \TYPO3\CMS\Extbase\Domain\Model\FrontendUser|null |
|
314 | 5 | */ |
|
315 | 1 | public function getCurrentFeUserObject() |
|
323 | |||
324 | /** |
||
325 | * Checks, if the registration can successfully be created. Note, that |
||
326 | * $result is passed by reference! |
||
327 | 2 | * |
|
328 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
||
329 | 2 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
|
330 | 2 | * @param int $result Result |
|
331 | * |
||
332 | * @return bool |
||
333 | */ |
||
334 | public function checkRegistrationSuccess($event, $registration, &$result) |
||
371 | 4 | ||
372 | 2 | /** |
|
373 | 2 | * Returns if the given e-mail is registered to the given event |
|
374 | 4 | * |
|
375 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event |
||
376 | * @param string $email |
||
377 | * @return bool |
||
378 | */ |
||
379 | protected function emailNotUnique($event, $email) |
||
384 | |||
385 | /** |
||
386 | * Returns, if payment redirect for the payment method is enabled |
||
387 | * |
||
388 | * @param Registration $registration |
||
389 | * @return bool |
||
390 | */ |
||
391 | public function redirectPaymentEnabled($registration) |
||
405 | |||
406 | /** |
||
407 | * Returns if the given amount of registrations for the event will be registrations for the waitlist |
||
408 | * (depending on the total amount of registrations and free places) |
||
409 | * |
||
410 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event |
||
411 | * @param int $amountOfRegistrations |
||
412 | * @return bool |
||
413 | */ |
||
414 | public function isWaitlistRegistration($event, $amountOfRegistrations) |
||
427 | } |
||
428 |
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: