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 |
||
| 21 | class NotificationService |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * The object manager |
||
| 25 | * |
||
| 26 | * @var \TYPO3\CMS\Extbase\Object\ObjectManager |
||
| 27 | */ |
||
| 28 | protected $objectManager; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Registration repository |
||
| 32 | * |
||
| 33 | * @var \DERHANSEN\SfEventMgt\Domain\Repository\RegistrationRepository |
||
| 34 | */ |
||
| 35 | protected $registrationRepository = null; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Email Service |
||
| 39 | * |
||
| 40 | * @var \DERHANSEN\SfEventMgt\Service\EmailService |
||
| 41 | */ |
||
| 42 | protected $emailService; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Hash Service |
||
| 46 | * |
||
| 47 | * @var \TYPO3\CMS\Extbase\Security\Cryptography\HashService |
||
| 48 | */ |
||
| 49 | protected $hashService; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * FluidStandaloneService |
||
| 53 | * |
||
| 54 | * @var \DERHANSEN\SfEventMgt\Service\FluidStandaloneService |
||
| 55 | */ |
||
| 56 | protected $fluidStandaloneService; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * CustomNotificationLogRepository |
||
| 60 | * |
||
| 61 | * @var \DERHANSEN\SfEventMgt\Domain\Repository\CustomNotificationLogRepository |
||
| 62 | */ |
||
| 63 | protected $customNotificationLogRepository = null; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * AttachmentService |
||
| 67 | * |
||
| 68 | * @var \DERHANSEN\SfEventMgt\Service\Notification\AttachmentService |
||
| 69 | */ |
||
| 70 | protected $attachmentService; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var \TYPO3\CMS\Extbase\SignalSlot\Dispatcher |
||
| 74 | */ |
||
| 75 | protected $signalSlotDispatcher = null; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * DI for $attachmentService |
||
| 79 | * |
||
| 80 | * @param Notification\AttachmentService $attachmentService |
||
| 81 | */ |
||
| 82 | public function injectAttachmentService( |
||
| 87 | |||
| 88 | /** |
||
| 89 | * DI for $customNotificationLogRepository |
||
| 90 | * |
||
| 91 | * @param \DERHANSEN\SfEventMgt\Domain\Repository\CustomNotificationLogRepository $customNotificationLogRepository |
||
| 92 | */ |
||
| 93 | public function injectCustomNotificationLogRepository( |
||
| 98 | 2 | ||
| 99 | /** |
||
| 100 | 6 | * DI for $emailService |
|
| 101 | * |
||
| 102 | 6 | * @param EmailService $emailService |
|
| 103 | 6 | */ |
|
| 104 | 6 | public function injectEmailService(\DERHANSEN\SfEventMgt\Service\EmailService $emailService) |
|
| 108 | 2 | ||
| 109 | 2 | /** |
|
| 110 | 2 | * DI for $fluidStandaloneService |
|
| 111 | 2 | * |
|
| 112 | * @param FluidStandaloneService $fluidStandaloneService |
||
| 113 | 2 | */ |
|
| 114 | 2 | public function injectFluidStandaloneService( |
|
| 119 | 6 | ||
| 120 | /** |
||
| 121 | * DI for $hashService |
||
| 122 | * |
||
| 123 | * @param \TYPO3\CMS\Extbase\Security\Cryptography\HashService $hashService |
||
| 124 | */ |
||
| 125 | public function injectHashService(\TYPO3\CMS\Extbase\Security\Cryptography\HashService $hashService) |
||
| 129 | |||
| 130 | /** |
||
| 131 | 8 | * DI for $objectManager |
|
| 132 | * |
||
| 133 | 8 | * @param \TYPO3\CMS\Extbase\Object\ObjectManager $objectManager |
|
| 134 | */ |
||
| 135 | public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManager $objectManager) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * DI for $registrationRepository |
||
| 142 | * |
||
| 143 | * @param \DERHANSEN\SfEventMgt\Domain\Repository\RegistrationRepository $registrationRepository |
||
| 144 | */ |
||
| 145 | 2 | public function injectRegistrationRepository( |
|
| 150 | 2 | ||
| 151 | 2 | /** |
|
| 152 | 2 | * DI for $signalSlotDispatcher |
|
| 153 | 2 | * |
|
| 154 | * @param Dispatcher $signalSlotDispatcher |
||
| 155 | */ |
||
| 156 | public function injectSignalSlotDispatcher(\TYPO3\CMS\Extbase\SignalSlot\Dispatcher $signalSlotDispatcher) |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Sends a custom notification defined by the given customNotification key |
||
| 163 | * to all confirmed users of the event |
||
| 164 | * |
||
| 165 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
||
| 166 | 34 | * @param string $customNotification CustomNotification |
|
| 167 | * @param array $settings Settings |
||
| 168 | 34 | * |
|
| 169 | * @return int Number of notifications sent |
||
| 170 | 34 | */ |
|
| 171 | 4 | public function sendCustomNotification($event, $customNotification, $settings) |
|
| 198 | |||
| 199 | /** |
||
| 200 | * Returns true if conditions are not met to send a custom notification |
||
| 201 | * |
||
| 202 | 34 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event |
|
| 203 | * @param array $settings |
||
| 204 | 34 | * @param string $customNotification |
|
| 205 | 34 | * |
|
| 206 | * @return bool |
||
| 207 | 34 | */ |
|
| 208 | 6 | protected function cantSendCustomNotification($event, $settings, $customNotification) |
|
| 212 | 6 | ||
| 213 | 6 | /** |
|
| 214 | 6 | * Adds a logentry to the custom notification log |
|
| 215 | 22 | * |
|
| 216 | 6 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
|
| 217 | 6 | * @param string $details Details |
|
| 218 | 6 | * @param int $emailsSent E-Mails sent |
|
| 219 | 16 | * |
|
| 220 | * @return void |
||
| 221 | */ |
||
| 222 | public function createCustomNotificationLogentry($event, $details, $emailsSent) |
||
| 231 | 34 | ||
| 232 | /** |
||
| 233 | 34 | * Sends a message to the user based on the given type |
|
| 234 | * |
||
| 235 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
||
| 236 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
||
| 237 | * @param array $settings Settings |
||
| 238 | * @param int $type Type |
||
| 239 | * @param string $customNotification CustomNotification |
||
| 240 | * |
||
| 241 | * @return bool TRUE if successful, else FALSE |
||
| 242 | */ |
||
| 243 | public function sendUserMessage($event, $registration, $settings, $type, $customNotification = '') |
||
| 315 | 22 | ||
| 316 | /** |
||
| 317 | * Returns an array with template and subject for the user message |
||
| 318 | * |
||
| 319 | 22 | * @param array $settings |
|
| 320 | 22 | * @param int $type Type |
|
| 321 | 22 | * @param string $customNotification |
|
| 322 | * @return array |
||
| 323 | 52 | */ |
|
| 324 | protected function getUserMessageTemplateSubject($settings, $type, $customNotification) |
||
| 358 | 60 | ||
| 359 | 60 | /** |
|
| 360 | 60 | * Sends a message to the admin based on the given type |
|
| 361 | 60 | * |
|
| 362 | 60 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
|
| 363 | 60 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
|
| 364 | * @param array $settings Settings |
||
| 365 | * @param int $type Type |
||
| 366 | * |
||
| 367 | * @return bool TRUE if successful, else FALSE |
||
| 368 | */ |
||
| 369 | public function sendAdminMessage($event, $registration, $settings, $type) |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Returns an array with template and subject for the admin message |
||
| 438 | * |
||
| 439 | * @param array $settings |
||
| 440 | * @param int $type Type |
||
| 441 | * @return array |
||
| 442 | */ |
||
| 443 | protected function getAdminMessageTemplateSubject($settings, $type) |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Returns the rendered HTML for the given template |
||
| 476 | * |
||
| 477 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
||
| 478 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
||
| 479 | * @param string $template Template |
||
| 480 | * @param array $settings Settings |
||
| 481 | * |
||
| 482 | * @return string |
||
| 483 | */ |
||
| 484 | protected function getNotificationBody($event, $registration, $template, $settings) |
||
| 500 | } |
||
| 501 |