Complex classes like EE_Messages_Queue 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 EE_Messages_Queue, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class EE_Messages_Queue { |
||
| 18 | |||
| 19 | |||
| 20 | /** |
||
| 21 | * @type string reference for sending action |
||
| 22 | */ |
||
| 23 | const action_sending = 'sending'; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @type string reference for generation action |
||
| 27 | */ |
||
| 28 | const action_generating = 'generation'; |
||
| 29 | |||
| 30 | |||
| 31 | |||
| 32 | /** |
||
| 33 | * @type EE_Message_Repository $_message_repository |
||
| 34 | */ |
||
| 35 | protected $_message_repository; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Sets the limit of how many messages are generated per process. |
||
| 39 | * @type int |
||
| 40 | */ |
||
| 41 | protected $_batch_count; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Sets the limit of how many messages can be sent per hour. |
||
| 45 | * @type int |
||
| 46 | */ |
||
| 47 | protected $_rate_limit; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * This is an array of cached queue items being stored in this object. |
||
| 51 | * The array keys will be the ID of the EE_Message in the db if saved. If the EE_Message |
||
| 52 | * is not saved to the db then its key will be an increment of "UNS" (i.e. UNS1, UNS2 etc.) |
||
| 53 | * @type EE_Message[] |
||
| 54 | */ |
||
| 55 | protected $_cached_queue_items; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Tracks the number of unsaved queue items. |
||
| 59 | * @type int |
||
| 60 | */ |
||
| 61 | protected $_unsaved_count = 0; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * used to record if a do_messenger_hooks has already been called for a message type. This prevents multiple |
||
| 65 | * hooks getting fired if users have setup their action/filter hooks to prevent duplicate calls. |
||
| 66 | * |
||
| 67 | * @type array |
||
| 68 | */ |
||
| 69 | protected $_did_hook = array(); |
||
| 70 | |||
| 71 | |||
| 72 | |||
| 73 | /** |
||
| 74 | * Constructor. |
||
| 75 | * Setup all the initial properties and load a EE_Message_Repository. |
||
| 76 | * |
||
| 77 | * @param \EE_Message_Repository $message_repository |
||
| 78 | */ |
||
| 79 | public function __construct( EE_Message_Repository $message_repository ) { |
||
| 84 | |||
| 85 | |||
| 86 | |||
| 87 | /** |
||
| 88 | * Add a EE_Message object to the queue |
||
| 89 | * |
||
| 90 | * @param EE_Message $message |
||
| 91 | * @param array $data This will be an array of data to attach to the object in the repository. If the |
||
| 92 | * object is persisted, this data will be saved on an extra_meta object related to |
||
| 93 | * EE_Message. |
||
| 94 | * @param bool $preview Whether this EE_Message represents a preview or not. |
||
| 95 | * @param bool $test_send This indicates whether to do a test send instead of actual send. A test send will |
||
| 96 | * use the messenger send method but typically is based on preview data. |
||
| 97 | * @return bool Whether the message was successfully added to the repository or not. |
||
| 98 | */ |
||
| 99 | public function add( EE_Message $message, $data = array(), $preview = false, $test_send = false ) { |
||
| 104 | |||
| 105 | |||
| 106 | |||
| 107 | |||
| 108 | /** |
||
| 109 | * Removes EE_Message from _queue that matches the given EE_Message if the pointer is on a matching EE_Message |
||
| 110 | * @param EE_Message $message The message to detach from the queue |
||
| 111 | * @param bool $persist This flag indicates whether to attempt to delete the object from the db as well. |
||
| 112 | * @return bool |
||
| 113 | */ |
||
| 114 | public function remove( EE_Message $message, $persist = false ) { |
||
| 131 | |||
| 132 | |||
| 133 | |||
| 134 | |||
| 135 | /** |
||
| 136 | * Persists all queued EE_Message objects to the db. |
||
| 137 | * @return array() @see EE_Messages_Repository::saveAll() for return values. |
||
|
|
|||
| 138 | */ |
||
| 139 | public function save() { |
||
| 142 | |||
| 143 | |||
| 144 | |||
| 145 | |||
| 146 | |||
| 147 | /** |
||
| 148 | * @return EE_Message_Repository |
||
| 149 | */ |
||
| 150 | public function get_message_repository() { |
||
| 153 | |||
| 154 | |||
| 155 | |||
| 156 | |||
| 157 | /** |
||
| 158 | * This does the following things: |
||
| 159 | * 1. Checks if there is a lock on generation (prevents race conditions). If there is a lock then exits (return false). |
||
| 160 | * 2. If no lock, sets lock, then retrieves a batch of non-generated EE_Message objects and adds to queue |
||
| 161 | * 3. Returns bool. True = batch ready. False = no batch ready (or nothing available for generation). |
||
| 162 | * |
||
| 163 | * Note: Callers should make sure they release the lock otherwise batch generation will be prevented from continuing. |
||
| 164 | * The lock is on a transient that is set to expire after one hour as a fallback in case locks are not removed. |
||
| 165 | * |
||
| 166 | * @return bool true if successfully retrieved batch, false no batch ready. |
||
| 167 | */ |
||
| 168 | public function get_batch_to_generate() { |
||
| 196 | |||
| 197 | |||
| 198 | /** |
||
| 199 | * This does the following things: |
||
| 200 | * 1. Checks if there is a lock on sending (prevents race conditions). If there is a lock then exits (return false). |
||
| 201 | * 2. Grabs the allowed number of messages to send for the rate_limit. If cannot send any more messages, then return false. |
||
| 202 | * 2. If no lock, sets lock, then retrieves a batch of EE_Message objects, adds to queue and triggers execution. |
||
| 203 | * 3. On success or unsuccessful send, sets status appropriately. |
||
| 204 | * 4. Saves messages via the queue |
||
| 205 | * 5. Releases lock. |
||
| 206 | * |
||
| 207 | * @return bool true on success, false if something preventing sending (i.e. lock set). Note: true does not necessarily |
||
| 208 | * mean that all messages were successfully sent. It just means that this method successfully completed. |
||
| 209 | * On true, client may want to call $this->count_STS_in_queue( EEM_Message::status_failed ) to see if |
||
| 210 | * any failed EE_Message objects. Each failed message object will also have a saved error message on it |
||
| 211 | * to assist with notifying user. |
||
| 212 | */ |
||
| 213 | public function get_to_send_batch_and_send() { |
||
| 252 | |||
| 253 | |||
| 254 | |||
| 255 | |||
| 256 | /** |
||
| 257 | * Locks the queue so that no other queues can call the "batch" methods. |
||
| 258 | * |
||
| 259 | * @param string $type The type of queue being locked. |
||
| 260 | */ |
||
| 261 | public function lock_queue( $type = EE_Messages_Queue::action_generating ) { |
||
| 264 | |||
| 265 | |||
| 266 | |||
| 267 | |||
| 268 | /** |
||
| 269 | * Unlocks the queue so that batch methods can be used. |
||
| 270 | * |
||
| 271 | * @param string $type The type of queue being unlocked. |
||
| 272 | */ |
||
| 273 | public function unlock_queue( $type = EE_Messages_Queue::action_generating ) { |
||
| 276 | |||
| 277 | |||
| 278 | |||
| 279 | |||
| 280 | /** |
||
| 281 | * Retrieve the key used for the lock transient. |
||
| 282 | * @param string $type The type of lock. |
||
| 283 | * @return string |
||
| 284 | */ |
||
| 285 | protected function _get_lock_key( $type = EE_Messages_Queue::action_generating ) { |
||
| 288 | |||
| 289 | |||
| 290 | |||
| 291 | |||
| 292 | /** |
||
| 293 | * Retrieve the expiry time for the lock transient. |
||
| 294 | * @param string $type The type of lock |
||
| 295 | * @return int time to expiry in seconds. |
||
| 296 | */ |
||
| 297 | protected function _get_lock_expiry( $type = EE_Messages_Queue::action_generating ) { |
||
| 300 | |||
| 301 | |||
| 302 | /** |
||
| 303 | * Returns the key used for rate limit transient. |
||
| 304 | * @return string |
||
| 305 | */ |
||
| 306 | protected function _get_rate_limit_key() { |
||
| 309 | |||
| 310 | |||
| 311 | /** |
||
| 312 | * Returns the rate limit expiry time. |
||
| 313 | * @return int |
||
| 314 | */ |
||
| 315 | protected function _get_rate_limit_expiry() { |
||
| 318 | |||
| 319 | |||
| 320 | |||
| 321 | |||
| 322 | /** |
||
| 323 | * Returns the default rate limit for sending messages. |
||
| 324 | * @return int |
||
| 325 | */ |
||
| 326 | protected function _default_rate_limit() { |
||
| 329 | |||
| 330 | |||
| 331 | |||
| 332 | |||
| 333 | /** |
||
| 334 | * Return the orderby array for priority. |
||
| 335 | * @return array |
||
| 336 | */ |
||
| 337 | protected function _get_priority_orderby() { |
||
| 343 | |||
| 344 | |||
| 345 | |||
| 346 | |||
| 347 | /** |
||
| 348 | * Returns whether batch methods are "locked" or not. |
||
| 349 | * |
||
| 350 | * @param string $type The type of lock being checked for. |
||
| 351 | * @return bool |
||
| 352 | */ |
||
| 353 | public function is_locked( $type = EE_Messages_Queue::action_generating ) { |
||
| 380 | |||
| 381 | |||
| 382 | |||
| 383 | |||
| 384 | |||
| 385 | |||
| 386 | |||
| 387 | /** |
||
| 388 | * Retrieves the rate limit that may be cached as a transient. |
||
| 389 | * If the rate limit is not set, then this sets the default rate limit and expiry and returns it. |
||
| 390 | * @return int |
||
| 391 | */ |
||
| 392 | public function get_rate_limit() { |
||
| 399 | |||
| 400 | |||
| 401 | |||
| 402 | |||
| 403 | /** |
||
| 404 | * This updates existing rate limit with the new limit which is the old minus the batch. |
||
| 405 | * @param int $batch_completed This sets the new rate limit based on the given batch that was completed. |
||
| 406 | */ |
||
| 407 | public function set_rate_limit( $batch_completed ) { |
||
| 414 | |||
| 415 | |||
| 416 | /** |
||
| 417 | * This method checks the queue for ANY EE_Message objects with a priority matching the given priority passed in. |
||
| 418 | * If that exists, then we immediately initiate a non-blocking request to do the requested action type. |
||
| 419 | * |
||
| 420 | * Note: Keep in mind that there is the possibility that the request will not execute if there is already another request |
||
| 421 | * running on a queue for the given task. |
||
| 422 | * @param string $task This indicates what type of request is going to be initiated. |
||
| 423 | * @param int $priority This indicates the priority that triggers initiating the request. |
||
| 424 | */ |
||
| 425 | public function initiate_request_by_priority( $task = 'generate', $priority = EEM_Message::priority_high ) { |
||
| 460 | |||
| 461 | |||
| 462 | |||
| 463 | /** |
||
| 464 | * Loops through the EE_Message objects in the _queue and calls the messenger send methods for each message. |
||
| 465 | * |
||
| 466 | * @param bool $save Used to indicate whether to save the message queue after sending |
||
| 467 | * (default will save). |
||
| 468 | * @param mixed $sending_messenger (optional) When the sending messenger is different than |
||
| 469 | * what is on the EE_Message object in the queue. |
||
| 470 | * For instance, showing the browser view of an email message, |
||
| 471 | * or giving a pdf generated view of an html document. |
||
| 472 | * This should be an instance of EE_messenger but if you call this method |
||
| 473 | * intending it to be a sending messenger but a valid one could not be retrieved |
||
| 474 | * then send in an instance of EE_Error that contains the related error message. |
||
| 475 | * @param bool|int $by_priority When set, this indicates that only messages |
||
| 476 | * matching the given priority should be executed. |
||
| 477 | * |
||
| 478 | * @return int Number of messages sent. Note, 0 does not mean that no messages were processed. |
||
| 479 | * Also, if the messenger is an request type messenger (or a preview), |
||
| 480 | * its entirely possible that the messenger will exit before |
||
| 481 | */ |
||
| 482 | public function execute( $save = true, $sending_messenger = null, $by_priority = false ) { |
||
| 533 | |||
| 534 | |||
| 535 | |||
| 536 | /** |
||
| 537 | * _process_message |
||
| 538 | * |
||
| 539 | * @param EE_Message $message |
||
| 540 | * @param mixed $sending_messenger (optional) |
||
| 541 | * @return bool |
||
| 542 | */ |
||
| 543 | protected function _process_message( EE_Message $message, $sending_messenger = null ) { |
||
| 570 | |||
| 571 | |||
| 572 | |||
| 573 | /** |
||
| 574 | * The intention of this method is to count how many EE_Message objects |
||
| 575 | * are in the queue with a given status. |
||
| 576 | * |
||
| 577 | * Example usage: |
||
| 578 | * After a caller calls the "EE_Message_Queue::execute()" method, the caller can check if there were any failed sends |
||
| 579 | * by calling $queue->count_STS_in_queue( EEM_Message_Queue::status_failed ). |
||
| 580 | * |
||
| 581 | * @param array $status Stati to check for in queue |
||
| 582 | * @return int Count of EE_Message's matching the given status. |
||
| 583 | */ |
||
| 584 | public function count_STS_in_queue( $status ) { |
||
| 595 | |||
| 596 | |||
| 597 | /** |
||
| 598 | * Executes the get_preview method on the provided messenger. |
||
| 599 | * |
||
| 600 | *@param EE_Message $message |
||
| 601 | * @param EE_messenger $messenger |
||
| 602 | * @param EE_message_type $message_type |
||
| 603 | * @param $test_send |
||
| 604 | * @return bool true means all went well, false means, not so much. |
||
| 605 | */ |
||
| 606 | protected function _do_preview( EE_Message $message, EE_messenger $messenger, EE_message_type $message_type, $test_send ) { |
||
| 618 | |||
| 619 | |||
| 620 | |||
| 621 | |||
| 622 | /** |
||
| 623 | * Executes the send method on the provided messenger |
||
| 624 | * |
||
| 625 | * EE_Messengers are expected to: |
||
| 626 | * - return true if the send was successful. |
||
| 627 | * - return false if the send was unsuccessful but can be tried again. |
||
| 628 | * - throw an Exception if the send was unsuccessful and cannot be tried again. |
||
| 629 | * |
||
| 630 | * @param EE_Message $message |
||
| 631 | * @param EE_messenger $messenger |
||
| 632 | * @param EE_message_type $message_type |
||
| 633 | * |
||
| 634 | * @return bool true means all went well, false means, not so much. |
||
| 635 | */ |
||
| 636 | protected function _do_send( EE_Message $message, EE_messenger $messenger, EE_message_type $message_type ) { |
||
| 637 | try { |
||
| 638 | if ( $messenger->send_message( $message, $message_type ) ) { |
||
| 639 | $message->set_STS_ID( EEM_Message::status_sent ); |
||
| 640 | return true; |
||
| 641 | } else { |
||
| 642 | $message->set_STS_ID( EEM_Message::status_retry ); |
||
| 643 | return false; |
||
| 644 | } |
||
| 645 | } catch( SendMessageException $e ) { |
||
| 646 | $message->set_STS_ID( EEM_Message::status_failed ); |
||
| 647 | $message->set_error_message( $e->getMessage() ); |
||
| 648 | return false; |
||
| 649 | } |
||
| 650 | } |
||
| 651 | |||
| 652 | |||
| 653 | |||
| 654 | |||
| 655 | |||
| 656 | /** |
||
| 657 | * This sets any necessary error messages on the message object and its status to failed. |
||
| 658 | * @param EE_Message $message |
||
| 659 | * @param array $error_messages the response from the messenger. |
||
| 660 | */ |
||
| 661 | protected function _set_error_message( EE_Message $message, $error_messages ) { |
||
| 678 | |||
| 679 | } //end EE_Messages_Queue class |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.