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) |
||
| 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 | * @param array $settings Settings |
||
| 247 | * @param int $type Type |
||
| 248 | * @param CustomNotification $customNotification |
||
| 249 | * |
||
| 250 | * @return bool TRUE if successful, else FALSE |
||
| 251 | */ |
||
| 252 | public function sendUserMessage($event, $registration, $settings, $type, $customNotification = null) |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Returns an array with template and subject for the user message |
||
| 355 | * |
||
| 356 | * @param array $settings |
||
| 357 | * @param int $type Type |
||
| 358 | * @param CustomNotification $customNotification |
||
| 359 | * @return array |
||
| 360 | */ |
||
| 361 | 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: