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 |
||
| 26 | class NotificationService |
||
| 27 | { |
||
| 28 | |||
| 29 | /** |
||
| 30 | * The object manager |
||
| 31 | * |
||
| 32 | * @var \TYPO3\CMS\Extbase\Object\ObjectManager |
||
| 33 | * @inject |
||
| 34 | */ |
||
| 35 | protected $objectManager; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Registration repository |
||
| 39 | * |
||
| 40 | * @var \DERHANSEN\SfEventMgt\Domain\Repository\RegistrationRepository |
||
| 41 | * @inject |
||
| 42 | */ |
||
| 43 | protected $registrationRepository = null; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Email Service |
||
| 47 | * |
||
| 48 | * @var \DERHANSEN\SfEventMgt\Service\EmailService |
||
| 49 | * @inject |
||
| 50 | */ |
||
| 51 | protected $emailService; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Hash Service |
||
| 55 | * |
||
| 56 | * @var \TYPO3\CMS\Extbase\Security\Cryptography\HashService |
||
| 57 | * @inject |
||
| 58 | */ |
||
| 59 | protected $hashService; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * FluidStandaloneService |
||
| 63 | * |
||
| 64 | * @var \DERHANSEN\SfEventMgt\Service\FluidStandaloneService |
||
| 65 | * @inject |
||
| 66 | */ |
||
| 67 | protected $fluidStandaloneService; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * CustomNotificationLogRepository |
||
| 71 | * |
||
| 72 | * @var \DERHANSEN\SfEventMgt\Domain\Repository\CustomNotificationLogRepository |
||
| 73 | * @inject |
||
| 74 | */ |
||
| 75 | protected $customNotificationLogRepository = null; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * AttachmentService |
||
| 79 | * |
||
| 80 | * @var \DERHANSEN\SfEventMgt\Service\Notification\AttachmentService |
||
| 81 | * @inject |
||
| 82 | */ |
||
| 83 | protected $attachmentService; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Sends a custom notification defined by the given customNotification key |
||
| 87 | * to all confirmed users of the event |
||
| 88 | * |
||
| 89 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
||
| 90 | * @param string $customNotification CustomNotification |
||
| 91 | * @param array $settings Settings |
||
| 92 | * |
||
| 93 | * @return int Number of notifications sent |
||
| 94 | */ |
||
| 95 | 4 | public function sendCustomNotification($event, $customNotification, $settings) |
|
| 121 | |||
| 122 | /** |
||
| 123 | * Returns true if conditions are not met to send a custom notification |
||
| 124 | * |
||
| 125 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event |
||
| 126 | * @param array $settings |
||
| 127 | * @param string $customNotification |
||
| 128 | * |
||
| 129 | * @return bool |
||
| 130 | */ |
||
| 131 | 4 | protected function cantSendCustomNotification($event, $settings, $customNotification) |
|
| 135 | |||
| 136 | /** |
||
| 137 | * Adds a logentry to the custom notification log |
||
| 138 | * |
||
| 139 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
||
| 140 | * @param string $details Details |
||
| 141 | * @param int $emailsSent E-Mails sent |
||
| 142 | * |
||
| 143 | * @return void |
||
| 144 | */ |
||
| 145 | 1 | public function createCustomNotificationLogentry($event, $details, $emailsSent) |
|
| 154 | |||
| 155 | /** |
||
| 156 | * Sends a message to the user based on the given type |
||
| 157 | * |
||
| 158 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
||
| 159 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
||
| 160 | * @param array $settings Settings |
||
| 161 | * @param int $type Type |
||
| 162 | * @param string $customNotification CustomNotification |
||
| 163 | * |
||
| 164 | * @return bool TRUE if successful, else FALSE |
||
| 165 | */ |
||
| 166 | 17 | public function sendUserMessage($event, $registration, $settings, $type, $customNotification = '') |
|
| 193 | |||
| 194 | /** |
||
| 195 | * Returns an array with template and subject for the user message |
||
| 196 | * |
||
| 197 | * @param array $settings |
||
| 198 | * @param int $type Type |
||
| 199 | * @param string $customNotification |
||
| 200 | * @return array |
||
| 201 | */ |
||
| 202 | 17 | protected function getUserMessageTemplateSubject($settings, $type, $customNotification) |
|
| 235 | |||
| 236 | /** |
||
| 237 | * Sends a message to the admin based on the given type |
||
| 238 | * |
||
| 239 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
||
| 240 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
||
| 241 | * @param array $settings Settings |
||
| 242 | * @param int $type Type |
||
| 243 | * |
||
| 244 | * @return bool TRUE if successful, else FALSE |
||
| 245 | */ |
||
| 246 | 26 | public function sendAdminMessage($event, $registration, $settings, $type) |
|
| 290 | |||
| 291 | /** |
||
| 292 | * Returns an array with template and subject for the admin message |
||
| 293 | * |
||
| 294 | * @param array $settings |
||
| 295 | * @param int $type Type |
||
| 296 | * @return array |
||
| 297 | */ |
||
| 298 | 26 | protected function getAdminMessageTemplateSubject($settings, $type) |
|
| 327 | |||
| 328 | /** |
||
| 329 | * Returns the rendered HTML for the given template |
||
| 330 | * |
||
| 331 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Event $event Event |
||
| 332 | * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration $registration Registration |
||
| 333 | * @param string $template Template |
||
| 334 | * @param array $settings Settings |
||
| 335 | * |
||
| 336 | * @return string |
||
| 337 | */ |
||
| 338 | 30 | protected function getNotificationBody($event, $registration, $template, $settings) |
|
| 365 | } |
||
| 366 |