Complex classes like Jetpack_Sync_Module_Full_Sync 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_Module_Full_Sync, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class Jetpack_Sync_Module_Full_Sync extends Jetpack_Sync_Module { |
||
| 18 | const STATUS_OPTION_PREFIX = 'jetpack_sync_full_'; |
||
| 19 | const FULL_SYNC_TIMEOUT = 3600; |
||
| 20 | |||
| 21 | public function name() { |
||
| 24 | |||
| 25 | function init_full_sync_listeners( $callable ) { |
||
| 26 | // synthetic actions for full sync |
||
| 27 | add_action( 'jetpack_full_sync_start', $callable ); |
||
| 28 | add_action( 'jetpack_full_sync_end', $callable ); |
||
| 29 | add_action( 'jetpack_full_sync_cancelled', $callable ); |
||
| 30 | } |
||
| 31 | |||
| 32 | function init_before_send() { |
||
| 36 | |||
| 37 | function start( $modules = null ) { |
||
| 38 | $was_already_running = $this->is_started() && ! $this->is_finished(); |
||
| 39 | |||
| 40 | // remove all evidence of previous full sync items and status |
||
| 41 | $this->reset_data(); |
||
| 42 | |||
| 43 | if ( $was_already_running ) { |
||
| 44 | /** |
||
| 45 | * Fires when a full sync is cancelled. |
||
| 46 | * |
||
| 47 | * @since 4.2.0 |
||
| 48 | */ |
||
| 49 | do_action( 'jetpack_full_sync_cancelled' ); |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Fires when a full sync begins. This action is serialized |
||
| 54 | * and sent to the server so that it knows a full sync is coming. |
||
| 55 | * |
||
| 56 | * @since 4.2.0 |
||
| 57 | */ |
||
| 58 | do_action( 'jetpack_full_sync_start' ); |
||
| 59 | $this->update_status_option( 'started', time() ); |
||
| 60 | |||
| 61 | // configure modules |
||
| 62 | if ( ! is_array( $modules ) ) { |
||
| 63 | $modules = array(); |
||
| 64 | } |
||
| 65 | |||
| 66 | // by default, all modules are fully enabled |
||
| 67 | if ( count( $modules ) === 0 ) { |
||
| 68 | $default_module_config = true; |
||
| 69 | } else { |
||
| 70 | $default_module_config = false; |
||
| 71 | } |
||
| 72 | |||
| 73 | // set default configuration, calculate totals, and save configuration if totals > 0 |
||
| 74 | foreach ( Jetpack_Sync_Modules::get_modules() as $module ) { |
||
| 75 | $module_name = $module->name(); |
||
| 76 | if ( ! isset( $modules[ $module_name ] ) ) { |
||
| 77 | $modules[ $module_name ] = $default_module_config; |
||
| 78 | } |
||
| 79 | |||
| 80 | // check if this module is enabled |
||
| 81 | if ( ! ( $module_config = $modules[ $module_name ] ) ) { |
||
| 82 | continue; |
||
| 83 | } |
||
| 84 | |||
| 85 | $total_items = $module->estimate_full_sync_actions( $module_config ); |
||
| 86 | |||
| 87 | if ( ! is_null( $total_items ) && $total_items > 0 ) { |
||
| 88 | $this->update_status_option( "{$module_name}_total", $total_items ); |
||
| 89 | $this->update_status_option( "{$module_name}_config", $module_config ); |
||
| 90 | } |
||
| 91 | } |
||
| 92 | |||
| 93 | foreach ( Jetpack_Sync_Modules::get_modules() as $module ) { |
||
| 94 | $module_name = $module->name(); |
||
| 95 | $module_config = $modules[ $module_name ]; |
||
| 96 | |||
| 97 | // check if this module is enabled |
||
| 98 | if ( ! $module_config ) { |
||
| 99 | continue; |
||
| 100 | } |
||
| 101 | |||
| 102 | $items_enqueued = $module->enqueue_full_sync_actions( $module_config ); |
||
| 103 | |||
| 104 | if ( ! is_null( $items_enqueued ) && $items_enqueued > 0 ) { |
||
| 105 | $this->update_status_option( "{$module_name}_queued", $items_enqueued ); |
||
| 106 | } |
||
| 107 | } |
||
| 108 | |||
| 109 | $this->update_status_option( 'queue_finished', time() ); |
||
| 110 | |||
| 111 | $store = new Jetpack_Sync_WP_Replicastore(); |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Fires when a full sync ends. This action is serialized |
||
| 115 | * and sent to the server with checksums so that we can confirm the |
||
| 116 | * sync was successful. |
||
| 117 | * |
||
| 118 | * @since 4.2.0 |
||
| 119 | */ |
||
| 120 | do_action( 'jetpack_full_sync_end', $store->checksum_all() ); |
||
| 121 | |||
| 122 | return true; |
||
| 123 | } |
||
| 124 | |||
| 125 | function update_sent_progress_action( $actions ) { |
||
| 126 | |||
| 127 | // quick way to map to first items with an array of arrays |
||
| 128 | $actions_with_counts = array_count_values( array_map( 'reset', $actions ) ); |
||
| 129 | |||
| 130 | if ( ! $this->is_started() || $this->is_finished() ) { |
||
| 131 | return; |
||
| 132 | } |
||
| 133 | |||
| 134 | if ( isset( $actions_with_counts['jetpack_full_sync_start'] ) ) { |
||
| 135 | $this->update_status_option( 'sent_started', time() ); |
||
| 136 | } |
||
| 137 | |||
| 138 | foreach ( Jetpack_Sync_Modules::get_modules() as $module ) { |
||
| 139 | $module_actions = $module->get_full_sync_actions(); |
||
| 140 | $status_option_name = "{$module->name()}_sent"; |
||
| 141 | $items_sent = $this->get_status_option( $status_option_name, 0 ); |
||
| 142 | |||
| 143 | foreach ( $module_actions as $module_action ) { |
||
| 144 | if ( isset( $actions_with_counts[ $module_action ] ) ) { |
||
| 145 | $items_sent += $actions_with_counts[ $module_action ]; |
||
| 146 | } |
||
| 147 | } |
||
| 148 | |||
| 149 | if ( $items_sent > 0 ) { |
||
| 150 | $this->update_status_option( $status_option_name, $items_sent ); |
||
| 151 | } |
||
| 152 | } |
||
| 153 | |||
| 154 | if ( isset( $actions_with_counts['jetpack_full_sync_end'] ) ) { |
||
| 155 | $this->update_status_option( 'finished', time() ); |
||
| 156 | } |
||
| 157 | } |
||
| 158 | |||
| 159 | public function is_started() { |
||
| 162 | |||
| 163 | public function is_finished() { |
||
| 164 | return !! $this->get_status_option( 'finished' ); |
||
| 166 | |||
| 167 | public function get_status() { |
||
| 201 | |||
| 202 | public function clear_status() { |
||
| 216 | |||
| 217 | public function reset_data() { |
||
| 223 | |||
| 224 | private function get_status_option( $option, $default = null ) { |
||
| 236 | |||
| 237 | private function update_status_option( $name, $value ) { |
||
| 241 | } |
||
| 242 |