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 |
||
28 | class NotificationService |
||
29 | { |
||
30 | /** |
||
31 | * The object manager |
||
32 | * |
||
33 | * @var \TYPO3\CMS\Extbase\Object\ObjectManager |
||
34 | */ |
||
35 | protected $objectManager; |
||
36 | |||
37 | /** |
||
38 | * Registration repository |
||
39 | * |
||
40 | * @var \DERHANSEN\SfEventMgt\Domain\Repository\RegistrationRepository |
||
41 | */ |
||
42 | protected $registrationRepository; |
||
43 | |||
44 | /** |
||
45 | * Email Service |
||
46 | * |
||
47 | * @var \DERHANSEN\SfEventMgt\Service\EmailService |
||
48 | */ |
||
49 | protected $emailService; |
||
50 | |||
51 | /** |
||
52 | * Hash Service |
||
53 | * |
||
54 | * @var \TYPO3\CMS\Extbase\Security\Cryptography\HashService |
||
55 | */ |
||
56 | protected $hashService; |
||
57 | |||
58 | /** |
||
59 | * FluidStandaloneService |
||
60 | * |
||
61 | * @var \DERHANSEN\SfEventMgt\Service\FluidStandaloneService |
||
62 | */ |
||
63 | protected $fluidStandaloneService; |
||
64 | |||
65 | /** |
||
66 | * CustomNotificationLogRepository |
||
67 | * |
||
68 | * @var \DERHANSEN\SfEventMgt\Domain\Repository\CustomNotificationLogRepository |
||
69 | */ |
||
70 | protected $customNotificationLogRepository; |
||
71 | |||
72 | /** |
||
73 | * AttachmentService |
||
74 | * |
||
75 | * @var \DERHANSEN\SfEventMgt\Service\Notification\AttachmentService |
||
76 | */ |
||
77 | protected $attachmentService; |
||
78 | |||
79 | /** |
||
80 | * @var EventDispatcherInterface |
||
81 | */ |
||
82 | protected $eventDispatcher; |
||
83 | |||
84 | /** |
||
85 | * DI for $attachmentService |
||
86 | * |
||
87 | * @param Notification\AttachmentService $attachmentService |
||
88 | */ |
||
89 | public function injectAttachmentService( |
||
94 | |||
95 | /** |
||
96 | * DI for $customNotificationLogRepository |
||
97 | * |
||
98 | * @param \DERHANSEN\SfEventMgt\Domain\Repository\CustomNotificationLogRepository $customNotificationLogRepository |
||
99 | */ |
||
100 | public function injectCustomNotificationLogRepository( |
||
105 | |||
106 | /** |
||
107 | * DI for $emailService |
||
108 | * |
||
109 | * @param EmailService $emailService |
||
110 | */ |
||
111 | public function injectEmailService(\DERHANSEN\SfEventMgt\Service\EmailService $emailService) |
||
115 | |||
116 | /** |
||
117 | * DI for $fluidStandaloneService |
||
118 | * |
||
119 | * @param FluidStandaloneService $fluidStandaloneService |
||
120 | */ |
||
121 | public function injectFluidStandaloneService( |
||
126 | |||
127 | /** |
||
128 | * DI for $hashService |
||
129 | * |
||
130 | * @param \TYPO3\CMS\Extbase\Security\Cryptography\HashService $hashService |
||
131 | */ |
||
132 | public function injectHashService(\TYPO3\CMS\Extbase\Security\Cryptography\HashService $hashService) |
||
136 | |||
137 | /** |
||
138 | * DI for $objectManager |
||
139 | * |
||
140 | * @param \TYPO3\CMS\Extbase\Object\ObjectManager $objectManager |
||
141 | */ |
||
142 | public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManager $objectManager) |
||
146 | |||
147 | /** |
||
148 | * DI for $registrationRepository |
||
149 | * |
||
150 | * @param \DERHANSEN\SfEventMgt\Domain\Repository\RegistrationRepository $registrationRepository |
||
151 | */ |
||
152 | public function injectRegistrationRepository( |
||
157 | |||
158 | /** |
||
159 | * @param EventDispatcherInterface $eventDispatcher |
||
160 | */ |
||
161 | public function injectEventDispatcher(EventDispatcherInterface $eventDispatcher) |
||
165 | |||
166 | /** |
||
167 | * Sends a custom notification defined by the given customNotification key |
||
168 | * to all confirmed users of the event |
||
169 | * |
||
170 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event |
||
171 | * @param CustomNotification $customNotification |
||
172 | * @param array $settings Settings |
||
173 | * |
||
174 | * @return int Number of notifications sent |
||
175 | */ |
||
176 | public function sendCustomNotification(Event $event, CustomNotification $customNotification, array $settings = []) |
||
207 | |||
208 | /** |
||
209 | * Returns true if conditions are not met to send a custom notification |
||
210 | * |
||
211 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event |
||
212 | * @param array $settings |
||
213 | * @param CustomNotification $customNotification |
||
214 | * |
||
215 | * @return bool |
||
216 | */ |
||
217 | protected function cantSendCustomNotification($event, $settings, $customNotification) |
||
222 | |||
223 | /** |
||
224 | * Adds a logentry to the custom notification log |
||
225 | * |
||
226 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
||
227 | * @param string $details Details |
||
228 | * @param int $emailsSent E-Mails sent |
||
229 | */ |
||
230 | public function createCustomNotificationLogentry($event, $details, $emailsSent) |
||
239 | |||
240 | /** |
||
241 | * Sends a message to the user based on the given type |
||
242 | * |
||
243 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
||
244 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
||
245 | * @param array $settings Settings |
||
246 | * @param int $type Type |
||
247 | * @param CustomNotification $customNotification |
||
248 | * |
||
249 | * @return bool TRUE if successful, else FALSE |
||
250 | */ |
||
251 | public function sendUserMessage($event, $registration, $settings, $type, $customNotification = null) |
||
351 | |||
352 | /** |
||
353 | * Returns an array with template and subject for the user message |
||
354 | * |
||
355 | * @param array $settings |
||
356 | * @param int $type Type |
||
357 | * @param CustomNotification $customNotification |
||
358 | * @return array |
||
359 | */ |
||
360 | protected function getUserMessageTemplateSubject($settings, $type, $customNotification = null) |
||
402 | |||
403 | /** |
||
404 | * Sends a message to the admin based on the given type |
||
405 | * |
||
406 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
||
407 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
||
408 | * @param array $settings Settings |
||
409 | * @param int $type Type |
||
410 | * |
||
411 | * @return bool TRUE if successful, else FALSE |
||
412 | */ |
||
413 | public function sendAdminMessage($event, $registration, $settings, $type) |
||
485 | |||
486 | /** |
||
487 | * Returns an array with template and subject for the admin message |
||
488 | * |
||
489 | * @param array $settings |
||
490 | * @param int $type Type |
||
491 | * @return array |
||
492 | */ |
||
493 | protected function getAdminMessageTemplateSubject($settings, $type) |
||
523 | |||
524 | /** |
||
525 | * Returns the rendered HTML for the given template |
||
526 | * |
||
527 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
||
528 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
||
529 | * @param string $template Template |
||
530 | * @param array $settings Settings |
||
531 | * @param array $additionalBodyVariables |
||
532 | * @return string |
||
533 | */ |
||
534 | protected function getNotificationBody($event, $registration, $template, $settings, $additionalBodyVariables = []) |
||
551 | } |
||
552 |
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: