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 (#936)
by Tom
02:41
created

Site_Backup::warning()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 10
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 10
loc 10
rs 9.4286
cc 3
eloc 4
nc 2
nop 2
1
<?php
2
3
namespace HM\BackUpWordPress;
4
5
class Site_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
		if ( $this->type !== 'file' ) {
41
			$this->backup_database();
42
		}
43
44
		if ( $this->type !== 'database' ) {
45
			$this->backup_files();
46
		}
47
48
	}
49
50
	public function backup_database() {
51
52
		if ( $this->status ) {
53
			$this->status->set_status( __( 'Backing up database...', 'backupwordpress' ) );
54
		}
55
56
		$database_backup_engines = apply_filters( 'hmbkp_database_backup_engines', array(
57
			new Mysqldump_Database_Backup_Engine,
58
			new IMysqldump_Database_Backup_Engine
59
		) );
60
61
		// Set the file backup engine settings
62
		if (  $this->database_dump_filename ) {
63
			foreach( $database_backup_engines as &$backup_engine ) {
64
				$backup_engine->set_backup_filename( $this->database_dump_filename );
65
			}
66
		}
67
68
		// Dump the database
69
		$database_dump = $this->perform_backup( $database_backup_engines );
70
71
		if ( is_a( $database_dump, __NAMESPACE__ . '\\Backup_Engine' ) ) {
72
			$this->database_dump_filepath = $database_dump->get_backup_filepath();
73
		}
74
75
		// Fire up the file backup engines
76
		$file_backup_engines = apply_filters( 'hmbkp_file_backup_engines', array(
77
			new Zip_File_Backup_Engine,
78
			new Zip_Archive_File_Backup_Engine
79
		) );
80
81
		// Set the file backup engine settings
82
		foreach( $file_backup_engines as &$backup_engine ) {
83
			$backup_engine->set_backup_filename( $this->backup_filename );
84
			$backup_engine->set_excludes( new Excludes( array( '*.zip', 'index.html', '.htaccess', '.*-running' ) ) );
85
		}
86
87
		// Zip up the database dump
88
		$root = Path::get_root();
89
		Path::get_instance()->set_root( Path::get_path() );
90
		$file_backup = $this->perform_backup( $file_backup_engines );
91
		Path::get_instance()->set_root( $root );
92
93
		if ( is_a( $file_backup, __NAMESPACE__ . '\\Backup_Engine' ) ) {
94
			$this->backup_filepath = $file_backup->get_backup_filepath();
95
		}
96
97
		// Delete the Database Backup now that we've zipped it up
98
		if ( file_exists( $this->database_dump_filepath ) ) {
99
			unlink( $this->database_dump_filepath );
100
		}
101
102
	}
103
104
	public function backup_files() {
105
106
		if ( $this->status ) {
107
			$this->status->set_status( __( 'Backing up files...', 'backupwordpress' ) );
108
		}
109
110
		// Fire up the file backup engines
111
		$backup_engines = apply_filters( 'hmbkp_file_backup_engines', array(
112
			new Zip_File_Backup_Engine,
113
			new Zip_Archive_File_Backup_Engine
114
		) );
115
116
		// Set the file backup engine settings
117
		foreach( $backup_engines as &$backup_engine ) {
118
			$backup_engine->set_backup_filename( $this->backup_filename );
119
			if ( $this->excludes ) {
120
				$backup_engine->set_excludes( $this->excludes );
121
			}
122
		}
123
124
		$file_backup = $this->perform_backup( $backup_engines );
125
126
		if ( is_a( $file_backup, __NAMESPACE__ . '\\Backup_Engine' ) ) {
127
			$this->backup_filepath = $file_backup->get_backup_filepath();
128
		}
129
130
	}
131
132
	/**
133
	 * Perform the backup by iterating through each Backup_Engine in turn until
134
	 * we find one which works. If a backup filename or any excludes have been
135
	 * set then those are passed to each Backup_Engine.
136
	 */
137
	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...
138
139
		foreach ( $backup_engines as $backup_engine ) {
140
141
			if ( $backup_engine->backup() ) {
142
				$this->warnings = array_merge( $this->warnings, $backup_engine->get_warnings() );
143
				return $backup_engine;
144
			}
145
			$this->warnings = array_merge( $this->warnings, $backup_engine->get_warnings() );
146
			$this->errors = array_merge( $this->errors, $backup_engine->get_errors() );
147
		}
148
149
		return false;
150
151
	}
152
153
	public function get_warnings() {
154
		return $this->warnings;
155
	}
156
157
	public function get_errors() {
158
		return $this->errors;
159
	}
160
161
	/**
162
	 * Add an warning to the errors warnings.
163
	 *
164
	 * A warning is always treat as non-fatal and should only be used for recoverable
165
	 * issues with the backup process.
166
	 *
167
	 * @param  string $context The context for the warning.
168
	 * @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...
169
	 */
170 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...
171
172
		if ( empty( $context ) || empty( $warning ) ) {
173
			return;
174
		}
175
176
		// Ensure we don't store duplicate warnings by md5'ing the error as the key
177
		$this->warnings[ $context ][ $_key = md5( implode( ':', (array) $warning ) ) ] = $warning;
178
179
	}
180
181
	public function get_database_backup_filepath() {
182
		return $this->database_dump_filepath;
183
	}
184
185
	public function get_backup_filepath() {
186
		return $this->backup_filepath;
187
	}
188
189
}
190