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