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 | * Duplicates (all public accessable properties) the given registration the |
|
112 | 1 | * amount of times configured in amountOfRegistrations |
|
113 | 1 | * |
|
114 | 1 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
|
115 | 1 | * |
|
116 | 1 | * @return void |
|
117 | 1 | */ |
|
118 | public function createDependingRegistrations($registration) |
||
134 | 1 | ||
135 | /** |
||
136 | * Confirms all depending registrations based on the given main registration |
||
137 | * |
||
138 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
||
139 | * |
||
140 | * @return void |
||
141 | */ |
||
142 | public function confirmDependingRegistrations($registration) |
||
151 | |||
152 | 4 | /** |
|
153 | 1 | * Checks if the registration can be confirmed and returns an array of variables |
|
154 | 1 | * |
|
155 | 1 | * @param int $reguid UID of registration |
|
156 | 1 | * @param string $hmac HMAC for parameters |
|
157 | 3 | * |
|
158 | * @return array |
||
159 | */ |
||
160 | 4 | public function checkConfirmRegistration($reguid, $hmac) |
|
206 | |||
207 | /** |
||
208 | * Cancels all depending registrations based on the given main registration |
||
209 | * |
||
210 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
||
211 | * |
||
212 | * @return void |
||
213 | */ |
||
214 | 4 | public function cancelDependingRegistrations($registration) |
|
221 | |||
222 | 4 | /** |
|
223 | 1 | * Checks if the registration can be cancelled and returns an array of variables |
|
224 | 1 | * |
|
225 | 1 | * @param int $reguid UID of registration |
|
226 | 1 | * @param string $hmac HMAC for parameters |
|
227 | 3 | * |
|
228 | * @return array |
||
229 | */ |
||
230 | 4 | public function checkCancelRegistration($reguid, $hmac) |
|
279 | |||
280 | /** |
||
281 | * Returns the current frontend user object if available |
||
282 | 19 | * |
|
283 | * @return mixed \TYPO3\CMS\Extbase\Domain\Model\FrontendUser|null |
||
284 | 19 | */ |
|
285 | 19 | public function getCurrentFeUserObject() |
|
293 | 2 | ||
294 | 15 | /** |
|
295 | 13 | * Checks, if the registration can successfully be created. Note, that |
|
296 | 13 | * $result is passed by reference! |
|
297 | 2 | * |
|
298 | 2 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
|
299 | 13 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
|
300 | 11 | * @param int $result Result |
|
301 | 11 | * |
|
302 | 2 | * @return bool |
|
303 | 2 | */ |
|
304 | 11 | public function checkRegistrationSuccess($event, $registration, &$result) |
|
342 | 1 | ||
343 | /** |
||
344 | * Returns if the given e-mail is registered to the given event |
||
345 | * |
||
346 | 1 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event |
|
347 | 1 | * @param string $email |
|
348 | 1 | * @return bool |
|
349 | */ |
||
350 | protected function emailNotUnique($event, $email) |
||
356 | |||
357 | /** |
||
358 | * Returns, if payment redirect for the payment method is enabled |
||
359 | * |
||
360 | * @param Registration $registration |
||
361 | * @return bool |
||
362 | 7 | */ |
|
363 | public function redirectPaymentEnabled($registration) |
||
377 | |||
378 | /** |
||
379 | * Returns if the given amount of registrations for the event will be registrations for the waitlist |
||
380 | * (depending on the total amount of registrations and free places) |
||
381 | * |
||
382 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event |
||
383 | * @param int $amountOfRegistrations |
||
384 | * @return bool |
||
385 | */ |
||
386 | public function isWaitlistRegistration($event, $amountOfRegistrations) |
||
400 | } |
||
401 |
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: