/**
  * Upload the backup to S3 on the hmbkp_backup_complete
  *
  * @see  HM_Backup::do_action
  *
  * @param  string $action The action received from the backup
  *
  * @return void
  */
 public function action($action, Backup $backup)
 {
     if ('hmbkp_backup_complete' === $action && $this->get_field_value('S3')) {
         $file = $backup->get_archive_filepath();
         if (defined('HMBKP_AWS_ACCESS_KEY')) {
             $key = HMBKP_AWS_ACCESS_KEY;
         } else {
             $key = $this->get_field_value('access_key');
         }
         if (defined('HMBKP_AWS_SECRET_KEY')) {
             $secret = HMBKP_AWS_SECRET_KEY;
         } else {
             $secret = $this->get_field_value('secret_key');
             if (defined('HMBKP_AWS_REGION')) {
                 $region = HMBKP_AWS_REGION;
             } else {
                 $region = $this->get_field_value('aws_region');
             }
         }
         if (defined('HMBKP_AWS_BUCKET')) {
             $bucket = HMBKP_AWS_BUCKET;
         } else {
             $bucket = $this->get_field_value('bucket');
         }
         $this->fetch_s3_connection($key, $secret, $region);
         if (!empty($this->s3)) {
             $this->schedule->set_status(__('Uploading to Amazon S3', 'backupwordpress'));
             $this->upload_backup($file, $bucket);
             $this->delete_old_backups($bucket);
         } else {
             $backup->error('S3', sprintf(__('Could not connect to %s', 'backupwordpress'), $this->get_field_value('bucket')));
         }
     }
 }
 /**
  * Hook into the actions fired in the Backup class and set the status
  *
  * @param $action
  */
 public function do_action($action, Backup $backup)
 {
     // Pass the actions to all the services
     // Todo should be decoupled into the service class
     foreach (Services::get_services($this) as $service) {
         $service->action($action, $backup);
     }
     switch ($action) {
         case 'hmbkp_backup_started':
         case 'hmbkp_mysqldump_finished':
         case 'hmbkp_archive_finished':
             break;
         case 'hmbkp_mysqldump_started':
             $this->set_status(sprintf(__('Dumping Database %s', 'backupwordpress'), '(<code>' . $this->backup->get_mysqldump_method() . '</code>)'));
             break;
         case 'hmbkp_mysqldump_verify_started':
             $this->set_status(sprintf(__('Verifying Database Dump %s', 'backupwordpress'), '(<code>' . $this->backup->get_mysqldump_method() . '</code>)'));
             break;
         case 'hmbkp_archive_started':
             $this->set_status(sprintf(__('Creating zip archive %s', 'backupwordpress'), '(<code>' . $this->backup->get_archive_method() . '</code>)'));
             break;
         case 'hmbkp_archive_verify_started':
             $this->set_status(sprintf(__('Verifying Zip Archive %s', 'backupwordpress'), '(<code>' . $this->backup->get_archive_method() . '</code>)'));
             break;
         case 'hmbkp_backup_complete':
             $this->set_status(__('Finishing Backup', 'backupwordpress'));
             $this->update_average_schedule_run_time($this->get_schedule_running_start_time(), time());
             break;
         case 'hmbkp_error':
             if ($this->backup->get_errors()) {
                 $file = $this->get_path() . '/.backup_errors';
                 if (file_exists($file)) {
                     @unlink($file);
                 }
                 if (!($handle = @fopen($file, 'w'))) {
                     return;
                 }
                 fwrite($handle, json_encode($this->backup->get_errors()));
                 fclose($handle);
             }
             break;
         case 'hmbkp_warning':
             if ($this->backup->get_warnings()) {
                 $file = $this->get_path() . '/.backup_warnings';
                 if (file_exists($file)) {
                     @unlink($file);
                 }
                 if (!($handle = @fopen($file, 'w'))) {
                     return;
                 }
                 fwrite($handle, json_encode($this->backup->get_warnings()));
                 fclose($handle);
             }
             break;
         default:
             return new \WP_Error('unexpected-error', __('An unexpected error occured', 'backupwordpress'));
     }
 }