GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#1025)
by Tom
02:21
created

Backup::perform_backup()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 24
rs 8.9713
cc 3
eloc 9
nc 3
nop 1
1
<?php
2
3
namespace HM\BackUpWordPress;
4
5
class Backup {
6
7
	private $excludes;
8
	public $warnings = array();
9
	public $errors = array();
10
	private $backup_filename;
11
	private $database_dump_filename;
12
	private $backup_filepath = '';
13
	private $database_dump_filepath = '';
14
	private $status = null;
15
	private $type = 'complete';
16
17
	public function __construct( $backup_filename, $database_dump_filename = null ) {
18
		$this->backup_filename = $backup_filename;
19
		$this->database_dump_filename = $database_dump_filename;
20
	}
21
22
	public function set_type( $type ) {
23
		$this->type = $type;
24
	}
25
26
	public function set_backup_filename( $filename ) {
27
		$this->backup_filename = $filename;
28
	}
29
30
	public function set_status( Backup_Status $status ) {
31
		$this->status = $status;
32
	}
33
34
	public function set_excludes( Excludes $excludes ) {
35
		$this->excludes = $excludes;
36
	}
37
38
	public function run() {
39
40
		Path::get_instance()->cleanup();
41
42
		if ( $this->type !== 'file' ) {
43
			$this->backup_database();
44
		}
45
46
		if ( $this->type !== 'database' ) {
47
			$this->backup_files();
48
		}
49
50
		Path::get_instance()->cleanup();
51
52
	}
53
54
	public function backup_database() {
55
56
		if ( $this->status ) {
57
			$this->status->set_status( __( 'Backing up database...', 'backupwordpress' ) );
58
		}
59
60
		$database_backup_engines = apply_filters( 'hmbkp_database_backup_engines', array(
61
			new Mysqldump_Database_Backup_Engine,
62
			new IMysqldump_Database_Backup_Engine
63
		) );
64
65
		// Set the file backup engine settings
66
		if (  $this->database_dump_filename ) {
67
			foreach( $database_backup_engines as &$backup_engine ) {
68
				$backup_engine->set_backup_filename( $this->database_dump_filename );
69
			}
70
		}
71
72
		// Dump the database
73
		$database_dump = $this->perform_backup( $database_backup_engines );
74
75
		if ( is_a( $database_dump, __NAMESPACE__ . '\\Backup_Engine' ) ) {
76
			$this->database_dump_filepath = $database_dump->get_backup_filepath();
77
		}
78
79
		// Fire up the file backup engines
80
		$file_backup_engines = apply_filters( 'hmbkp_file_backup_engines', array(
81
			new Zip_File_Backup_Engine,
82
			new Zip_Archive_File_Backup_Engine
83
		) );
84
85
		// Set the file backup engine settings
86
		foreach( $file_backup_engines as &$backup_engine ) {
87
			$backup_engine->set_backup_filename( $this->backup_filename );
88
			$backup_engine->set_excludes( new Excludes( array( '*.zip', 'index.html', '.htaccess', '.*-running', '.files' ) ) );
89
		}
90
91
		// Zip up the database dump
92
		$root = Path::get_root();
93
		Path::get_instance()->set_root( Path::get_path() );
94
		$file_backup = $this->perform_backup( $file_backup_engines );
95
		Path::get_instance()->set_root( $root );
96
97
		if ( is_a( $file_backup, __NAMESPACE__ . '\\Backup_Engine' ) ) {
98
			$this->backup_filepath = $file_backup->get_backup_filepath();
99
		}
100
101
		// Delete the Database Backup now that we've zipped it up
102
		if ( file_exists( $this->database_dump_filepath ) ) {
103
			unlink( $this->database_dump_filepath );
104
		}
105
106
	}
107
108
	public function backup_files() {
109
110
		if ( $this->status ) {
111
			$this->status->set_status( __( 'Backing up files...', 'backupwordpress' ) );
112
		}
113
114
		// Fire up the file backup engines
115
		$backup_engines = apply_filters( 'hmbkp_file_backup_engines', array(
116
			new Zip_File_Backup_Engine,
117
			new Zip_Archive_File_Backup_Engine
118
		) );
119
120
		// Set the file backup engine settings
121
		foreach( $backup_engines as &$backup_engine ) {
122
			$backup_engine->set_backup_filename( $this->backup_filename );
123
			if ( $this->excludes ) {
124
				$backup_engine->set_excludes( $this->excludes );
125
			}
126
		}
127
128
		$file_backup = $this->perform_backup( $backup_engines );
129
130
		if ( is_a( $file_backup, __NAMESPACE__ . '\\Backup_Engine' ) ) {
131
			$this->backup_filepath = $file_backup->get_backup_filepath();
132
		}
133
134
	}
135
136
	/**
137
	 * Perform the backup by iterating through each Backup_Engine in turn until
138
	 * we find one which works. If a backup filename or any excludes have been
139
	 * set then those are passed to each Backup_Engine.
140
	 */
141
	public function perform_backup( Array $backup_engines ) {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
142
143
		foreach ( $backup_engines as $backup_engine ) {
144
145
			/**
146
			 * If the backup_engine completes the backup then we
147
			 * clear any errors or warnings from previously failed backup_engines
148
			 * and return the successful one.
149
			 */
150
			if ( $backup_engine->backup() ) {
151
				$this->errors = array();
152
				$this->warnings = $backup_engine->get_warnings();
153
				return $backup_engine;
154
			}
155
156
			// Store all the errors and warnings as they are shown if all engines fail
157
			$this->warnings = array_merge( $this->warnings, $backup_engine->get_warnings() );
158
			$this->errors   = array_merge( $this->errors, $backup_engine->get_errors() );
159
160
		}
161
162
		return false;
163
164
	}
165
166
	public function get_warnings() {
167
		return $this->warnings;
168
	}
169
170
	public function get_errors() {
171
		return $this->errors;
172
	}
173
174
	/**
175
	 * Add an warning to the errors warnings.
176
	 *
177
	 * A warning is always treat as non-fatal and should only be used for recoverable
178
	 * issues with the backup process.
179
	 *
180
	 * @param  string $context The context for the warning.
181
	 * @param  string $error   The warning that was encountered.
0 ignored issues
show
Bug introduced by
There is no parameter named $error. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
182
	 */
183 View Code Duplication
	public function warning( $context, $warning ) {
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
184
185
		if ( empty( $context ) || empty( $warning ) ) {
186
			return;
187
		}
188
189
		// Ensure we don't store duplicate warnings by md5'ing the error as the key
190
		$this->warnings[ $context ][ md5( implode( ':', (array) $warning ) ) ] = $warning;
191
192
	}
193
194
	/**
195
	 * Back compat with old error method
196
	 *
197
	 * Only the backup engines themselves can fire fatal errors
198
	 *
199
	 * @deprecated 3.4 Backup->warning( $context, $warning )
200
	 */
201
	public function error( $context, $message ) {
202
		_deprecated_function( __FUNCTION__, '3.4', 'Backup->warning( $context, $warning )' );
203
		$this->warning( $context, $message );
204
	}
205
206
	public function get_database_backup_filepath() {
207
		return $this->database_dump_filepath;
208
	}
209
210
	public function get_backup_filepath() {
211
		return $this->backup_filepath;
212
	}
213
214
	/**
215
	 * Back compat with old method name
216
	 *
217
	 * @see Backup::get_backup_filepath()
218
 	 * @deprecated 3.4 Use Backup::get_backup_filepath()
219
	 */
220
	public function get_archive_filepath() {
221
		_deprecated_function( __FUNCTION__, '3.4', 'get_backup_filepath()' );
222
		return $this->get_backup_filepath();
223
	}
224
225
}
226