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 |
||
20 | class NotificationService |
||
21 | { |
||
22 | /** |
||
23 | * The object manager |
||
24 | * |
||
25 | * @var \TYPO3\CMS\Extbase\Object\ObjectManager |
||
26 | */ |
||
27 | protected $objectManager; |
||
28 | |||
29 | /** |
||
30 | * Registration repository |
||
31 | * |
||
32 | * @var \DERHANSEN\SfEventMgt\Domain\Repository\RegistrationRepository |
||
33 | */ |
||
34 | protected $registrationRepository = null; |
||
35 | |||
36 | /** |
||
37 | * Email Service |
||
38 | * |
||
39 | * @var \DERHANSEN\SfEventMgt\Service\EmailService |
||
40 | */ |
||
41 | protected $emailService; |
||
42 | |||
43 | /** |
||
44 | * Hash Service |
||
45 | * |
||
46 | * @var \TYPO3\CMS\Extbase\Security\Cryptography\HashService |
||
47 | */ |
||
48 | protected $hashService; |
||
49 | |||
50 | /** |
||
51 | * FluidStandaloneService |
||
52 | * |
||
53 | * @var \DERHANSEN\SfEventMgt\Service\FluidStandaloneService |
||
54 | */ |
||
55 | protected $fluidStandaloneService; |
||
56 | |||
57 | /** |
||
58 | * CustomNotificationLogRepository |
||
59 | * |
||
60 | * @var \DERHANSEN\SfEventMgt\Domain\Repository\CustomNotificationLogRepository |
||
61 | */ |
||
62 | protected $customNotificationLogRepository = null; |
||
63 | |||
64 | /** |
||
65 | * AttachmentService |
||
66 | * |
||
67 | * @var \DERHANSEN\SfEventMgt\Service\Notification\AttachmentService |
||
68 | */ |
||
69 | protected $attachmentService; |
||
70 | |||
71 | /** |
||
72 | * DI for $attachmentService |
||
73 | * |
||
74 | * @param Notification\AttachmentService $attachmentService |
||
75 | */ |
||
76 | public function injectAttachmentService( |
||
81 | |||
82 | /** |
||
83 | * DI for $customNotificationLogRepository |
||
84 | * |
||
85 | * @param \DERHANSEN\SfEventMgt\Domain\Repository\CustomNotificationLogRepository $customNotificationLogRepository |
||
86 | */ |
||
87 | public function injectCustomNotificationLogRepository( |
||
92 | |||
93 | /** |
||
94 | * DI for $emailService |
||
95 | 4 | * |
|
96 | * @param EmailService $emailService |
||
97 | 4 | */ |
|
98 | 1 | public function injectEmailService(\DERHANSEN\SfEventMgt\Service\EmailService $emailService) |
|
102 | 3 | ||
103 | 3 | /** |
|
104 | 3 | * DI for $fluidStandaloneService |
|
105 | * |
||
106 | 3 | * @param FluidStandaloneService $fluidStandaloneService |
|
107 | 1 | */ |
|
108 | 1 | public function injectFluidStandaloneService( |
|
113 | 1 | ||
114 | 1 | /** |
|
115 | 1 | * DI for $hashService |
|
116 | 1 | * |
|
117 | 1 | * @param \TYPO3\CMS\Extbase\Security\Cryptography\HashService $hashService |
|
118 | 3 | */ |
|
119 | 3 | public function injectHashService(\TYPO3\CMS\Extbase\Security\Cryptography\HashService $hashService) |
|
123 | |||
124 | /** |
||
125 | * DI for $objectManager |
||
126 | * |
||
127 | * @param \TYPO3\CMS\Extbase\Object\ObjectManager $objectManager |
||
128 | */ |
||
129 | public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManager $objectManager) |
||
133 | 4 | ||
134 | /** |
||
135 | * DI for $registrationRepository |
||
136 | * |
||
137 | * @param \DERHANSEN\SfEventMgt\Domain\Repository\RegistrationRepository $registrationRepository |
||
138 | */ |
||
139 | public function injectRegistrationRepository( |
||
144 | |||
145 | 1 | /** |
|
146 | * Sends a custom notification defined by the given customNotification key |
||
147 | 1 | * to all confirmed users of the event |
|
148 | 1 | * |
|
149 | 1 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
|
150 | 1 | * @param string $customNotification CustomNotification |
|
151 | 1 | * @param array $settings Settings |
|
152 | 1 | * |
|
153 | 1 | * @return int Number of notifications sent |
|
154 | */ |
||
155 | public function sendCustomNotification($event, $customNotification, $settings) |
||
182 | 10 | ||
183 | 10 | /** |
|
184 | 10 | * Returns true if conditions are not met to send a custom notification |
|
185 | 10 | * |
|
186 | 10 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event |
|
187 | 10 | * @param array $settings |
|
188 | * @param string $customNotification |
||
189 | 10 | * |
|
190 | * @return bool |
||
191 | 5 | */ |
|
192 | protected function cantSendCustomNotification($event, $settings, $customNotification) |
||
196 | |||
197 | /** |
||
198 | * Adds a logentry to the custom notification log |
||
199 | * |
||
200 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
||
201 | * @param string $details Details |
||
202 | 17 | * @param int $emailsSent E-Mails sent |
|
203 | * |
||
204 | 17 | * @return void |
|
205 | 17 | */ |
|
206 | public function createCustomNotificationLogentry($event, $details, $emailsSent) |
||
215 | 11 | ||
216 | 3 | /** |
|
217 | 3 | * Sends a message to the user based on the given type |
|
218 | 3 | * |
|
219 | 8 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
|
220 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
||
221 | * @param array $settings Settings |
||
222 | * @param int $type Type |
||
223 | 8 | * @param string $customNotification CustomNotification |
|
224 | 1 | * |
|
225 | 1 | * @return bool TRUE if successful, else FALSE |
|
226 | 1 | */ |
|
227 | 7 | public function sendUserMessage($event, $registration, $settings, $type, $customNotification = '') |
|
263 | |||
264 | 20 | /** |
|
265 | 20 | * Returns an array with template and subject for the user message |
|
266 | 15 | * |
|
267 | 15 | * @param array $settings |
|
268 | 15 | * @param int $type Type |
|
269 | 15 | * @param string $customNotification |
|
270 | 15 | * @return array |
|
271 | 15 | */ |
|
272 | 15 | protected function getUserMessageTemplateSubject($settings, $type, $customNotification) |
|
306 | 5 | ||
307 | 21 | /** |
|
308 | 5 | * Sends a message to the admin based on the given type |
|
309 | 5 | * |
|
310 | 5 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
|
311 | 16 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
|
312 | 5 | * @param array $settings Settings |
|
313 | 5 | * @param int $type Type |
|
314 | 5 | * |
|
315 | 11 | * @return bool TRUE if successful, else FALSE |
|
316 | */ |
||
317 | public function sendAdminMessage($event, $registration, $settings, $type) |
||
377 | |||
378 | /** |
||
379 | * Returns an array with template and subject for the admin message |
||
380 | * |
||
381 | * @param array $settings |
||
382 | * @param int $type Type |
||
383 | * @return array |
||
384 | */ |
||
385 | protected function getAdminMessageTemplateSubject($settings, $type) |
||
415 | |||
416 | /** |
||
417 | * Returns the rendered HTML for the given template |
||
418 | * |
||
419 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
||
420 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
||
421 | * @param string $template Template |
||
422 | * @param array $settings Settings |
||
423 | * |
||
424 | * @return string |
||
425 | */ |
||
426 | protected function getNotificationBody($event, $registration, $template, $settings) |
||
443 | } |
||
444 |