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 | * The object manager |
||
29 | * |
||
30 | * @var \TYPO3\CMS\Extbase\Object\ObjectManager |
||
31 | */ |
||
32 | protected $objectManager; |
||
33 | |||
34 | /** |
||
35 | * Registration repository |
||
36 | * |
||
37 | * @var \DERHANSEN\SfEventMgt\Domain\Repository\RegistrationRepository |
||
38 | */ |
||
39 | protected $registrationRepository = null; |
||
40 | |||
41 | /** |
||
42 | * Email Service |
||
43 | * |
||
44 | * @var \DERHANSEN\SfEventMgt\Service\EmailService |
||
45 | */ |
||
46 | protected $emailService; |
||
47 | |||
48 | /** |
||
49 | * Hash Service |
||
50 | * |
||
51 | * @var \TYPO3\CMS\Extbase\Security\Cryptography\HashService |
||
52 | */ |
||
53 | protected $hashService; |
||
54 | |||
55 | /** |
||
56 | * FluidStandaloneService |
||
57 | * |
||
58 | * @var \DERHANSEN\SfEventMgt\Service\FluidStandaloneService |
||
59 | */ |
||
60 | protected $fluidStandaloneService; |
||
61 | |||
62 | /** |
||
63 | * CustomNotificationLogRepository |
||
64 | * |
||
65 | * @var \DERHANSEN\SfEventMgt\Domain\Repository\CustomNotificationLogRepository |
||
66 | */ |
||
67 | protected $customNotificationLogRepository = null; |
||
68 | |||
69 | /** |
||
70 | * AttachmentService |
||
71 | * |
||
72 | * @var \DERHANSEN\SfEventMgt\Service\Notification\AttachmentService |
||
73 | */ |
||
74 | protected $attachmentService; |
||
75 | |||
76 | /** |
||
77 | * @var EventDispatcherInterface |
||
78 | */ |
||
79 | protected $eventDispatcher; |
||
80 | |||
81 | /** |
||
82 | * DI for $attachmentService |
||
83 | * |
||
84 | * @param Notification\AttachmentService $attachmentService |
||
85 | */ |
||
86 | public function injectAttachmentService( |
||
91 | |||
92 | /** |
||
93 | * DI for $customNotificationLogRepository |
||
94 | * |
||
95 | * @param \DERHANSEN\SfEventMgt\Domain\Repository\CustomNotificationLogRepository $customNotificationLogRepository |
||
96 | */ |
||
97 | public function injectCustomNotificationLogRepository( |
||
102 | |||
103 | /** |
||
104 | * DI for $emailService |
||
105 | * |
||
106 | * @param EmailService $emailService |
||
107 | */ |
||
108 | public function injectEmailService(\DERHANSEN\SfEventMgt\Service\EmailService $emailService) |
||
112 | |||
113 | /** |
||
114 | * DI for $fluidStandaloneService |
||
115 | * |
||
116 | * @param FluidStandaloneService $fluidStandaloneService |
||
117 | */ |
||
118 | public function injectFluidStandaloneService( |
||
123 | |||
124 | /** |
||
125 | * DI for $hashService |
||
126 | * |
||
127 | * @param \TYPO3\CMS\Extbase\Security\Cryptography\HashService $hashService |
||
128 | */ |
||
129 | public function injectHashService(\TYPO3\CMS\Extbase\Security\Cryptography\HashService $hashService) |
||
133 | |||
134 | /** |
||
135 | * DI for $objectManager |
||
136 | * |
||
137 | * @param \TYPO3\CMS\Extbase\Object\ObjectManager $objectManager |
||
138 | */ |
||
139 | public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManager $objectManager) |
||
143 | |||
144 | /** |
||
145 | * DI for $registrationRepository |
||
146 | * |
||
147 | * @param \DERHANSEN\SfEventMgt\Domain\Repository\RegistrationRepository $registrationRepository |
||
148 | */ |
||
149 | public function injectRegistrationRepository( |
||
154 | |||
155 | /** |
||
156 | * @param EventDispatcherInterface $eventDispatcher |
||
157 | */ |
||
158 | public function injectEventDispatcher(EventDispatcherInterface $eventDispatcher) |
||
162 | |||
163 | /** |
||
164 | * Sends a custom notification defined by the given customNotification key |
||
165 | * to all confirmed users of the event |
||
166 | * |
||
167 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
||
168 | * @param string $customNotification CustomNotification |
||
169 | * @param array $settings Settings |
||
170 | * |
||
171 | * @return int Number of notifications sent |
||
172 | */ |
||
173 | public function sendCustomNotification($event, $customNotification, $settings) |
||
200 | |||
201 | /** |
||
202 | * Returns true if conditions are not met to send a custom notification |
||
203 | * |
||
204 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event |
||
205 | * @param array $settings |
||
206 | * @param string $customNotification |
||
207 | * |
||
208 | * @return bool |
||
209 | */ |
||
210 | protected function cantSendCustomNotification($event, $settings, $customNotification) |
||
214 | |||
215 | /** |
||
216 | * Adds a logentry to the custom notification log |
||
217 | * |
||
218 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
||
219 | * @param string $details Details |
||
220 | * @param int $emailsSent E-Mails sent |
||
221 | * |
||
222 | * @return void |
||
223 | */ |
||
224 | public function createCustomNotificationLogentry($event, $details, $emailsSent) |
||
233 | |||
234 | /** |
||
235 | * Sends a message to the user based on the given type |
||
236 | * |
||
237 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
||
238 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
||
239 | * @param array $settings Settings |
||
240 | * @param int $type Type |
||
241 | * @param string $customNotification CustomNotification |
||
242 | * |
||
243 | * @return bool TRUE if successful, else FALSE |
||
244 | */ |
||
245 | public function sendUserMessage($event, $registration, $settings, $type, $customNotification = '') |
||
246 | { |
||
247 | list($template, $subject) = $this->getUserMessageTemplateSubject($settings, $type, $customNotification); |
||
248 | |||
249 | if (is_null($event) || is_null($registration) || is_null($type) || !is_array($settings) || |
||
250 | (substr($template, -5) != '.html') || (bool)$settings['notification']['disabled'] |
||
251 | ) { |
||
252 | return false; |
||
253 | } |
||
254 | |||
255 | if (!$registration->isIgnoreNotifications()) { |
||
256 | $body = $this->getNotificationBody($event, $registration, $template, $settings); |
||
257 | $subject = $this->fluidStandaloneService->parseStringFluid( |
||
258 | $subject, |
||
259 | [ |
||
260 | 'event' => $event, |
||
261 | 'registration' => $registration |
||
262 | ] |
||
263 | ); |
||
264 | $attachments = $this->attachmentService->getAttachments( |
||
265 | $settings, |
||
266 | $registration, |
||
267 | $type, |
||
268 | MessageRecipient::USER |
||
269 | ); |
||
270 | |||
271 | // Get iCal attachment if configured |
||
272 | $iCalAttachment = $this->attachmentService->getICalAttachment( |
||
273 | $settings, |
||
274 | $registration, |
||
275 | $type, |
||
276 | MessageRecipient::USER |
||
277 | ); |
||
278 | |||
279 | if ($iCalAttachment !== '') { |
||
280 | $attachments[] = $iCalAttachment; |
||
281 | } |
||
282 | |||
283 | $modifyUserMessageSenderEvent = new ModifyUserMessageSenderEvent( |
||
284 | $settings['notification']['senderName'] ?? '', |
||
285 | $settings['notification']['senderEmail'] ?? '', |
||
286 | $settings['notification']['replyToEmail'] ?? '', |
||
287 | $registration, |
||
288 | $type, |
||
289 | $this |
||
290 | ); |
||
291 | $this->eventDispatcher->dispatch($modifyUserMessageSenderEvent); |
||
|
|||
292 | $senderName = $modifyUserMessageSenderEvent->getSenderName(); |
||
293 | $senderEmail = $modifyUserMessageSenderEvent->getSenderEmail(); |
||
294 | $replyToEmail = $modifyUserMessageSenderEvent->getReplyToEmail(); |
||
295 | |||
296 | $modifyUserAttachmentsEvent = new ModifyUserMessageAttachmentsEvent( |
||
297 | $attachments, |
||
298 | $registration, |
||
299 | $type, |
||
300 | $this |
||
301 | ); |
||
302 | $this->eventDispatcher->dispatch($modifyUserMessageSenderEvent); |
||
303 | $attachments = $modifyUserAttachmentsEvent->getAttachments(); |
||
304 | |||
305 | $result = $this->emailService->sendEmailMessage( |
||
306 | $senderEmail, |
||
307 | $registration->getEmail(), |
||
308 | $subject, |
||
309 | $body, |
||
310 | $senderName, |
||
311 | $attachments, |
||
312 | $replyToEmail |
||
313 | ); |
||
314 | |||
315 | $afterUserMessageSentEvent = new AfterUserMessageSentEvent( |
||
316 | $registration, |
||
317 | $body, |
||
318 | $subject, |
||
319 | $attachments, |
||
320 | $senderName, |
||
321 | $senderEmail, |
||
322 | $replyToEmail, |
||
323 | $this |
||
324 | ); |
||
325 | $this->eventDispatcher->dispatch($afterUserMessageSentEvent); |
||
326 | |||
327 | // Cleanup iCal attachment if available |
||
328 | if ($iCalAttachment !== '') { |
||
329 | \TYPO3\CMS\Core\Utility\GeneralUtility::unlink_tempfile($iCalAttachment); |
||
330 | } |
||
331 | |||
332 | return $result; |
||
333 | } |
||
334 | |||
335 | return false; |
||
336 | } |
||
337 | |||
338 | /** |
||
339 | * Returns an array with template and subject for the user message |
||
340 | * |
||
341 | * @param array $settings |
||
342 | * @param int $type Type |
||
343 | * @param string $customNotification |
||
344 | * @return array |
||
345 | */ |
||
346 | protected function getUserMessageTemplateSubject($settings, $type, $customNotification) |
||
380 | |||
381 | /** |
||
382 | * Sends a message to the admin based on the given type |
||
383 | * |
||
384 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
||
385 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
||
386 | * @param array $settings Settings |
||
387 | * @param int $type Type |
||
388 | * |
||
389 | * @return bool TRUE if successful, else FALSE |
||
390 | */ |
||
391 | public function sendAdminMessage($event, $registration, $settings, $type) |
||
463 | |||
464 | /** |
||
465 | * Returns an array with template and subject for the admin message |
||
466 | * |
||
467 | * @param array $settings |
||
468 | * @param int $type Type |
||
469 | * @return array |
||
470 | */ |
||
471 | protected function getAdminMessageTemplateSubject($settings, $type) |
||
501 | |||
502 | /** |
||
503 | * Returns the rendered HTML for the given template |
||
504 | * |
||
505 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
||
506 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
||
507 | * @param string $template Template |
||
508 | * @param array $settings Settings |
||
509 | * |
||
510 | * @return string |
||
511 | */ |
||
512 | protected function getNotificationBody($event, $registration, $template, $settings) |
||
528 | } |
||
529 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: