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 if ( ! defined('EVENT_ESPRESSO_VERSION')) { exit('No direct script access allowed'); } |
||
12 | class EE_Messages_Queue { |
||
13 | |||
14 | |||
15 | /** |
||
16 | * @type string reference for sending action |
||
17 | */ |
||
18 | const action_sending = 'sending'; |
||
19 | |||
20 | /** |
||
21 | * @type string reference for generation action |
||
22 | */ |
||
23 | const action_generating = 'generation'; |
||
24 | |||
25 | |||
26 | |||
27 | /** |
||
28 | * @type EE_Message_Repository $_queue |
||
29 | */ |
||
30 | protected $_queue; |
||
31 | |||
32 | /** |
||
33 | * Sets the limit of how many messages are generated per process. |
||
34 | * @type int |
||
35 | */ |
||
36 | protected $_batch_count; |
||
37 | |||
38 | /** |
||
39 | * Sets the limit of how many messages can be sent per hour. |
||
40 | * @type int |
||
41 | */ |
||
42 | protected $_rate_limit; |
||
43 | |||
44 | /** |
||
45 | * This is an array of cached queue items being stored in this object. |
||
46 | * The array keys will be the ID of the EE_Message in the db if saved. If the EE_Message |
||
47 | * is not saved to the db then its key will be an increment of "UNS" (i.e. UNS1, UNS2 etc.) |
||
48 | * @type EE_Message[] |
||
49 | */ |
||
50 | protected $_cached_queue_items; |
||
51 | |||
52 | /** |
||
53 | * Tracks the number of unsaved queue items. |
||
54 | * @type int |
||
55 | */ |
||
56 | protected $_unsaved_count = 0; |
||
57 | |||
58 | /** |
||
59 | * used to record if a do_messenger_hooks has already been called for a message type. This prevents multiple |
||
60 | * hooks getting fired if users have setup their action/filter hooks to prevent duplicate calls. |
||
61 | * |
||
62 | * @type array |
||
63 | */ |
||
64 | protected $_did_hook = array(); |
||
65 | |||
66 | |||
67 | |||
68 | /** |
||
69 | * Constructor. |
||
70 | * Setup all the initial properties and load a EE_Message_Repository. |
||
71 | * |
||
72 | * @param \EE_Message_Repository $message_repository |
||
73 | */ |
||
74 | public function __construct( EE_Message_Repository $message_repository ) { |
||
79 | |||
80 | |||
81 | |||
82 | /** |
||
83 | * Add a EE_Message object to the queue |
||
84 | * |
||
85 | * @param EE_Message $message |
||
86 | * @param array $data This will be an array of data to attach to the object in the repository. If the |
||
87 | * object is persisted, this data will be saved on an extra_meta object related to |
||
88 | * EE_Message. |
||
89 | * @param bool $preview Whether this EE_Message represents a preview or not. |
||
90 | * @param bool $test_send This indicates whether to do a test send instead of actual send. A test send will |
||
91 | * use the messenger send method but typically is based on preview data. |
||
92 | * @return bool Whether the message was successfully added to the repository or not. |
||
93 | */ |
||
94 | public function add( EE_Message $message, $data = array(), $preview = false, $test_send = false ) { |
||
99 | |||
100 | |||
101 | |||
102 | |||
103 | /** |
||
104 | * Removes EE_Message from _queue that matches the given EE_Message if the pointer is on a matching EE_Message |
||
105 | * @param EE_Message $message The message to detach from the queue |
||
106 | * @param bool $persist This flag indicates whether to attempt to delete the object from the db as well. |
||
107 | * @return bool |
||
108 | */ |
||
109 | public function remove( EE_Message $message, $persist = false ) { |
||
126 | |||
127 | |||
128 | |||
129 | |||
130 | /** |
||
131 | * Persists all queued EE_Message objects to the db. |
||
132 | * @return array() @see EE_Messages_Repository::saveAll() for return values. |
||
|
|||
133 | */ |
||
134 | public function save() { |
||
137 | |||
138 | |||
139 | |||
140 | |||
141 | |||
142 | /** |
||
143 | * @return EE_Message_Repository |
||
144 | */ |
||
145 | public function get_queue() { |
||
148 | |||
149 | |||
150 | |||
151 | |||
152 | /** |
||
153 | * This does the following things: |
||
154 | * 1. Checks if there is a lock on generation (prevents race conditions). If there is a lock then exits (return false). |
||
155 | * 2. If no lock, sets lock, then retrieves a batch of non-generated EE_Message objects and adds to queue |
||
156 | * 3. Returns bool. True = batch ready. False = no batch ready (or nothing available for generation). |
||
157 | * |
||
158 | * Note: Callers should make sure they release the lock otherwise batch generation will be prevented from continuing. |
||
159 | * The lock is on a transient that is set to expire after one hour as a fallback in case locks are not removed. |
||
160 | * |
||
161 | * @return bool true if successfully retrieved batch, false no batch ready. |
||
162 | */ |
||
163 | public function get_batch_to_generate() { |
||
191 | |||
192 | |||
193 | /** |
||
194 | * This does the following things: |
||
195 | * 1. Checks if there is a lock on sending (prevents race conditions). If there is a lock then exits (return false). |
||
196 | * 2. Grabs the allowed number of messages to send for the rate_limit. If cannot send any more messages, then return false. |
||
197 | * 2. If no lock, sets lock, then retrieves a batch of EE_Message objects, adds to queue and triggers execution. |
||
198 | * 3. On success or unsuccessful send, sets status appropriately. |
||
199 | * 4. Saves messages via the queue |
||
200 | * 5. Releases lock. |
||
201 | * |
||
202 | * @return bool true on success, false if something preventing sending (i.e. lock set). Note: true does not necessarily |
||
203 | * mean that all messages were successfully sent. It just means that this method successfully completed. |
||
204 | * On true, client may want to call $this->count_STS_in_queue( EEM_Message::status_failed ) to see if |
||
205 | * any failed EE_Message objects. Each failed message object will also have a saved error message on it |
||
206 | * to assist with notifying user. |
||
207 | */ |
||
208 | public function get_to_send_batch_and_send() { |
||
247 | |||
248 | |||
249 | |||
250 | |||
251 | /** |
||
252 | * Locks the queue so that no other queues can call the "batch" methods. |
||
253 | * |
||
254 | * @param string $type The type of queue being locked. |
||
255 | */ |
||
256 | public function lock_queue( $type = EE_Messages_Queue::action_generating ) { |
||
259 | |||
260 | |||
261 | |||
262 | |||
263 | /** |
||
264 | * Unlocks the queue so that batch methods can be used. |
||
265 | * |
||
266 | * @param string $type The type of queue being unlocked. |
||
267 | */ |
||
268 | public function unlock_queue( $type = EE_Messages_Queue::action_generating ) { |
||
271 | |||
272 | |||
273 | |||
274 | |||
275 | /** |
||
276 | * Retrieve the key used for the lock transient. |
||
277 | * @param string $type The type of lock. |
||
278 | * @return string |
||
279 | */ |
||
280 | protected function _get_lock_key( $type = EE_Messages_Queue::action_generating ) { |
||
283 | |||
284 | |||
285 | |||
286 | |||
287 | /** |
||
288 | * Retrieve the expiry time for the lock transient. |
||
289 | * @param string $type The type of lock |
||
290 | * @return int time to expiry in seconds. |
||
291 | */ |
||
292 | protected function _get_lock_expiry( $type = EE_Messages_Queue::action_generating ) { |
||
295 | |||
296 | |||
297 | /** |
||
298 | * Returns the key used for rate limit transient. |
||
299 | * @return string |
||
300 | */ |
||
301 | protected function _get_rate_limit_key() { |
||
304 | |||
305 | |||
306 | /** |
||
307 | * Returns the rate limit expiry time. |
||
308 | * @return int |
||
309 | */ |
||
310 | protected function _get_rate_limit_expiry() { |
||
313 | |||
314 | |||
315 | |||
316 | |||
317 | /** |
||
318 | * Returns the default rate limit for sending messages. |
||
319 | * @return int |
||
320 | */ |
||
321 | protected function _default_rate_limit() { |
||
324 | |||
325 | |||
326 | |||
327 | |||
328 | /** |
||
329 | * Return the orderby array for priority. |
||
330 | * @return array |
||
331 | */ |
||
332 | protected function _get_priority_orderby() { |
||
338 | |||
339 | |||
340 | |||
341 | |||
342 | /** |
||
343 | * Returns whether batch methods are "locked" or not. |
||
344 | * |
||
345 | * @param string $type The type of lock being checked for. |
||
346 | * @return bool |
||
347 | */ |
||
348 | public function is_locked( $type = EE_Messages_Queue::action_generating ) { |
||
351 | |||
352 | |||
353 | |||
354 | |||
355 | |||
356 | |||
357 | |||
358 | /** |
||
359 | * Retrieves the rate limit that may be cached as a transient. |
||
360 | * If the rate limit is not set, then this sets the default rate limit and expiry and returns it. |
||
361 | * @return int |
||
362 | */ |
||
363 | public function get_rate_limit() { |
||
370 | |||
371 | |||
372 | |||
373 | |||
374 | /** |
||
375 | * This updates existing rate limit with the new limit which is the old minus the batch. |
||
376 | * @param int $batch_completed This sets the new rate limit based on the given batch that was completed. |
||
377 | */ |
||
378 | public function set_rate_limit( $batch_completed ) { |
||
385 | |||
386 | |||
387 | /** |
||
388 | * This method checks the queue for ANY EE_Message objects with a priority matching the given priority passed in. |
||
389 | * If that exists, then we immediately initiate a non-blocking request to do the requested action type. |
||
390 | * |
||
391 | * Note: Keep in mind that there is the possibility that the request will not execute if there is already another request |
||
392 | * running on a queue for the given task. |
||
393 | * @param string $task This indicates what type of request is going to be initiated. |
||
394 | * @param int $priority This indicates the priority that triggers initiating the request. |
||
395 | */ |
||
396 | public function initiate_request_by_priority( $task = 'generate', $priority = EEM_Message::priority_high ) { |
||
408 | |||
409 | |||
410 | |||
411 | /** |
||
412 | * Loops through the EE_Message objects in the _queue and calls the messenger send methods for each message. |
||
413 | * |
||
414 | * @param bool $save Used to indicate whether to save the message queue after sending |
||
415 | * (default will save). |
||
416 | * @param mixed $sending_messenger (optional) When the sending messenger is different than |
||
417 | * what is on the EE_Message object in the queue. |
||
418 | * For instance, showing the browser view of an email message, |
||
419 | * or giving a pdf generated view of an html document. |
||
420 | * This should be an instance of EE_Messenger |
||
421 | * @param bool|int $by_priority When set, this indicates that only messages |
||
422 | * matching the given priority should be executed. |
||
423 | * |
||
424 | * @return int Number of messages sent. Note, 0 does not mean that no messages were processed. |
||
425 | * Also, if the messenger is an request type messenger (or a preview), |
||
426 | * its entirely possible that the messenger will exit before |
||
427 | */ |
||
428 | public function execute( $save = true, $sending_messenger = null, $by_priority = false ) { |
||
476 | |||
477 | |||
478 | |||
479 | /** |
||
480 | * _process_message |
||
481 | * |
||
482 | * @param EE_Message $message |
||
483 | * @param mixed $sending_messenger (optional) |
||
484 | * @return bool |
||
485 | */ |
||
486 | protected function _process_message( EE_Message $message, $sending_messenger = null ) { |
||
513 | |||
514 | |||
515 | |||
516 | /** |
||
517 | * The intention of this method is to count how many EE_Message objects |
||
518 | * are in the queue with a given status. |
||
519 | * |
||
520 | * Example usage: |
||
521 | * After a caller calls the "EE_Message_Queue::execute()" method, the caller can check if there were any failed sends |
||
522 | * by calling $queue->count_STS_in_queue( EEM_Message_Queue::status_failed ). |
||
523 | * |
||
524 | * @param array $status Stati to check for in queue |
||
525 | * @return int Count of EE_Message's matching the given status. |
||
526 | */ |
||
527 | public function count_STS_in_queue( $status ) { |
||
537 | |||
538 | |||
539 | /** |
||
540 | * Executes the get_preview method on the provided messenger. |
||
541 | * |
||
542 | *@param EE_Message $message |
||
543 | * @param EE_Messenger $messenger |
||
544 | * @param EE_message_type $message_type |
||
545 | * @param $test_send |
||
546 | * @return bool true means all went well, false means, not so much. |
||
547 | */ |
||
548 | protected function _do_preview( EE_Message $message, EE_Messenger $messenger, EE_message_type $message_type, $test_send ) { |
||
560 | |||
561 | |||
562 | |||
563 | |||
564 | /** |
||
565 | * Executes the send method on the provided messenger |
||
566 | * |
||
567 | *@param EE_Message $message |
||
568 | * @param EE_Messenger $messenger |
||
569 | * @param EE_message_type $message_type |
||
570 | * @return bool true means all went well, false means, not so much. |
||
571 | */ |
||
572 | protected function _do_send( EE_Message $message, EE_Messenger $messenger, EE_message_type $message_type ) { |
||
581 | |||
582 | |||
583 | |||
584 | |||
585 | |||
586 | /** |
||
587 | * This sets any necessary error messages on the message object and its status to failed. |
||
588 | * @param EE_Message $message |
||
589 | * @param array $error_messages the response from the messenger. |
||
590 | */ |
||
591 | protected function _set_error_message( EE_Message $message, $error_messages ) { |
||
608 | |||
609 | } //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.