Complex classes like NotificationsService 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 NotificationsService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class NotificationsService { |
||
| 16 | |||
| 17 | const QUEUE_NAME = 'notifications'; |
||
| 18 | |||
| 19 | /** @var \Elgg\Notifications\SubscriptionsService */ |
||
| 20 | protected $subscriptions; |
||
| 21 | |||
| 22 | /** @var \Elgg\Queue\Queue */ |
||
| 23 | protected $queue; |
||
| 24 | |||
| 25 | /** @var \Elgg\PluginHooksService */ |
||
| 26 | protected $hooks; |
||
| 27 | |||
| 28 | /** @var \ElggSession */ |
||
| 29 | protected $session; |
||
| 30 | |||
| 31 | /** @var array Registered notification events */ |
||
| 32 | protected $events = array(); |
||
| 33 | |||
| 34 | /** @var array Registered notification methods */ |
||
| 35 | protected $methods = array(); |
||
| 36 | |||
| 37 | /** @var array Deprecated notification handlers */ |
||
| 38 | protected $deprHandlers = array(); |
||
| 39 | |||
| 40 | /** @var array Deprecated message subjects */ |
||
| 41 | protected $deprSubjects = array(); |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Constructor |
||
| 45 | * |
||
| 46 | * @param \Elgg\Notifications\SubscriptionsService $subscriptions Subscription service |
||
| 47 | * @param \Elgg\Queue\Queue $queue Queue |
||
| 48 | * @param \Elgg\PluginHooksService $hooks Plugin hook service |
||
| 49 | * @param \ElggSession $session Session service |
||
| 50 | */ |
||
| 51 | 10 | public function __construct(\Elgg\Notifications\SubscriptionsService $subscriptions, |
|
| 58 | |||
| 59 | /** |
||
| 60 | * @see elgg_register_notification_event() |
||
| 61 | * @access private |
||
| 62 | */ |
||
| 63 | 7 | public function registerEvent($type, $subtype, array $actions = array()) { |
|
| 79 | |||
| 80 | /** |
||
| 81 | * @see elgg_unregister_notification_event() |
||
| 82 | * @access private |
||
| 83 | */ |
||
| 84 | 1 | public function unregisterEvent($type, $subtype) { |
|
| 94 | |||
| 95 | /** |
||
| 96 | * @access private |
||
| 97 | */ |
||
| 98 | 2 | public function getEvents() { |
|
| 101 | |||
| 102 | /** |
||
| 103 | * @see elgg_register_notification_method() |
||
| 104 | * @access private |
||
| 105 | */ |
||
| 106 | 2 | public function registerMethod($name) { |
|
| 109 | |||
| 110 | /** |
||
| 111 | * @see elgg_unregister_notification_method() |
||
| 112 | * @access private |
||
| 113 | */ |
||
| 114 | 1 | public function unregisterMethod($name) { |
|
| 121 | |||
| 122 | /** |
||
| 123 | * @access private |
||
| 124 | */ |
||
| 125 | 2 | public function getMethods() { |
|
| 128 | |||
| 129 | /** |
||
| 130 | * Add a notification event to the queue |
||
| 131 | * |
||
| 132 | * @param string $action Action name |
||
| 133 | * @param string $type Type of the object of the action |
||
| 134 | * @param \ElggData $object The object of the action |
||
| 135 | * @return void |
||
| 136 | * @access private |
||
| 137 | */ |
||
| 138 | 5 | public function enqueueEvent($action, $type, $object) { |
|
| 163 | |||
| 164 | /** |
||
| 165 | * Pull notification events from queue until stop time is reached |
||
| 166 | * |
||
| 167 | * @param int $stopTime The Unix time to stop sending notifications |
||
| 168 | * @return int The number of notification events handled |
||
| 169 | * @access private |
||
| 170 | */ |
||
| 171 | 3 | public function processQueue($stopTime) { |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Sends the notifications based on subscriptions |
||
| 213 | * |
||
| 214 | * @param \Elgg\Notifications\Event $event Notification event |
||
| 215 | * @param array $subscriptions Subscriptions for this event |
||
| 216 | * @return int The number of notifications handled |
||
| 217 | * @access private |
||
| 218 | */ |
||
| 219 | 1 | protected function sendNotifications($event, $subscriptions) { |
|
| 237 | |||
| 238 | /** |
||
| 239 | * Send a notification to a subscriber |
||
| 240 | * |
||
| 241 | * @param \Elgg\Notifications\Event $event The notification event |
||
| 242 | * @param int $guid The guid of the subscriber |
||
| 243 | * @param string $method The notification method |
||
| 244 | * @return bool |
||
| 245 | * @access private |
||
| 246 | */ |
||
| 247 | 3 | protected function sendNotification(\Elgg\Notifications\Event $event, $guid, $method) { |
|
| 307 | |||
| 308 | /** |
||
| 309 | * Register a deprecated notification handler |
||
| 310 | * |
||
| 311 | * @param string $method Method name |
||
| 312 | * @param string $handler Handler callback |
||
| 313 | * @return void |
||
| 314 | */ |
||
| 315 | public function registerDeprecatedHandler($method, $handler) { |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Get a deprecated notification handler callback |
||
| 321 | * |
||
| 322 | * @param string $method Method name |
||
| 323 | * @return callback|null |
||
| 324 | */ |
||
| 325 | public function getDeprecatedHandler($method) { |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Provides a way to incrementally wean Elgg's notifications code from the |
||
| 335 | * global $NOTIFICATION_HANDLERS |
||
| 336 | * |
||
| 337 | * @return array |
||
| 338 | */ |
||
| 339 | public function getMethodsAsDeprecatedGlobal() { |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Get the notification body using a pre-Elgg 1.9 plugin hook |
||
| 349 | * |
||
| 350 | * @param \Elgg\Notifications\Notification $notification Notification |
||
| 351 | * @param \Elgg\Notifications\Event $event Event |
||
| 352 | * @param string $method Method |
||
| 353 | * @return \Elgg\Notifications\Notification |
||
| 354 | */ |
||
| 355 | protected function getDeprecatedNotificationBody(\Elgg\Notifications\Notification $notification, \Elgg\Notifications\Event $event, $method) { |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Set message subject for deprecated notification code |
||
| 376 | * |
||
| 377 | * @param string $type Entity type |
||
| 378 | * @param string $subtype Entity subtype |
||
| 379 | * @param string $subject Subject line |
||
| 380 | * @return void |
||
| 381 | */ |
||
| 382 | public function setDeprecatedNotificationSubject($type, $subtype, $subject) { |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Get the deprecated subject |
||
| 399 | * |
||
| 400 | * @param string $type Entity type |
||
| 401 | * @param string $subtype Entity subtype |
||
| 402 | * @return string |
||
| 403 | */ |
||
| 404 | protected function getDeprecatedNotificationSubject($type, $subtype) { |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Is someone using the deprecated override |
||
| 425 | * |
||
| 426 | * @param \Elgg\Notifications\Event $event Event |
||
| 427 | * @return boolean |
||
| 428 | */ |
||
| 429 | 1 | protected function existsDeprecatedNotificationOverride(\Elgg\Notifications\Event $event) { |
|
| 447 | } |
||
| 448 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.