Complex classes like NotificationService 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 NotificationService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class NotificationService |
||
22 | { |
||
23 | /** |
||
24 | * The object manager |
||
25 | * |
||
26 | * @var \TYPO3\CMS\Extbase\Object\ObjectManager |
||
27 | */ |
||
28 | protected $objectManager; |
||
29 | |||
30 | /** |
||
31 | * Registration repository |
||
32 | * |
||
33 | * @var \DERHANSEN\SfEventMgt\Domain\Repository\RegistrationRepository |
||
34 | */ |
||
35 | protected $registrationRepository = null; |
||
36 | |||
37 | /** |
||
38 | * Email Service |
||
39 | * |
||
40 | * @var \DERHANSEN\SfEventMgt\Service\EmailService |
||
41 | */ |
||
42 | protected $emailService; |
||
43 | |||
44 | /** |
||
45 | * Hash Service |
||
46 | * |
||
47 | * @var \TYPO3\CMS\Extbase\Security\Cryptography\HashService |
||
48 | */ |
||
49 | protected $hashService; |
||
50 | |||
51 | /** |
||
52 | * FluidStandaloneService |
||
53 | * |
||
54 | * @var \DERHANSEN\SfEventMgt\Service\FluidStandaloneService |
||
55 | */ |
||
56 | protected $fluidStandaloneService; |
||
57 | |||
58 | /** |
||
59 | * CustomNotificationLogRepository |
||
60 | * |
||
61 | * @var \DERHANSEN\SfEventMgt\Domain\Repository\CustomNotificationLogRepository |
||
62 | */ |
||
63 | protected $customNotificationLogRepository = null; |
||
64 | |||
65 | /** |
||
66 | * AttachmentService |
||
67 | * |
||
68 | * @var \DERHANSEN\SfEventMgt\Service\Notification\AttachmentService |
||
69 | */ |
||
70 | protected $attachmentService; |
||
71 | |||
72 | /** |
||
73 | * @var \TYPO3\CMS\Extbase\SignalSlot\Dispatcher |
||
74 | */ |
||
75 | protected $signalSlotDispatcher = null; |
||
76 | |||
77 | /** |
||
78 | * DI for $attachmentService |
||
79 | * |
||
80 | * @param Notification\AttachmentService $attachmentService |
||
81 | */ |
||
82 | public function injectAttachmentService( |
||
87 | |||
88 | /** |
||
89 | * DI for $customNotificationLogRepository |
||
90 | * |
||
91 | * @param \DERHANSEN\SfEventMgt\Domain\Repository\CustomNotificationLogRepository $customNotificationLogRepository |
||
92 | */ |
||
93 | public function injectCustomNotificationLogRepository( |
||
98 | 1 | ||
99 | /** |
||
100 | 3 | * DI for $emailService |
|
101 | * |
||
102 | 3 | * @param EmailService $emailService |
|
103 | 3 | */ |
|
104 | 3 | public function injectEmailService(\DERHANSEN\SfEventMgt\Service\EmailService $emailService) |
|
108 | 1 | ||
109 | 1 | /** |
|
110 | 1 | * DI for $fluidStandaloneService |
|
111 | 1 | * |
|
112 | * @param FluidStandaloneService $fluidStandaloneService |
||
113 | 1 | */ |
|
114 | 1 | public function injectFluidStandaloneService( |
|
119 | 3 | ||
120 | /** |
||
121 | * DI for $hashService |
||
122 | * |
||
123 | * @param \TYPO3\CMS\Extbase\Security\Cryptography\HashService $hashService |
||
124 | */ |
||
125 | public function injectHashService(\TYPO3\CMS\Extbase\Security\Cryptography\HashService $hashService) |
||
129 | |||
130 | /** |
||
131 | 4 | * DI for $objectManager |
|
132 | * |
||
133 | 4 | * @param \TYPO3\CMS\Extbase\Object\ObjectManager $objectManager |
|
134 | */ |
||
135 | public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManager $objectManager) |
||
139 | |||
140 | /** |
||
141 | * DI for $registrationRepository |
||
142 | * |
||
143 | * @param \DERHANSEN\SfEventMgt\Domain\Repository\RegistrationRepository $registrationRepository |
||
144 | */ |
||
145 | 1 | public function injectRegistrationRepository( |
|
150 | 1 | ||
151 | 1 | /** |
|
152 | 1 | * DI for $signalSlotDispatcher |
|
153 | 1 | * |
|
154 | * @param Dispatcher $signalSlotDispatcher |
||
155 | */ |
||
156 | public function injectSignalSlotDispatcher(\TYPO3\CMS\Extbase\SignalSlot\Dispatcher $signalSlotDispatcher) |
||
160 | |||
161 | /** |
||
162 | * Sends a custom notification defined by the given customNotification key |
||
163 | * to all confirmed users of the event |
||
164 | * |
||
165 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
||
166 | 17 | * @param string $customNotification CustomNotification |
|
167 | * @param array $settings Settings |
||
168 | 17 | * |
|
169 | * @return int Number of notifications sent |
||
170 | 17 | */ |
|
171 | 2 | public function sendCustomNotification($event, $customNotification, $settings) |
|
198 | |||
199 | /** |
||
200 | * Returns true if conditions are not met to send a custom notification |
||
201 | * |
||
202 | 17 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event |
|
203 | * @param array $settings |
||
204 | 17 | * @param string $customNotification |
|
205 | 17 | * |
|
206 | * @return bool |
||
207 | 17 | */ |
|
208 | 3 | protected function cantSendCustomNotification($event, $settings, $customNotification) |
|
212 | 3 | ||
213 | 3 | /** |
|
214 | 3 | * Adds a logentry to the custom notification log |
|
215 | 11 | * |
|
216 | 3 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
|
217 | 3 | * @param string $details Details |
|
218 | 3 | * @param int $emailsSent E-Mails sent |
|
219 | 8 | * |
|
220 | * @return void |
||
221 | */ |
||
222 | public function createCustomNotificationLogentry($event, $details, $emailsSent) |
||
231 | 17 | ||
232 | /** |
||
233 | 17 | * Sends a message to the user based on the given type |
|
234 | * |
||
235 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
||
236 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
||
237 | * @param array $settings Settings |
||
238 | * @param int $type Type |
||
239 | * @param string $customNotification CustomNotification |
||
240 | * |
||
241 | * @return bool TRUE if successful, else FALSE |
||
242 | */ |
||
243 | public function sendUserMessage($event, $registration, $settings, $type, $customNotification = '') |
||
298 | 26 | ||
299 | /** |
||
300 | 26 | * Returns an array with template and subject for the user message |
|
301 | 26 | * |
|
302 | * @param array $settings |
||
303 | 26 | * @param int $type Type |
|
304 | 5 | * @param string $customNotification |
|
305 | 5 | * @return array |
|
306 | 5 | */ |
|
307 | 21 | protected function getUserMessageTemplateSubject($settings, $type, $customNotification) |
|
341 | 30 | ||
342 | 30 | /** |
|
343 | 30 | * Sends a message to the admin based on the given type |
|
344 | 30 | * |
|
345 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
||
346 | 30 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
|
347 | * @param array $settings Settings |
||
348 | * @param int $type Type |
||
349 | * |
||
350 | * @return bool TRUE if successful, else FALSE |
||
351 | */ |
||
352 | 30 | public function sendAdminMessage($event, $registration, $settings, $type) |
|
418 | |||
419 | /** |
||
420 | * Returns an array with template and subject for the admin message |
||
421 | * |
||
422 | * @param array $settings |
||
423 | * @param int $type Type |
||
424 | * @return array |
||
425 | */ |
||
426 | protected function getAdminMessageTemplateSubject($settings, $type) |
||
456 | |||
457 | /** |
||
458 | * Returns the rendered HTML for the given template |
||
459 | * |
||
460 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
||
461 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
||
462 | * @param string $template Template |
||
463 | * @param array $settings Settings |
||
464 | * |
||
465 | * @return string |
||
466 | */ |
||
467 | protected function getNotificationBody($event, $registration, $template, $settings) |
||
483 | } |
||
484 |