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 |
||
| 23 | class NotificationService |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * The object manager |
||
| 27 | * |
||
| 28 | * @var \TYPO3\CMS\Extbase\Object\ObjectManager |
||
| 29 | */ |
||
| 30 | protected $objectManager; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Registration repository |
||
| 34 | * |
||
| 35 | * @var \DERHANSEN\SfEventMgt\Domain\Repository\RegistrationRepository |
||
| 36 | */ |
||
| 37 | protected $registrationRepository = null; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Email Service |
||
| 41 | * |
||
| 42 | * @var \DERHANSEN\SfEventMgt\Service\EmailService |
||
| 43 | */ |
||
| 44 | protected $emailService; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Hash Service |
||
| 48 | * |
||
| 49 | * @var \TYPO3\CMS\Extbase\Security\Cryptography\HashService |
||
| 50 | */ |
||
| 51 | protected $hashService; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * FluidStandaloneService |
||
| 55 | * |
||
| 56 | * @var \DERHANSEN\SfEventMgt\Service\FluidStandaloneService |
||
| 57 | */ |
||
| 58 | protected $fluidStandaloneService; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * CustomNotificationLogRepository |
||
| 62 | * |
||
| 63 | * @var \DERHANSEN\SfEventMgt\Domain\Repository\CustomNotificationLogRepository |
||
| 64 | */ |
||
| 65 | protected $customNotificationLogRepository = null; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * AttachmentService |
||
| 69 | * |
||
| 70 | * @var \DERHANSEN\SfEventMgt\Service\Notification\AttachmentService |
||
| 71 | */ |
||
| 72 | protected $attachmentService; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var \TYPO3\CMS\Extbase\SignalSlot\Dispatcher |
||
| 76 | */ |
||
| 77 | protected $signalSlotDispatcher = null; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * DI for $attachmentService |
||
| 81 | * |
||
| 82 | * @param Notification\AttachmentService $attachmentService |
||
| 83 | */ |
||
| 84 | public function injectAttachmentService( |
||
| 89 | |||
| 90 | /** |
||
| 91 | * DI for $customNotificationLogRepository |
||
| 92 | * |
||
| 93 | * @param \DERHANSEN\SfEventMgt\Domain\Repository\CustomNotificationLogRepository $customNotificationLogRepository |
||
| 94 | */ |
||
| 95 | public function injectCustomNotificationLogRepository( |
||
| 100 | |||
| 101 | /** |
||
| 102 | * DI for $emailService |
||
| 103 | * |
||
| 104 | * @param EmailService $emailService |
||
| 105 | */ |
||
| 106 | public function injectEmailService(\DERHANSEN\SfEventMgt\Service\EmailService $emailService) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * DI for $fluidStandaloneService |
||
| 113 | * |
||
| 114 | * @param FluidStandaloneService $fluidStandaloneService |
||
| 115 | */ |
||
| 116 | public function injectFluidStandaloneService( |
||
| 121 | |||
| 122 | /** |
||
| 123 | * DI for $hashService |
||
| 124 | * |
||
| 125 | * @param \TYPO3\CMS\Extbase\Security\Cryptography\HashService $hashService |
||
| 126 | */ |
||
| 127 | public function injectHashService(\TYPO3\CMS\Extbase\Security\Cryptography\HashService $hashService) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * DI for $objectManager |
||
| 134 | * |
||
| 135 | * @param \TYPO3\CMS\Extbase\Object\ObjectManager $objectManager |
||
| 136 | */ |
||
| 137 | public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManager $objectManager) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * DI for $registrationRepository |
||
| 144 | * |
||
| 145 | * @param \DERHANSEN\SfEventMgt\Domain\Repository\RegistrationRepository $registrationRepository |
||
| 146 | */ |
||
| 147 | public function injectRegistrationRepository( |
||
| 152 | |||
| 153 | /** |
||
| 154 | * DI for $signalSlotDispatcher |
||
| 155 | * |
||
| 156 | * @param Dispatcher $signalSlotDispatcher |
||
| 157 | */ |
||
| 158 | public function injectSignalSlotDispatcher(\TYPO3\CMS\Extbase\SignalSlot\Dispatcher $signalSlotDispatcher) |
||
| 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 |
||
| 168 | * @param CustomNotification $customNotification |
||
| 169 | * @param array $settings Settings |
||
| 170 | * |
||
| 171 | * @return int Number of notifications sent |
||
| 172 | */ |
||
| 173 | public function sendCustomNotification(Event $event, CustomNotification $customNotification, array $settings = []) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Returns true if conditions are not met to send a custom notification |
||
| 209 | * |
||
| 210 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event |
||
| 211 | * @param array $settings |
||
| 212 | * @param CustomNotification $customNotification |
||
| 213 | * |
||
| 214 | * @return bool |
||
| 215 | */ |
||
| 216 | protected function cantSendCustomNotification($event, $settings, $customNotification) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Adds a logentry to the custom notification log |
||
| 224 | * |
||
| 225 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
||
| 226 | * @param string $details Details |
||
| 227 | * @param int $emailsSent E-Mails sent |
||
| 228 | * |
||
| 229 | * @return void |
||
| 230 | */ |
||
| 231 | 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) |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Returns an array with template and subject for the user message |
||
| 343 | * |
||
| 344 | * @param array $settings |
||
| 345 | * @param int $type Type |
||
| 346 | * @param CustomNotification $customNotification |
||
| 347 | * @return array |
||
| 348 | */ |
||
| 349 | protected function getUserMessageTemplateSubject($settings, $type, $customNotification = null) |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Sends a message to the admin based on the given type |
||
| 394 | * |
||
| 395 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
||
| 396 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
||
| 397 | * @param array $settings Settings |
||
| 398 | * @param int $type Type |
||
| 399 | * |
||
| 400 | * @return bool TRUE if successful, else FALSE |
||
| 401 | */ |
||
| 402 | public function sendAdminMessage($event, $registration, $settings, $type) |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Returns an array with template and subject for the admin message |
||
| 471 | * |
||
| 472 | * @param array $settings |
||
| 473 | * @param int $type Type |
||
| 474 | * @return array |
||
| 475 | */ |
||
| 476 | protected function getAdminMessageTemplateSubject($settings, $type) |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Returns the rendered HTML for the given template |
||
| 509 | * |
||
| 510 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
||
| 511 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
||
| 512 | * @param string $template Template |
||
| 513 | * @param array $settings Settings |
||
| 514 | * @param array $additionalBodyVariables |
||
| 515 | * @return string |
||
| 516 | */ |
||
| 517 | protected function getNotificationBody($event, $registration, $template, $settings, $additionalBodyVariables = []) |
||
| 534 | } |
||
| 535 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: