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 | * tons of added_option callbacks. |
||
| 35 | */ |
||
| 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() { |
||
| 123 | global $wpdb; |
||
| 124 | $this->delete_checkout_id(); |
||
| 125 | $wpdb->query( $wpdb->prepare( |
||
| 126 | "DELETE FROM $wpdb->options WHERE option_name LIKE %s", "jpsq_{$this->id}-%" |
||
| 127 | ) ); |
||
| 128 | } |
||
| 129 | |||
| 130 | function size() { |
||
| 131 | global $wpdb; |
||
| 132 | |||
| 133 | return (int) $wpdb->get_var( $wpdb->prepare( |
||
| 134 | "SELECT count(*) FROM $wpdb->options WHERE option_name LIKE %s", "jpsq_{$this->id}-%" |
||
| 135 | ) ); |
||
| 136 | } |
||
| 137 | |||
| 138 | // we use this peculiar implementation because it's much faster than count(*) |
||
| 139 | function has_any_items() { |
||
| 140 | global $wpdb; |
||
| 141 | $value = $wpdb->get_var( $wpdb->prepare( |
||
| 142 | "SELECT exists( SELECT option_name FROM $wpdb->options WHERE option_name LIKE %s )", "jpsq_{$this->id}-%" |
||
| 143 | ) ); |
||
| 144 | |||
| 145 | return ( $value === '1' ); |
||
| 146 | } |
||
| 147 | |||
| 148 | function checkout( $buffer_size ) { |
||
| 149 | if ( ! $this->acquire_lock() ) { |
||
| 150 | return new WP_Error( 'unclosed_buffer', 'There is an unclosed buffer' ); |
||
| 151 | } |
||
| 152 | |||
| 153 | $buffer_id = uniqid(); |
||
| 154 | |||
| 155 | $result = $this->set_checkout_id( $buffer_id ); |
||
| 156 | |||
| 157 | if ( ! $result || is_wp_error( $result ) ) { |
||
| 158 | return $result; |
||
| 159 | } |
||
| 160 | |||
| 161 | $items = $this->fetch_items( $buffer_size ); |
||
| 162 | |||
| 163 | if ( count( $items ) === 0 ) { |
||
| 164 | return false; |
||
| 165 | } |
||
| 166 | |||
| 167 | $buffer = new Jetpack_Sync_Queue_Buffer( $buffer_id, array_slice( $items, 0, $buffer_size ) ); |
||
| 168 | |||
| 169 | return $buffer; |
||
| 170 | } |
||
| 171 | |||
| 172 | // this checks out rows until it either empties the queue or hits a certain memory limit |
||
| 173 | // it loads the sizes from the DB first so that it doesn't accidentally |
||
| 174 | // load more data into memory than it needs to. |
||
| 175 | // The only way it will load more items than $max_size is if a single queue item |
||
| 176 | // exceeds the memory limit, but in that case it will send that item by itself. |
||
| 177 | function checkout_with_memory_limit( $max_memory, $max_buffer_size = 500 ) { |
||
| 178 | if ( $this->get_checkout_id() ) { |
||
| 179 | return new WP_Error( 'unclosed_buffer', 'There is an unclosed buffer' ); |
||
| 180 | } |
||
| 181 | |||
| 182 | $buffer_id = uniqid(); |
||
| 183 | |||
| 184 | $result = $this->set_checkout_id( $buffer_id ); |
||
| 185 | |||
| 186 | if ( ! $result || is_wp_error( $result ) ) { |
||
| 187 | return $result; |
||
| 188 | } |
||
| 189 | |||
| 190 | // get the map of buffer_id -> memory_size |
||
| 191 | global $wpdb; |
||
| 192 | |||
| 193 | $items_with_size = $wpdb->get_results( |
||
| 194 | $wpdb->prepare( |
||
| 195 | "SELECT option_name AS id, LENGTH(option_value) AS value_size FROM $wpdb->options WHERE option_name LIKE %s ORDER BY option_name ASC LIMIT %d", |
||
| 196 | "jpsq_{$this->id}-%", |
||
| 197 | $max_buffer_size |
||
| 198 | ), |
||
| 199 | OBJECT |
||
| 200 | ); |
||
| 201 | |||
| 202 | $total_memory = 0; |
||
| 203 | $item_ids = array(); |
||
| 204 | |||
| 205 | foreach ( $items_with_size as $item_with_size ) { |
||
| 206 | $total_memory += $item_with_size->value_size; |
||
| 207 | |||
| 208 | // if this is the first item and it exceeds memory, allow loop to continue |
||
| 209 | // we will exit on the next iteration instead |
||
| 210 | if ( $total_memory > $max_memory && count( $item_ids ) > 0 ) { |
||
| 211 | break; |
||
| 212 | } |
||
| 213 | $item_ids[] = $item_with_size->id; |
||
| 214 | } |
||
| 215 | |||
| 216 | $items = $this->fetch_items_by_id( $item_ids ); |
||
| 217 | |||
| 218 | if ( count( $items ) === 0 ) { |
||
| 219 | $this->delete_checkout_id(); |
||
| 220 | |||
| 221 | return false; |
||
| 222 | } |
||
| 223 | |||
| 224 | $buffer = new Jetpack_Sync_Queue_Buffer( $buffer_id, $items ); |
||
| 225 | |||
| 226 | return $buffer; |
||
| 227 | } |
||
| 228 | |||
| 229 | function checkin( $buffer ) { |
||
| 230 | $is_valid = $this->validate_checkout( $buffer ); |
||
| 231 | |||
| 232 | if ( is_wp_error( $is_valid ) ) { |
||
| 233 | return $is_valid; |
||
| 234 | } |
||
| 235 | |||
| 236 | $this->delete_checkout_id(); |
||
| 237 | |||
| 238 | return true; |
||
| 239 | } |
||
| 240 | |||
| 241 | function close( $buffer, $ids_to_remove = null ) { |
||
| 242 | $is_valid = $this->validate_checkout( $buffer ); |
||
| 243 | |||
| 244 | if ( is_wp_error( $is_valid ) ) { |
||
| 245 | return $is_valid; |
||
| 246 | } |
||
| 247 | |||
| 248 | $this->delete_checkout_id(); |
||
| 249 | |||
| 250 | // by default clear all items in the buffer |
||
| 251 | if ( is_null( $ids_to_remove ) ) { |
||
| 252 | $ids_to_remove = $buffer->get_item_ids(); |
||
| 253 | } |
||
| 254 | |||
| 255 | global $wpdb; |
||
| 256 | |||
| 257 | if ( count( $ids_to_remove ) > 0 ) { |
||
| 258 | $sql = "DELETE FROM $wpdb->options WHERE option_name IN (" . implode( ', ', array_fill( 0, count( $ids_to_remove ), '%s' ) ) . ')'; |
||
| 259 | $query = call_user_func_array( array( $wpdb, 'prepare' ), array_merge( array( $sql ), $ids_to_remove ) ); |
||
| 260 | $wpdb->query( $query ); |
||
| 261 | } |
||
| 262 | |||
| 263 | return true; |
||
| 264 | } |
||
| 265 | |||
| 266 | function flush_all() { |
||
| 267 | $items = Jetpack_Sync_Utils::get_item_values( $this->fetch_items() ); |
||
| 268 | $this->reset(); |
||
| 269 | |||
| 270 | return $items; |
||
| 271 | } |
||
| 272 | |||
| 273 | function get_all() { |
||
| 274 | return $this->fetch_items(); |
||
| 275 | } |
||
| 276 | |||
| 277 | // use with caution, this could allow multiple processes to delete |
||
| 278 | // and send from the queue at the same time |
||
| 279 | function force_checkin() { |
||
| 280 | $this->delete_checkout_id(); |
||
| 281 | } |
||
| 282 | |||
| 283 | // used to lock checkouts from the queue. |
||
| 284 | // tries to wait up to $timeout seconds for the queue to be empty |
||
| 285 | function lock( $timeout = 30 ) { |
||
| 286 | $tries = 0; |
||
| 287 | |||
| 288 | while ( $this->has_any_items() && $tries < $timeout ) { |
||
| 289 | sleep( 1 ); |
||
| 290 | $tries += 1; |
||
| 291 | } |
||
| 292 | |||
| 293 | if ( $tries === 30 ) { |
||
| 294 | return new WP_Error( 'lock_timeout', 'Timeout waiting for sync queue to empty' ); |
||
| 295 | } |
||
| 296 | |||
| 297 | if ( $this->get_checkout_id() ) { |
||
| 298 | return new WP_Error( 'unclosed_buffer', 'There is an unclosed buffer' ); |
||
| 299 | } |
||
| 300 | |||
| 301 | // hopefully this means we can acquire a checkout? |
||
| 302 | $result = $this->set_checkout_id( 'lock' ); |
||
| 303 | |||
| 304 | if ( ! $result || is_wp_error( $result ) ) { |
||
| 305 | return $result; |
||
| 306 | } |
||
| 307 | |||
| 308 | return true; |
||
| 309 | } |
||
| 310 | |||
| 311 | function unlock() { |
||
| 312 | $this->delete_checkout_id(); |
||
| 313 | } |
||
| 314 | |||
| 315 | private function get_checkout_id() { |
||
| 316 | if ( $this->use_named_lock ) { |
||
| 317 | global $wpdb; |
||
| 318 | $wpdb-> |
||
| 319 | } else { |
||
| 320 | return get_transient( $this->get_checkout_transient_name() ); |
||
| 321 | } |
||
| 322 | } |
||
| 323 | |||
| 324 | private function set_checkout_id( $checkout_id ) { |
||
| 325 | if ( $this->use_named_lock ) { |
||
| 326 | |||
| 327 | } else { |
||
| 328 | return set_transient( $this->get_checkout_transient_name(), $checkout_id, 5 * 60 ); // 5 minute timeout |
||
| 329 | } |
||
| 330 | } |
||
| 331 | |||
| 332 | private function delete_checkout_id() { |
||
| 333 | delete_transient( $this->get_checkout_transient_name() ); |
||
| 334 | } |
||
| 335 | |||
| 336 | private function get_checkout_transient_name() { |
||
| 337 | return "jpsq_{$this->id}_checkout"; |
||
| 338 | } |
||
| 339 | |||
| 340 | private function get_next_data_row_option_name() { |
||
| 341 | // this option is specifically chosen to, as much as possible, preserve time order |
||
| 342 | // and minimise the possibility of collisions between multiple processes working |
||
| 343 | // at the same time |
||
| 344 | // TODO: confirm we only need to support PHP 5.05+ (otherwise we'll need to emulate microtime as float, and avoid PHP_INT_MAX) |
||
| 345 | // @see: http://php.net/manual/en/function.microtime.php |
||
| 346 | $timestamp = sprintf( '%.6f', microtime( true ) ); |
||
| 347 | |||
| 348 | // row iterator is used to avoid collisions where we're writing data waaay fast in a single process |
||
| 349 | if ( $this->row_iterator === PHP_INT_MAX ) { |
||
| 350 | $this->row_iterator = 0; |
||
| 351 | } else { |
||
| 352 | $this->row_iterator += 1; |
||
| 353 | } |
||
| 354 | |||
| 355 | return 'jpsq_' . $this->id . '-' . $timestamp . '-' . getmypid() . '-' . $this->row_iterator; |
||
| 356 | } |
||
| 357 | |||
| 358 | private function fetch_items( $limit = null ) { |
||
| 359 | global $wpdb; |
||
| 360 | |||
| 361 | if ( $limit ) { |
||
| 362 | $query_sql = $wpdb->prepare( "SELECT option_name AS id, option_value AS value FROM $wpdb->options WHERE option_name LIKE %s ORDER BY option_name ASC LIMIT %d", "jpsq_{$this->id}-%", $limit ); |
||
| 363 | } else { |
||
| 364 | $query_sql = $wpdb->prepare( "SELECT option_name AS id, option_value AS value FROM $wpdb->options WHERE option_name LIKE %s ORDER BY option_name ASC", "jpsq_{$this->id}-%" ); |
||
| 365 | } |
||
| 366 | |||
| 367 | $items = $wpdb->get_results( $query_sql, OBJECT ); |
||
| 368 | foreach ( $items as $item ) { |
||
| 369 | $item->value = maybe_unserialize( $item->value ); |
||
| 370 | } |
||
| 371 | |||
| 372 | return $items; |
||
| 373 | } |
||
| 374 | |||
| 375 | private function fetch_items_by_id( $item_ids ) { |
||
| 376 | global $wpdb; |
||
| 377 | |||
| 378 | if ( count( $item_ids ) > 0 ) { |
||
| 379 | $sql = "SELECT option_name AS id, option_value AS value FROM $wpdb->options WHERE option_name IN (" . implode( ', ', array_fill( 0, count( $item_ids ), '%s' ) ) . ') ORDER BY option_name ASC'; |
||
| 380 | $query = call_user_func_array( array( $wpdb, 'prepare' ), array_merge( array( $sql ), $item_ids ) ); |
||
| 381 | $items = $wpdb->get_results( $query, OBJECT ); |
||
| 382 | foreach ( $items as $item ) { |
||
| 383 | $item->value = maybe_unserialize( $item->value ); |
||
| 384 | } |
||
| 385 | |||
| 386 | return $items; |
||
| 387 | } else { |
||
| 388 | return array(); |
||
| 389 | } |
||
| 390 | } |
||
| 391 | |||
| 392 | private function validate_checkout( $buffer ) { |
||
| 393 | if ( ! $buffer instanceof Jetpack_Sync_Queue_Buffer ) { |
||
| 394 | return new WP_Error( 'not_a_buffer', 'You must checkin an instance of Jetpack_Sync_Queue_Buffer' ); |
||
| 395 | } |
||
| 396 | |||
| 397 | $checkout_id = $this->get_checkout_id(); |
||
| 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.