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 |
||
26 | class NotificationService |
||
27 | { |
||
28 | /** |
||
29 | * The object manager |
||
30 | * |
||
31 | * @var \TYPO3\CMS\Extbase\Object\ObjectManager |
||
32 | */ |
||
33 | protected $objectManager; |
||
34 | |||
35 | /** |
||
36 | * Registration repository |
||
37 | * |
||
38 | * @var \DERHANSEN\SfEventMgt\Domain\Repository\RegistrationRepository |
||
39 | */ |
||
40 | protected $registrationRepository = null; |
||
41 | |||
42 | /** |
||
43 | * Email Service |
||
44 | * |
||
45 | * @var \DERHANSEN\SfEventMgt\Service\EmailService |
||
46 | */ |
||
47 | protected $emailService; |
||
48 | |||
49 | /** |
||
50 | * Hash Service |
||
51 | * |
||
52 | * @var \TYPO3\CMS\Extbase\Security\Cryptography\HashService |
||
53 | */ |
||
54 | protected $hashService; |
||
55 | |||
56 | /** |
||
57 | * FluidStandaloneService |
||
58 | * |
||
59 | * @var \DERHANSEN\SfEventMgt\Service\FluidStandaloneService |
||
60 | */ |
||
61 | protected $fluidStandaloneService; |
||
62 | |||
63 | /** |
||
64 | * CustomNotificationLogRepository |
||
65 | * |
||
66 | * @var \DERHANSEN\SfEventMgt\Domain\Repository\CustomNotificationLogRepository |
||
67 | */ |
||
68 | protected $customNotificationLogRepository = null; |
||
69 | |||
70 | /** |
||
71 | * AttachmentService |
||
72 | * |
||
73 | * @var \DERHANSEN\SfEventMgt\Service\Notification\AttachmentService |
||
74 | */ |
||
75 | protected $attachmentService; |
||
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 | * Sends a custom notification defined by the given customNotification key |
|
153 | 1 | * to all confirmed users of the event |
|
154 | * |
||
155 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
||
156 | * @param string $customNotification CustomNotification |
||
157 | * @param array $settings Settings |
||
158 | * |
||
159 | * @return int Number of notifications sent |
||
160 | */ |
||
161 | public function sendCustomNotification($event, $customNotification, $settings) |
||
187 | 10 | ||
188 | /** |
||
189 | 10 | * Returns true if conditions are not met to send a custom notification |
|
190 | * |
||
191 | 5 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event |
|
192 | * @param array $settings |
||
193 | * @param string $customNotification |
||
194 | * |
||
195 | * @return bool |
||
196 | */ |
||
197 | protected function cantSendCustomNotification($event, $settings, $customNotification) |
||
201 | |||
202 | 17 | /** |
|
203 | * Adds a logentry to the custom notification log |
||
204 | 17 | * |
|
205 | 17 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
|
206 | * @param string $details Details |
||
207 | 17 | * @param int $emailsSent E-Mails sent |
|
208 | 3 | * |
|
209 | 3 | * @return void |
|
210 | 3 | */ |
|
211 | 14 | public function createCustomNotificationLogentry($event, $details, $emailsSent) |
|
220 | |||
221 | /** |
||
222 | * Sends a message to the user based on the given type |
||
223 | 8 | * |
|
224 | 1 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
|
225 | 1 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
|
226 | 1 | * @param array $settings Settings |
|
227 | 7 | * @param int $type Type |
|
228 | 7 | * @param string $customNotification CustomNotification |
|
229 | 7 | * |
|
230 | * @return bool TRUE if successful, else FALSE |
||
231 | 17 | */ |
|
232 | public function sendUserMessage($event, $registration, $settings, $type, $customNotification = '') |
||
266 | 15 | ||
267 | 15 | /** |
|
268 | 15 | * Returns an array with template and subject for the user message |
|
269 | 15 | * |
|
270 | 15 | * @param array $settings |
|
271 | 15 | * @param int $type Type |
|
272 | 15 | * @param string $customNotification |
|
273 | 15 | * @return array |
|
274 | */ |
||
275 | 15 | protected function getUserMessageTemplateSubject($settings, $type, $customNotification) |
|
308 | 5 | ||
309 | 5 | /** |
|
310 | 5 | * Sends a message to the admin based on the given type |
|
311 | 16 | * |
|
312 | 5 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
|
313 | 5 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
|
314 | 5 | * @param array $settings Settings |
|
315 | 11 | * @param int $type Type |
|
316 | * |
||
317 | * @return bool TRUE if successful, else FALSE |
||
318 | */ |
||
319 | 11 | public function sendAdminMessage($event, $registration, $settings, $type) |
|
370 | |||
371 | /** |
||
372 | * Returns an array with template and subject for the admin message |
||
373 | * |
||
374 | * @param array $settings |
||
375 | * @param int $type Type |
||
376 | * @return array |
||
377 | */ |
||
378 | protected function getAdminMessageTemplateSubject($settings, $type) |
||
407 | |||
408 | /** |
||
409 | * Returns the rendered HTML for the given template |
||
410 | * |
||
411 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
||
412 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
||
413 | * @param string $template Template |
||
414 | * @param array $settings Settings |
||
415 | * |
||
416 | * @return string |
||
417 | */ |
||
418 | protected function getNotificationBody($event, $registration, $template, $settings) |
||
434 | } |
||
435 |