Complex classes like Scheduled_Backup 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 Scheduled_Backup, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class Scheduled_Backup { |
||
14 | |||
15 | /** |
||
16 | * The unique schedule id |
||
17 | * |
||
18 | * @var string |
||
19 | * @access private |
||
20 | */ |
||
21 | private $id = ''; |
||
22 | |||
23 | /** |
||
24 | * The slugified version of the schedule name |
||
25 | * |
||
26 | * @var string |
||
27 | * @access private |
||
28 | */ |
||
29 | private $slug = ''; |
||
30 | |||
31 | /** |
||
32 | * The raw schedule options from the database |
||
33 | * |
||
34 | * @var array |
||
35 | * @access private |
||
36 | */ |
||
37 | private $options = array( |
||
38 | 'max_backups' => 3, |
||
39 | 'excludes' => array(), |
||
40 | 'type' => 'complete', |
||
41 | 'reoccurrence' => 'manually', |
||
42 | ); |
||
43 | |||
44 | /** |
||
45 | * Setup the schedule object |
||
46 | * Loads the options from the database and populates properties |
||
47 | * |
||
48 | * @param string $id |
||
49 | * |
||
50 | * @throws Exception |
||
51 | */ |
||
52 | |||
53 | public function __construct( $id ) { |
||
54 | |||
55 | // Verify the schedule id |
||
56 | if ( ! is_string( $id ) || ! trim( $id ) ) { |
||
57 | throw new \Exception( 'Argument 1 for ' . __METHOD__ . ' must be a non-empty string' ); |
||
58 | } |
||
59 | |||
60 | // Store id for later |
||
61 | $this->id = $id; |
||
62 | |||
63 | // Load the options |
||
64 | $this->options = array_merge( $this->options, array_filter( (array) get_option( 'hmbkp_schedule_' . $this->get_id() ) ) ); |
||
65 | |||
66 | if ( defined( 'HMBKP_SCHEDULE_START_TIME' ) && strtotime( 'HMBKP_SCHEDULE_START_TIME' ) ) { |
||
67 | $this->set_schedule_start_time( strtotime( 'HMBKP_SCHEDULE_START_TIME' ) ); |
||
68 | } |
||
69 | |||
70 | // Setup the schedule if it isn't set |
||
71 | if ( ( ! $this->is_cron_scheduled() && $this->get_reoccurrence() !== 'manually' ) ) { |
||
72 | $this->schedule(); |
||
73 | } |
||
74 | |||
75 | $this->backup_filename = implode( '-', array( |
||
|
|||
76 | sanitize_title( str_ireplace( array( |
||
77 | 'http://', |
||
78 | 'https://', |
||
79 | 'www', |
||
80 | ), '', home_url() ) ), |
||
81 | $this->get_id(), |
||
82 | $this->get_type(), |
||
83 | current_time( 'Y-m-d-H-i-s' ), |
||
84 | ) ) . '.zip'; |
||
85 | |||
86 | $this->database_dump_filename = implode( '-', array( |
||
87 | 'database', |
||
88 | sanitize_title( str_ireplace( array( 'http://', 'https://', 'www' ), '', home_url() ) ), |
||
89 | $this->get_id(), |
||
90 | ) ) . '.sql'; |
||
91 | |||
92 | $this->status = new Backup_Status( $this->get_id() ); |
||
93 | |||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Get the id for this schedule |
||
98 | */ |
||
99 | public function get_id() { |
||
102 | |||
103 | /** |
||
104 | * Get a slugified version of name |
||
105 | */ |
||
106 | public function get_slug() { |
||
116 | |||
117 | /** |
||
118 | * Returns the given option value |
||
119 | * |
||
120 | * @param $option_name |
||
121 | * @return mixed The option value |
||
122 | */ |
||
123 | public function get_schedule_option( $option_name ) { |
||
128 | |||
129 | /** |
||
130 | * Get the name of this backup schedule |
||
131 | * |
||
132 | * @return string |
||
133 | */ |
||
134 | public function get_name() { |
||
137 | |||
138 | /** |
||
139 | * Get the type of backup |
||
140 | * |
||
141 | * @return string |
||
142 | */ |
||
143 | public function get_type() { |
||
146 | |||
147 | /** |
||
148 | * Set the type of backup |
||
149 | * |
||
150 | * @param string $type |
||
151 | */ |
||
152 | public function set_type( $type ) { |
||
157 | |||
158 | /** |
||
159 | * Get the exclude rules |
||
160 | * |
||
161 | * @return array |
||
162 | */ |
||
163 | public function get_excludes() { |
||
166 | |||
167 | /** |
||
168 | * Set the exclude rules |
||
169 | * |
||
170 | * @param mixed $excludes A comma separated list or array of exclude rules |
||
171 | * @param bool $append Whether to replace or append to existing rules |
||
172 | * |
||
173 | * @return string |
||
174 | */ |
||
175 | public function set_excludes( $exclude_rules, $append = false ) { |
||
187 | |||
188 | /** |
||
189 | * Get the maximum number of backups to keep |
||
190 | * |
||
191 | * @return int |
||
192 | */ |
||
193 | public function get_max_backups() { |
||
196 | |||
197 | /** |
||
198 | * Set the maximum number of backups to keep |
||
199 | * |
||
200 | * @param int $max |
||
201 | * |
||
202 | * @return WP_Error|boolean |
||
203 | */ |
||
204 | public function set_max_backups( $max ) { |
||
207 | |||
208 | public function get_status() { |
||
211 | |||
212 | /** |
||
213 | * Back compat with old set_status mathod |
||
214 | * |
||
215 | * @deprecated 3.4 Backup->status->set_status() |
||
216 | */ |
||
217 | public function set_status( $message ) { |
||
218 | _deprecated_function( __FUNCTION__, '3.4', 'Backup->status->set_status()' ); |
||
219 | $this->status->set_status( $message ); |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * Get the array of services options for this schedule |
||
224 | * |
||
225 | * @param $service |
||
226 | * @param null $option |
||
227 | * |
||
228 | * @return array |
||
229 | */ |
||
230 | public function get_service_options( $service, $option = null ) { |
||
249 | |||
250 | /** |
||
251 | * Set the service options for this schedule |
||
252 | * |
||
253 | * @param $service |
||
254 | * @param array $options |
||
255 | */ |
||
256 | public function set_service_options( $service, array $options ) { |
||
259 | |||
260 | /** |
||
261 | * Get the start time for the schedule |
||
262 | * |
||
263 | * @return int timestamp || 0 for manual only schedules |
||
264 | */ |
||
265 | public function get_schedule_start_time( $gmt = true ) { |
||
286 | |||
287 | /** |
||
288 | * Set the schedule start time. |
||
289 | * |
||
290 | * @param Int $time A valid timestamp |
||
291 | */ |
||
292 | public function set_schedule_start_time( $time ) { |
||
299 | |||
300 | /** |
||
301 | * Get the schedule reoccurrence |
||
302 | * |
||
303 | */ |
||
304 | public function get_reoccurrence() { |
||
307 | |||
308 | /** |
||
309 | * Set the schedule reoccurrence |
||
310 | * |
||
311 | * @param string $reoccurrence |
||
312 | * |
||
313 | * @return \WP_Error|null|boolean |
||
314 | */ |
||
315 | public function set_reoccurrence( $reoccurrence ) { |
||
316 | |||
317 | $hmbkp_schedules = cron_schedules(); |
||
318 | |||
319 | // Check it's valid |
||
320 | if ( ! is_string( $reoccurrence ) || ! trim( $reoccurrence ) || ( ! in_array( $reoccurrence, array_keys( $hmbkp_schedules ) ) ) && 'manually' !== $reoccurrence ) { |
||
321 | return new \WP_Error( 'hmbkp_invalid_argument_error', sprintf( __( 'Argument 1 for %s must be a valid cron recurrence or "manually"', 'backupwordpress' ), __METHOD__ ) ); |
||
322 | } |
||
323 | |||
324 | // If the recurrence is already set to the same thing then there's no need to continue |
||
325 | if ( isset( $this->options['reoccurrence'] ) && $this->options['reoccurrence'] === $reoccurrence && $this->is_cron_scheduled() ) { |
||
326 | return; |
||
327 | } |
||
328 | |||
329 | $this->options['reoccurrence'] = $reoccurrence; |
||
330 | |||
331 | if ( 'manually' === $reoccurrence ) { |
||
332 | $this->unschedule(); |
||
333 | |||
334 | } else { |
||
335 | $this->schedule(); |
||
336 | } |
||
337 | |||
338 | return true; |
||
339 | |||
340 | } |
||
341 | |||
342 | /** |
||
343 | * Get the interval between backups |
||
344 | * |
||
345 | * @return int |
||
346 | */ |
||
347 | public function get_interval() { |
||
358 | |||
359 | /** |
||
360 | * Get the next occurrence of this scheduled backup |
||
361 | * |
||
362 | */ |
||
363 | public function get_next_occurrence( $gmt = true ) { |
||
378 | |||
379 | public function is_cron_scheduled() { |
||
382 | |||
383 | /** |
||
384 | * Schedule the backup cron |
||
385 | * |
||
386 | */ |
||
387 | public function schedule() { |
||
397 | |||
398 | |||
399 | /** |
||
400 | * Unschedule the backup cron. |
||
401 | * |
||
402 | * @return void |
||
403 | */ |
||
404 | public function unschedule() { |
||
407 | |||
408 | /** |
||
409 | * Run the backup |
||
410 | * |
||
411 | */ |
||
412 | public function run() { |
||
413 | |||
414 | // Don't run if this schedule is already running |
||
415 | if ( $this->status->is_started() ) { |
||
416 | return; |
||
417 | } |
||
418 | |||
419 | // Setup our Site Backup Object |
||
420 | $backup = new Backup( $this->get_backup_filename(), $this->get_database_dump_filename() ); |
||
421 | $backup->set_type( $this->get_type() ); |
||
422 | $backup->set_excludes( $this->get_excludes() ); |
||
423 | $backup->set_status( $this->status ); |
||
424 | |||
425 | $this->do_action( 'hmbkp_backup_started', $backup ); |
||
426 | |||
427 | $this->status->start( $this->get_backup_filename(), __( 'Starting backup...', 'backupwordpress' ) ); |
||
428 | |||
429 | $this->status->set_status( __( 'Deleting old backups...', 'backupwordpress' ) ); |
||
430 | |||
431 | // Delete old backups now in-case we fatal error during the backup process |
||
432 | $this->delete_old_backups(); |
||
433 | |||
434 | $backup->run(); |
||
435 | |||
436 | $errors = array_merge( $backup->errors, $backup->warnings ); |
||
437 | $notices = array(); |
||
438 | foreach ( $errors as $key => $error ) { |
||
439 | $key = str_replace( array( __NAMESPACE__ . '\\', '_File_Backup_Engine', '_Database_Backup_Engine' ), array( '', '', '' ), $key ); |
||
440 | $notices[] = $key . ': ' . implode( ', ', $error ); |
||
441 | } |
||
442 | Notices::get_instance()->set_notices( 'backup_errors', $notices ); |
||
443 | |||
444 | $this->status->set_status( __( 'Deleting old backups...', 'backupwordpress' ) ); |
||
445 | |||
446 | // Delete old backups again |
||
447 | $this->delete_old_backups(); |
||
448 | |||
449 | $this->do_action( 'hmbkp_backup_complete', $backup ); |
||
450 | |||
451 | $this->status->finish(); |
||
452 | $this->update_average_schedule_run_time( $this->status->get_start_time(), time() ); |
||
453 | |||
454 | } |
||
455 | |||
456 | public function get_backup_filename() { |
||
464 | |||
465 | public function get_database_dump_filename() { |
||
468 | |||
469 | /** |
||
470 | * Hook into the actions fired in the Backup class and set the status |
||
471 | * |
||
472 | * @param $action |
||
473 | */ |
||
474 | public function do_action( $action, Backup $backup ) { |
||
475 | |||
476 | // Pass the actions to all the services |
||
477 | // Todo should be decoupled into the service class |
||
478 | foreach ( Services::get_services( $this ) as $service ) { |
||
479 | if ( is_wp_error( $service ) ) { |
||
480 | return $service; |
||
481 | } |
||
482 | $service->action( $action, $backup ); |
||
483 | } |
||
484 | |||
485 | } |
||
486 | |||
487 | /** |
||
488 | * Calculate schedule run time. |
||
489 | * |
||
490 | * @param int Timestamp $end |
||
491 | */ |
||
492 | public function update_average_schedule_run_time( $start, $end ) { |
||
515 | |||
516 | /** |
||
517 | * Calculates the average run time for this schedule. |
||
518 | * |
||
519 | * @return string |
||
520 | */ |
||
521 | public function get_schedule_average_duration() { |
||
559 | |||
560 | /** |
||
561 | * Get the backups created by this schedule |
||
562 | * |
||
563 | * @todo look into using recursiveDirectoryIterator and recursiveRegexIterator |
||
564 | * @return string[] - file paths of the backups |
||
565 | */ |
||
566 | public function get_backups() { |
||
567 | |||
568 | $files = array(); |
||
569 | |||
570 | if ( $handle = @opendir( Path::get_path() ) ) { |
||
571 | |||
572 | while ( false !== ( $file = readdir( $handle ) ) ) { |
||
573 | |||
574 | if ( pathinfo( $file, PATHINFO_EXTENSION ) === 'zip' && strpos( $file, $this->get_id() ) !== false && ( isset( $this->status ) && $this->get_backup_filename() !== $file ) ) { |
||
575 | $files[ @filemtime( trailingslashit( Path::get_path() ) . $file ) ] = trailingslashit( Path::get_path() ) . $file; |
||
576 | } |
||
577 | } |
||
578 | |||
579 | closedir( $handle ); |
||
580 | |||
581 | } |
||
582 | |||
583 | krsort( $files ); |
||
584 | |||
585 | return $files; |
||
586 | |||
587 | } |
||
588 | |||
589 | /** |
||
590 | * Delete old backups |
||
591 | * |
||
592 | * @access private |
||
593 | */ |
||
594 | public function delete_old_backups() { |
||
603 | |||
604 | /** |
||
605 | * Delete a specific back up file created by this schedule |
||
606 | * |
||
607 | * @param string $filepath |
||
608 | * |
||
609 | * @return \WP_Error|boolean |
||
610 | */ |
||
611 | public function delete_backup( $filepath ) { |
||
633 | |||
634 | /** |
||
635 | * Delete all back up files created by this schedule |
||
636 | * |
||
637 | */ |
||
638 | public function delete_backups() { |
||
641 | |||
642 | /** |
||
643 | * Save the schedules options. |
||
644 | * |
||
645 | */ |
||
646 | public function save() { |
||
647 | |||
657 | |||
658 | /** |
||
659 | * Cancel this schedule |
||
660 | * |
||
661 | * Cancels the cron job, removes the schedules options |
||
662 | * and optionally deletes all backups created by |
||
663 | * this schedule. |
||
664 | * |
||
665 | */ |
||
666 | public function cancel( $delete_backups = false ) { |
||
683 | } |
||
684 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: