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 |
||
25 | class NotificationService |
||
26 | { |
||
27 | |||
28 | /** |
||
29 | * The object manager |
||
30 | * |
||
31 | * @var \TYPO3\CMS\Extbase\Object\ObjectManager |
||
32 | * @inject |
||
33 | */ |
||
34 | protected $objectManager; |
||
35 | |||
36 | /** |
||
37 | * Registration repository |
||
38 | * |
||
39 | * @var \DERHANSEN\SfEventMgt\Domain\Repository\RegistrationRepository |
||
40 | * @inject |
||
41 | */ |
||
42 | protected $registrationRepository = null; |
||
43 | |||
44 | /** |
||
45 | * Email Service |
||
46 | * |
||
47 | * @var \DERHANSEN\SfEventMgt\Service\EmailService |
||
48 | * @inject |
||
49 | */ |
||
50 | protected $emailService; |
||
51 | |||
52 | /** |
||
53 | * Hash Service |
||
54 | * |
||
55 | * @var \TYPO3\CMS\Extbase\Security\Cryptography\HashService |
||
56 | * @inject |
||
57 | */ |
||
58 | protected $hashService; |
||
59 | |||
60 | /** |
||
61 | * FluidStandaloneService |
||
62 | * |
||
63 | * @var \DERHANSEN\SfEventMgt\Service\FluidStandaloneService |
||
64 | * @inject |
||
65 | */ |
||
66 | protected $fluidStandaloneService; |
||
67 | |||
68 | /** |
||
69 | * CustomNotificationLogRepository |
||
70 | * |
||
71 | * @var \DERHANSEN\SfEventMgt\Domain\Repository\CustomNotificationLogRepository |
||
72 | * @inject |
||
73 | */ |
||
74 | protected $customNotificationLogRepository = null; |
||
75 | |||
76 | /** |
||
77 | * Sends a custom notification defined by the given customNotification key |
||
78 | * to all confirmed users of the event |
||
79 | * |
||
80 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
||
81 | * @param string $customNotification CustomNotification |
||
82 | * @param array $settings Settings |
||
83 | * |
||
84 | * @return int Number of notifications sent |
||
85 | */ |
||
86 | public function sendCustomNotification($event, $customNotification, $settings) |
||
112 | |||
113 | /** |
||
114 | * Returns true if conditions are not met to send a custom notification |
||
115 | * |
||
116 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event |
||
117 | * @param array $settings |
||
118 | * @param string $customNotification |
||
119 | * |
||
120 | * @return bool |
||
121 | */ |
||
122 | protected function cantSendCustomNotification($event, $settings, $customNotification) |
||
126 | |||
127 | /** |
||
128 | * Adds a logentry to the custom notification log |
||
129 | * |
||
130 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
||
131 | * @param string $details Details |
||
132 | * @param int $emailsSent E-Mails sent |
||
133 | * |
||
134 | * @return void |
||
135 | */ |
||
136 | public function createCustomNotificationLogentry($event, $details, $emailsSent) |
||
145 | |||
146 | /** |
||
147 | * Sends a message to the user based on the given type |
||
148 | * |
||
149 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
||
150 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
||
151 | * @param array $settings Settings |
||
152 | * @param int $type Type |
||
153 | * @param string $customNotification CustomNotification |
||
154 | * |
||
155 | * @return bool TRUE if successful, else FALSE |
||
156 | */ |
||
157 | public function sendUserMessage($event, $registration, $settings, $type, $customNotification = '') |
||
177 | |||
178 | /** |
||
179 | * Returns an array with template and subject for the user message |
||
180 | * |
||
181 | * @param array $settings |
||
182 | * @param int $type Type |
||
183 | * @param string $customNotification |
||
184 | * @return array |
||
185 | */ |
||
186 | protected function getUserMessageTemplateSubject($settings, $type, $customNotification) |
||
211 | |||
212 | /** |
||
213 | * Sends a message to the admin based on the given type |
||
214 | * |
||
215 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
||
216 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
||
217 | * @param array $settings Settings |
||
218 | * @param int $type Type |
||
219 | * |
||
220 | * @return bool TRUE if successful, else FALSE |
||
221 | */ |
||
222 | public function sendAdminMessage($event, $registration, $settings, $type) |
||
258 | |||
259 | /** |
||
260 | * Returns an array with template and subject for the admin message |
||
261 | * |
||
262 | * @param array $settings |
||
263 | * @param int $type Type |
||
264 | * @return array |
||
265 | */ |
||
266 | protected function getAdminMessageTemplateSubject($settings, $type) |
||
287 | |||
288 | /** |
||
289 | * Returns the rendered HTML for the given template |
||
290 | * |
||
291 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
||
292 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
||
293 | * @param string $template Template |
||
294 | * @param array $settings Settings |
||
295 | * |
||
296 | * @return string |
||
297 | */ |
||
298 | protected function getNotificationBody($event, $registration, $template, $settings) |
||
325 | } |
||
326 |