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 |
||
| 34 | class Jetpack_Sync_Queue { |
||
|
|
|||
| 35 | public $id; |
||
| 36 | private $row_iterator; |
||
| 37 | |||
| 38 | function __construct( $id ) { |
||
| 39 | $this->id = str_replace( '-', '_', $id ); // necessary to ensure we don't have ID collisions in the SQL |
||
| 40 | $this->row_iterator = 0; |
||
| 41 | } |
||
| 42 | |||
| 43 | function add( $item ) { |
||
| 44 | global $wpdb; |
||
| 45 | $added = false; |
||
| 46 | // this basically tries to add the option until enough time has elapsed that |
||
| 47 | // it has a unique (microtime-based) option key |
||
| 48 | while ( ! $added ) { |
||
| 49 | $rows_added = $wpdb->query( $wpdb->prepare( |
||
| 50 | "INSERT INTO $wpdb->options (option_name, option_value,autoload) VALUES (%s, %s,%s)", |
||
| 51 | $this->get_next_data_row_option_name(), |
||
| 52 | serialize( $item ), |
||
| 53 | 'no' |
||
| 54 | ) ); |
||
| 55 | $added = ( $rows_added !== 0 ); |
||
| 56 | } |
||
| 57 | } |
||
| 58 | |||
| 59 | // Attempts to insert all the items in a single SQL query. May be subject to query size limits! |
||
| 60 | function add_all( $items ) { |
||
| 61 | global $wpdb; |
||
| 62 | $base_option_name = $this->get_next_data_row_option_name(); |
||
| 63 | |||
| 64 | $query = "INSERT INTO $wpdb->options (option_name, option_value,autoload) VALUES "; |
||
| 65 | |||
| 66 | $rows = array(); |
||
| 67 | |||
| 68 | for ( $i = 0; $i < count( $items ); $i += 1 ) { |
||
| 69 | $option_name = esc_sql( $base_option_name . '-' . $i ); |
||
| 70 | $option_value = esc_sql( serialize( $items[ $i ] ) ); |
||
| 71 | $rows[] = "('$option_name', '$option_value', 'no')"; |
||
| 72 | } |
||
| 73 | |||
| 74 | $rows_added = $wpdb->query( $query . join( ',', $rows ) ); |
||
| 75 | |||
| 76 | if ( $rows_added !== count( $items ) ) { |
||
| 77 | return new WP_Error( 'row_count_mismatch', "The number of rows inserted didn't match the size of the input array" ); |
||
| 78 | } |
||
| 79 | } |
||
| 80 | |||
| 81 | // Peek at the front-most item on the queue without checking it out |
||
| 82 | function peek( $count = 1 ) { |
||
| 83 | $items = $this->fetch_items( $count ); |
||
| 84 | if ( $items ) { |
||
| 85 | return Jetpack_Sync_Utils::get_item_values( $items ); |
||
| 86 | } |
||
| 87 | |||
| 88 | return array(); |
||
| 89 | } |
||
| 90 | |||
| 91 | // lag is the difference in time between the age of the oldest item and the current time |
||
| 92 | function lag() { |
||
| 93 | global $wpdb; |
||
| 94 | |||
| 95 | $last_item_name = $wpdb->get_var( $wpdb->prepare( |
||
| 96 | "SELECT option_name FROM $wpdb->options WHERE option_name LIKE %s ORDER BY option_name ASC LIMIT 1", |
||
| 97 | "jpsq_{$this->id}-%" |
||
| 98 | ) ); |
||
| 99 | |||
| 100 | if ( ! $last_item_name ) { |
||
| 101 | return null; |
||
| 102 | } |
||
| 103 | |||
| 104 | // break apart the item name to get the timestamp |
||
| 105 | $matches = null; |
||
| 106 | if ( preg_match( '/^jpsq_' . $this->id . '-(\d+\.\d+)-/', $last_item_name, $matches ) ) { |
||
| 107 | return microtime( true ) - floatval( $matches[1] ); |
||
| 108 | } else { |
||
| 109 | return null; |
||
| 110 | } |
||
| 111 | } |
||
| 112 | |||
| 113 | function reset() { |
||
| 114 | global $wpdb; |
||
| 115 | $this->delete_checkout_id(); |
||
| 116 | $wpdb->query( $wpdb->prepare( |
||
| 117 | "DELETE FROM $wpdb->options WHERE option_name LIKE %s", "jpsq_{$this->id}-%" |
||
| 118 | ) ); |
||
| 119 | } |
||
| 120 | |||
| 121 | function size() { |
||
| 122 | global $wpdb; |
||
| 123 | |||
| 124 | return (int) $wpdb->get_var( $wpdb->prepare( |
||
| 125 | "SELECT count(*) FROM $wpdb->options WHERE option_name LIKE %s", "jpsq_{$this->id}-%" |
||
| 126 | ) ); |
||
| 127 | } |
||
| 128 | |||
| 129 | // we use this peculiar implementation because it's much faster than count(*) |
||
| 130 | function has_any_items() { |
||
| 131 | global $wpdb; |
||
| 132 | $value = $wpdb->get_var( $wpdb->prepare( |
||
| 133 | "SELECT exists( SELECT option_name FROM $wpdb->options WHERE option_name LIKE %s )", "jpsq_{$this->id}-%" |
||
| 134 | ) ); |
||
| 135 | |||
| 136 | return ( $value === "1" ); |
||
| 137 | } |
||
| 138 | |||
| 139 | function checkout( $buffer_size ) { |
||
| 162 | |||
| 163 | // this checks out rows until it either empties the queue or hits a certain memory limit |
||
| 164 | // it loads the sizes from the DB first so that it doesn't accidentally |
||
| 165 | // load more data into memory than it needs to. |
||
| 166 | // The only way it will load more items than $max_size is if a single queue item |
||
| 167 | // exceeds the memory limit, but in that case it will send that item by itself. |
||
| 168 | function checkout_with_memory_limit( $max_memory, $max_buffer_size = 500 ) { |
||
| 219 | |||
| 220 | function checkin( $buffer ) { |
||
| 231 | |||
| 232 | function close( $buffer, $ids_to_remove = null ) { |
||
| 256 | |||
| 257 | function flush_all() { |
||
| 258 | $items = Jetpack_Sync_Utils::get_item_values( $this->fetch_items() ); |
||
| 259 | $this->reset(); |
||
| 260 | |||
| 261 | return $items; |
||
| 262 | } |
||
| 263 | |||
| 264 | function get_all() { |
||
| 265 | return $this->fetch_items(); |
||
| 266 | } |
||
| 267 | |||
| 268 | // use with caution, this could allow multiple processes to delete |
||
| 269 | // and send from the queue at the same time |
||
| 270 | function force_checkin() { |
||
| 271 | $this->delete_checkout_id(); |
||
| 272 | } |
||
| 273 | |||
| 274 | // used to lock checkouts from the queue. |
||
| 275 | // tries to wait up to $timeout seconds for the queue to be empty |
||
| 276 | function lock( $timeout = 30 ) { |
||
| 277 | $tries = 0; |
||
| 278 | |||
| 279 | while ( $this->has_any_items() && $tries < $timeout ) { |
||
| 280 | sleep( 1 ); |
||
| 281 | $tries += 1; |
||
| 282 | } |
||
| 283 | |||
| 284 | if ( $tries === 30 ) { |
||
| 285 | return new WP_Error( 'lock_timeout', 'Timeout waiting for sync queue to empty' ); |
||
| 286 | } |
||
| 287 | |||
| 288 | if ( $this->get_checkout_id() ) { |
||
| 289 | return new WP_Error( 'unclosed_buffer', 'There is an unclosed buffer' ); |
||
| 290 | } |
||
| 291 | |||
| 292 | // hopefully this means we can acquire a checkout? |
||
| 293 | $result = $this->set_checkout_id( 'lock' ); |
||
| 294 | |||
| 295 | if ( ! $result || is_wp_error( $result ) ) { |
||
| 296 | return $result; |
||
| 297 | } |
||
| 298 | |||
| 299 | return true; |
||
| 300 | } |
||
| 301 | |||
| 302 | function unlock() { |
||
| 305 | |||
| 306 | private function get_checkout_id() { |
||
| 307 | return get_transient( $this->get_checkout_transient_name() ); |
||
| 308 | } |
||
| 309 | |||
| 310 | private function set_checkout_id( $checkout_id ) { |
||
| 311 | return set_transient( $this->get_checkout_transient_name(), $checkout_id, 5 * 60 ); // 5 minute timeout |
||
| 312 | } |
||
| 313 | |||
| 314 | private function delete_checkout_id() { |
||
| 315 | delete_transient( $this->get_checkout_transient_name() ); |
||
| 316 | } |
||
| 317 | |||
| 318 | private function get_checkout_transient_name() { |
||
| 319 | return "jpsq_{$this->id}_checkout"; |
||
| 320 | } |
||
| 321 | |||
| 322 | private function get_next_data_row_option_name() { |
||
| 339 | |||
| 340 | private function fetch_items( $limit = null ) { |
||
| 356 | |||
| 357 | private function fetch_items_by_id( $item_ids ) { |
||
| 373 | |||
| 374 | private function validate_checkout( $buffer ) { |
||
| 391 | } |
||
| 392 | |||
| 411 |
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.