Complex classes like Jetpack_Sync_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 Jetpack_Sync_Queue, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 36 | class Jetpack_Sync_Queue { |
||
|
|
|||
| 37 | public $id; |
||
| 38 | private $row_iterator; |
||
| 39 | private $use_named_lock; |
||
| 40 | |||
| 41 | function __construct( $id ) { |
||
| 42 | $this->id = str_replace( '-', '_', $id ); // necessary to ensure we don't have ID collisions in the SQL |
||
| 43 | $this->row_iterator = 0; |
||
| 44 | $this->use_named_lock = (bool) Jetpack_Sync_Settings::get_setting( 'use_mysql_named_lock' ); |
||
| 45 | } |
||
| 46 | |||
| 47 | function add( $item ) { |
||
| 48 | global $wpdb; |
||
| 49 | $added = false; |
||
| 50 | // this basically tries to add the option until enough time has elapsed that |
||
| 51 | // it has a unique (microtime-based) option key |
||
| 52 | while ( ! $added ) { |
||
| 53 | $rows_added = $wpdb->query( $wpdb->prepare( |
||
| 54 | "INSERT INTO $wpdb->options (option_name, option_value, autoload) VALUES (%s, %s,%s)", |
||
| 55 | $this->get_next_data_row_option_name(), |
||
| 56 | serialize( $item ), |
||
| 57 | 'no' |
||
| 58 | ) ); |
||
| 59 | $added = ( 0 !== $rows_added ); |
||
| 60 | } |
||
| 61 | |||
| 62 | do_action( 'jpsq_item_added' ); |
||
| 63 | } |
||
| 64 | |||
| 65 | // Attempts to insert all the items in a single SQL query. May be subject to query size limits! |
||
| 66 | function add_all( $items ) { |
||
| 67 | global $wpdb; |
||
| 68 | $base_option_name = $this->get_next_data_row_option_name(); |
||
| 69 | |||
| 70 | $query = "INSERT INTO $wpdb->options (option_name, option_value, autoload) VALUES "; |
||
| 71 | |||
| 72 | $rows = array(); |
||
| 73 | |||
| 74 | for ( $i = 0; $i < count( $items ); $i += 1 ) { |
||
| 75 | $option_name = esc_sql( $base_option_name . '-' . $i ); |
||
| 76 | $option_value = esc_sql( serialize( $items[ $i ] ) ); |
||
| 77 | $rows[] = "('$option_name', '$option_value', 'no')"; |
||
| 78 | } |
||
| 79 | |||
| 80 | $rows_added = $wpdb->query( $query . join( ',', $rows ) ); |
||
| 81 | |||
| 82 | if ( count( $items ) === $rows_added ) { |
||
| 83 | return new WP_Error( 'row_count_mismatch', "The number of rows inserted didn't match the size of the input array" ); |
||
| 84 | } |
||
| 85 | |||
| 86 | do_action( 'jpsq_items_added', $rows_added ); |
||
| 87 | } |
||
| 88 | |||
| 89 | // Peek at the front-most item on the queue without checking it out |
||
| 90 | function peek( $count = 1 ) { |
||
| 91 | $items = $this->fetch_items( $count ); |
||
| 92 | if ( $items ) { |
||
| 93 | return Jetpack_Sync_Utils::get_item_values( $items ); |
||
| 94 | } |
||
| 95 | |||
| 96 | return array(); |
||
| 97 | } |
||
| 98 | |||
| 99 | // lag is the difference in time between the age of the oldest item |
||
| 100 | // (aka first or frontmost item) and the current time |
||
| 101 | function lag() { |
||
| 102 | global $wpdb; |
||
| 103 | |||
| 104 | $first_item_name = $wpdb->get_var( $wpdb->prepare( |
||
| 105 | "SELECT option_name FROM $wpdb->options WHERE option_name LIKE %s ORDER BY option_name ASC LIMIT 1", |
||
| 106 | "jpsq_{$this->id}-%" |
||
| 107 | ) ); |
||
| 108 | |||
| 109 | if ( ! $first_item_name ) { |
||
| 110 | return 0; |
||
| 111 | } |
||
| 112 | |||
| 113 | // break apart the item name to get the timestamp |
||
| 114 | $matches = null; |
||
| 115 | if ( preg_match( '/^jpsq_' . $this->id . '-(\d+\.\d+)-/', $first_item_name, $matches ) ) { |
||
| 116 | return microtime( true ) - floatval( $matches[1] ); |
||
| 117 | } else { |
||
| 118 | return 0; |
||
| 119 | } |
||
| 120 | } |
||
| 121 | |||
| 122 | function reset() { |
||
| 129 | |||
| 130 | function size() { |
||
| 137 | |||
| 138 | // we use this peculiar implementation because it's much faster than count(*) |
||
| 139 | function has_any_items() { |
||
| 147 | |||
| 148 | function checkout( $buffer_size ) { |
||
| 149 | $buffer_id = uniqid(); |
||
| 150 | |||
| 151 | $lock_result = $this->acquire_lock( $buffer_id ); |
||
| 152 | |||
| 167 | |||
| 168 | // this checks out rows until it either empties the queue or hits a certain memory limit |
||
| 169 | // it loads the sizes from the DB first so that it doesn't accidentally |
||
| 170 | // load more data into memory than it needs to. |
||
| 171 | // The only way it will load more items than $max_size is if a single queue item |
||
| 172 | // exceeds the memory limit, but in that case it will send that item by itself. |
||
| 173 | function checkout_with_memory_limit( $max_memory, $max_buffer_size = 500 ) { |
||
| 220 | |||
| 221 | function checkin( $buffer ) { |
||
| 230 | |||
| 231 | function close( $buffer, $ids_to_remove = null ) { |
||
| 253 | |||
| 254 | function flush_all() { |
||
| 260 | |||
| 261 | function get_all() { |
||
| 264 | |||
| 265 | // use with caution, this could allow multiple processes to delete |
||
| 266 | // and send from the queue at the same time |
||
| 267 | function force_checkin() { |
||
| 270 | |||
| 271 | // used to lock checkouts from the queue. |
||
| 272 | // tries to wait up to $timeout seconds for the queue to be empty |
||
| 273 | function lock( $timeout = 30 ) { |
||
| 293 | |||
| 294 | function unlock() { |
||
| 297 | |||
| 298 | private function acquire_lock( $buffer_id ) { |
||
| 317 | |||
| 318 | private function get_checkout_id() { |
||
| 321 | |||
| 322 | private function set_checkout_id( $checkout_id ) { |
||
| 325 | |||
| 326 | private function delete_checkout_id() { |
||
| 329 | |||
| 330 | private function get_checkout_transient_name() { |
||
| 333 | |||
| 334 | private function get_next_data_row_option_name() { |
||
| 351 | |||
| 352 | private function fetch_items( $limit = null ) { |
||
| 368 | |||
| 369 | private function fetch_items_by_id( $item_ids ) { |
||
| 385 | |||
| 386 | private function release_lock( $buffer ) { |
||
| 409 | } |
||
| 410 | |||
| 429 |
Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.