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 | 8 | /** |
|
| 96 | * DI for $customNotificationLogRepository |
||
| 97 | 8 | * |
|
| 98 | 2 | * @param \DERHANSEN\SfEventMgt\Domain\Repository\CustomNotificationLogRepository $customNotificationLogRepository |
|
| 99 | */ |
||
| 100 | 6 | public function injectCustomNotificationLogRepository( |
|
| 105 | |||
| 106 | 6 | /** |
|
| 107 | 2 | * DI for $emailService |
|
| 108 | 2 | * |
|
| 109 | 2 | * @param EmailService $emailService |
|
| 110 | 2 | */ |
|
| 111 | 2 | public function injectEmailService(\DERHANSEN\SfEventMgt\Service\EmailService $emailService) |
|
| 115 | 2 | ||
| 116 | 2 | /** |
|
| 117 | 2 | * DI for $fluidStandaloneService |
|
| 118 | 6 | * |
|
| 119 | 6 | * @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 | 8 | */ |
|
| 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 | 2 | /** |
|
| 148 | 2 | * DI for $registrationRepository |
|
| 149 | 2 | * |
|
| 150 | 2 | * @param \DERHANSEN\SfEventMgt\Domain\Repository\RegistrationRepository $registrationRepository |
|
| 151 | 2 | */ |
|
| 152 | 2 | public function injectRegistrationRepository( |
|
| 157 | |||
| 158 | /** |
||
| 159 | * @param EventDispatcherInterface $eventDispatcher |
||
| 160 | */ |
||
| 161 | public function injectEventDispatcher(EventDispatcherInterface $eventDispatcher) |
||
| 165 | |||
| 166 | 34 | /** |
|
| 167 | * Sends a custom notification defined by the given customNotification key |
||
| 168 | 34 | * to all confirmed users of the event |
|
| 169 | * |
||
| 170 | 34 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event |
|
| 171 | 4 | * @param CustomNotification $customNotification |
|
| 172 | * @param array $settings Settings |
||
| 173 | * |
||
| 174 | 30 | * @return int Number of notifications sent |
|
| 175 | 20 | */ |
|
| 176 | 20 | public function sendCustomNotification(Event $event, CustomNotification $customNotification, array $settings = []) |
|
| 207 | 34 | ||
| 208 | 6 | /** |
|
| 209 | 6 | * Returns true if conditions are not met to send a custom notification |
|
| 210 | 6 | * |
|
| 211 | 28 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event |
|
| 212 | 6 | * @param array $settings |
|
| 213 | 6 | * @param CustomNotification $customNotification |
|
| 214 | 6 | * |
|
| 215 | 22 | * @return bool |
|
| 216 | 6 | */ |
|
| 217 | 6 | protected function cantSendCustomNotification($event, $settings, $customNotification) |
|
| 222 | |||
| 223 | 16 | /** |
|
| 224 | 2 | * Adds a logentry to the custom notification log |
|
| 225 | 2 | * |
|
| 226 | 2 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
|
| 227 | 14 | * @param string $details Details |
|
| 228 | 14 | * @param int $emailsSent E-Mails sent |
|
| 229 | 14 | */ |
|
| 230 | public function createCustomNotificationLogentry($event, $details, $emailsSent) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Sends a message to the user based on the given type |
||
| 243 | * |
||
| 244 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
||
| 245 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
||
| 246 | 52 | * @param array $settings Settings |
|
| 247 | * @param int $type Type |
||
| 248 | 52 | * @param CustomNotification $customNotification |
|
| 249 | * |
||
| 250 | * @return bool TRUE if successful, else FALSE |
||
| 251 | 52 | */ |
|
| 252 | 50 | public function sendUserMessage($event, $registration, $settings, $type, $customNotification = null) |
|
| 352 | 60 | ||
| 353 | 60 | /** |
|
| 354 | 60 | * Returns an array with template and subject for the user message |
|
| 355 | 60 | * |
|
| 356 | 60 | * @param array $settings |
|
| 357 | 60 | * @param int $type Type |
|
| 358 | 60 | * @param CustomNotification $customNotification |
|
| 359 | 60 | * @return array |
|
| 360 | 60 | */ |
|
| 361 | 60 | protected function getUserMessageTemplateSubject($settings, $type, $customNotification = null) |
|
| 407 | |||
| 408 | /** |
||
| 409 | * Sends a message to the admin based on the given type |
||
| 410 | * |
||
| 411 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
||
| 412 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
||
| 413 | * @param array $settings Settings |
||
| 414 | * @param int $type Type |
||
| 415 | * |
||
| 416 | * @return bool TRUE if successful, else FALSE |
||
| 417 | */ |
||
| 418 | public function sendAdminMessage($event, $registration, $settings, $type) |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Returns an array with template and subject for the admin message |
||
| 493 | * |
||
| 494 | * @param array $settings |
||
| 495 | * @param int $type Type |
||
| 496 | * @return array |
||
| 497 | */ |
||
| 498 | protected function getAdminMessageTemplateSubject($settings, $type) |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Returns the rendered HTML for the given template |
||
| 535 | * |
||
| 536 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
||
| 537 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
||
| 538 | * @param string $template Template |
||
| 539 | * @param array $settings Settings |
||
| 540 | * @param array $additionalBodyVariables |
||
| 541 | * @return string |
||
| 542 | */ |
||
| 543 | protected function getNotificationBody($event, $registration, $template, $settings, $additionalBodyVariables = []) |
||
| 560 | } |
||
| 561 |
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: