| Conditions | 23 |
| Paths | 5764 |
| Total Lines | 170 |
| Lines | 2 |
| Ratio | 1.18 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 208 | private function _xcloner_scheduler_callback( $id, $schedule ) { |
||
| 209 | set_time_limit( 0 ); |
||
| 210 | |||
| 211 | $xcloner = new XCloner(); |
||
| 212 | $xcloner->init(); |
||
| 213 | $this->set_xcloner_container( $xcloner ); |
||
| 214 | |||
| 215 | #$hash = $this->xcloner_settings->get_hash(); |
||
| 216 | #$this->get_xcloner_container()->get_xcloner_settings()->set_hash($hash); |
||
| 217 | |||
| 218 | //$this->xcloner_settings = $this->get_xcloner_container()->get_xcloner_settings(); |
||
| 219 | $this->xcloner_file_system = $this->get_xcloner_container()->get_xcloner_filesystem(); |
||
| 220 | $this->xcloner_encryption = $this->get_xcloner_container()->get_xcloner_encryption(); |
||
| 221 | $this->xcloner_database = $this->get_xcloner_container()->get_xcloner_database(); |
||
| 222 | $this->archive_system = $this->get_xcloner_container()->get_archive_system(); |
||
| 223 | $this->logger = $this->get_xcloner_container()->get_xcloner_logger()->withName( "xcloner_scheduler" ); |
||
| 224 | $this->xcloner_remote_storage = $this->get_xcloner_container()->get_xcloner_remote_storage(); |
||
| 225 | |||
| 226 | $this->logger->info( sprintf( "New schedule hash is %s", $this->xcloner_settings->get_hash() ) ); |
||
| 227 | |||
| 228 | if ( isset( $schedule['backup_params']->diff_start_date ) && $schedule['backup_params']->diff_start_date ) { |
||
| 229 | $this->xcloner_file_system->set_diff_timestamp_start( $schedule['backup_params']->diff_start_date ); |
||
| 230 | } |
||
| 231 | |||
| 232 | if ( $schedule['recurrence'] == "single" ) { |
||
| 233 | $this->disable_single_cron( $schedule['id'] ); |
||
| 234 | } |
||
| 235 | |||
| 236 | if ( ! $schedule ) { |
||
| 237 | $this->logger->info( sprintf( "Could not load schedule with id'%s'", $id ), array( "CRON" ) ); |
||
| 238 | |||
| 239 | return; |
||
| 240 | } |
||
| 241 | |||
| 242 | //echo $this->get_xcloner_container()->get_xcloner_settings()->get_hash(); exit; |
||
| 243 | |||
| 244 | $this->update_hash( $schedule['id'], $this->xcloner_settings->get_hash() ); |
||
| 245 | |||
| 246 | $this->logger->info( sprintf( "Starting cron schedule '%s'", $schedule['name'] ), array( "CRON" ) ); |
||
| 247 | |||
| 248 | $this->xcloner_file_system->set_excluded_files( json_decode( $schedule['excluded_files'] ) ); |
||
| 249 | |||
| 250 | $init = 1; |
||
| 251 | $continue = 1; |
||
| 252 | |||
| 253 | while ( $continue ) { |
||
| 254 | $continue = $this->xcloner_file_system->start_file_recursion( $init ); |
||
| 255 | |||
| 256 | $init = 0; |
||
| 257 | } |
||
| 258 | |||
| 259 | $this->logger->info( sprintf( "File scan finished" ), array( "CRON" ) ); |
||
| 260 | |||
| 261 | $this->logger->info( sprintf( "Starting the database backup" ), array( "CRON" ) ); |
||
| 262 | |||
| 263 | $init = 1; |
||
| 264 | $return['finished'] = 0; |
||
| 265 | |||
| 266 | while ( ! $return['finished'] ) { |
||
| 267 | $return = $this->xcloner_database->start_database_recursion( (array) json_decode( $schedule['table_params'] ), $return, $init ); |
||
| 268 | $init = 0; |
||
| 269 | } |
||
| 270 | |||
| 271 | $this->logger->info( sprintf( "Database backup done" ), array( "CRON" ) ); |
||
| 272 | |||
| 273 | $this->logger->info( sprintf( "Starting file archive process" ), array( "CRON" ) ); |
||
| 274 | |||
| 275 | $init = 0; |
||
| 276 | $return['finished'] = 0; |
||
| 277 | $return['extra'] = array(); |
||
| 278 | |||
| 279 | while ( ! $return['finished'] ) { |
||
| 280 | $return = $this->archive_system->start_incremental_backup( (array) $schedule['backup_params'], $return['extra'], $init ); |
||
| 281 | $init = 0; |
||
| 282 | } |
||
| 283 | $this->logger->info( sprintf( "File archive process FINISHED." ), array( "CRON" ) ); |
||
| 284 | |||
| 285 | //getting the last backup archive file |
||
| 286 | $return['extra']['backup_parent'] = $this->archive_system->get_archive_name_with_extension(); |
||
| 287 | View Code Duplication | if ( $this->xcloner_file_system->is_part( $this->archive_system->get_archive_name_with_extension() ) ) { |
|
| 288 | $return['extra']['backup_parent'] = $this->archive_system->get_archive_name_multipart(); |
||
| 289 | } |
||
| 290 | |||
| 291 | //Updating schedule last backup archive |
||
| 292 | $this->update_last_backup( $schedule['id'], $return['extra']['backup_parent'] ); |
||
| 293 | |||
| 294 | //Encrypting the backup archive |
||
| 295 | $return_encrypted['finished'] = 0; |
||
| 296 | $return_encrypted['start'] = 0; |
||
| 297 | $return_encrypted['iv'] = ''; |
||
| 298 | $return_encrypted['target_file'] = ''; |
||
| 299 | $part = 0; |
||
| 300 | $backup_parts = array(); |
||
| 301 | |||
| 302 | if( $schedule['backup_params']->backup_encrypt){ |
||
| 303 | $this->logger->info( sprintf( "Encrypting backup archive %s.", $return['extra']['backup_parent'] ), array( "CRON" ) ); |
||
| 304 | |||
| 305 | $backup_file = $return['extra']['backup_parent']; |
||
| 306 | |||
| 307 | if ($this->xcloner_file_system->is_multipart($return['extra']['backup_parent'])) { |
||
| 308 | $backup_parts = $this->xcloner_file_system->get_multipart_files($return['extra']['backup_parent']); |
||
| 309 | $backup_file = $backup_parts[$part]; |
||
| 310 | } |
||
| 311 | |||
| 312 | while ( ! $return_encrypted['finished'] ) { |
||
| 313 | $return_encrypted = $this->xcloner_encryption->encrypt_file( |
||
| 314 | $backup_file, |
||
| 315 | "", |
||
| 316 | "", |
||
| 317 | "", |
||
| 318 | "", |
||
| 319 | true, |
||
| 320 | true |
||
| 321 | ); |
||
| 322 | |||
| 323 | if($return_encrypted['finished']) { |
||
| 324 | ++$part; |
||
| 325 | |||
| 326 | if ($part < sizeof($backup_parts)) { |
||
| 327 | $return_encrypted['finished'] = 0; |
||
| 328 | $backup_file = $backup_parts[$part]; |
||
| 329 | } |
||
| 330 | } |
||
| 331 | } |
||
| 332 | } |
||
| 333 | |||
| 334 | //Sending backup to remote storage |
||
| 335 | if ( isset( $schedule['remote_storage'] ) && $schedule['remote_storage'] && array_key_exists( $schedule['remote_storage'], $this->xcloner_remote_storage->get_available_storages() ) ) { |
||
| 336 | $backup_file = $return['extra']['backup_parent']; |
||
| 337 | |||
| 338 | $this->logger->info( sprintf( "Transferring backup to remote storage %s", strtoupper( $schedule['remote_storage'] ) ), array( "CRON" ) ); |
||
| 339 | |||
| 340 | if ( method_exists( $this->xcloner_remote_storage, "upload_backup_to_storage" ) ) { |
||
| 341 | call_user_func_array( array( |
||
| 342 | $this->xcloner_remote_storage, |
||
| 343 | "upload_backup_to_storage" |
||
| 344 | ), array( $backup_file, $schedule['remote_storage'] ) ); |
||
| 345 | } |
||
| 346 | } |
||
| 347 | |||
| 348 | //Sending email notification |
||
| 349 | if ( isset( $schedule['backup_params']->email_notification ) and $to = $schedule['backup_params']->email_notification ) { |
||
| 350 | try { |
||
| 351 | $from = ""; |
||
| 352 | $additional['lines_total'] = $return['extra']['lines_total']; |
||
| 353 | $subject = sprintf( __( "%s - new backup generated %s" ), $schedule['name'], $return['extra']['backup_parent'] ); |
||
| 354 | |||
| 355 | $this->archive_system->send_notification( $to, $from, $subject, $return['extra']['backup_parent'], $schedule, "", $additional ); |
||
| 356 | |||
| 357 | } catch ( Exception $e ) { |
||
| 358 | $this->logger->error( $e->getMessage() ); |
||
| 359 | } |
||
| 360 | } |
||
| 361 | |||
| 362 | //CHECK IF WE SHOULD DELETE BACKUP AFTER REMOTE TRANSFER IS DONE |
||
| 363 | if ( $schedule['remote_storage'] && $this->xcloner_settings->get_xcloner_option( 'xcloner_cleanup_delete_after_remote_transfer' ) ) { |
||
| 364 | $this->logger->info( sprintf( "Deleting %s from local storage matching rule xcloner_cleanup_delete_after_remote_transfer", $return['extra']['backup_parent'] ) ); |
||
| 365 | $this->xcloner_file_system->delete_backup_by_name( $return['extra']['backup_parent'] ); |
||
| 366 | |||
| 367 | } |
||
| 368 | |||
| 369 | //Removing the tmp filesystem used for backup |
||
| 370 | $this->xcloner_file_system->remove_tmp_filesystem(); |
||
| 371 | |||
| 372 | //Backup Storage Cleanup |
||
| 373 | $this->xcloner_file_system->backup_storage_cleanup(); |
||
| 374 | |||
| 375 | //Filesystem Cleanup |
||
| 376 | $this->xcloner_file_system->cleanup_tmp_directories(); |
||
| 377 | } |
||
| 378 | |||
| 425 |
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: