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:28
created

Database_Backup_Engine::verify_backup()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 20
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 20
rs 8.8571
cc 5
eloc 8
nc 8
nop 0
1
<?php
2
3
namespace HM\BackUpWordPress;
4
5
/**
6
 * The Database Backup Engine type
7
 *
8
 * All Database Backup Engine implementations should extend this class
9
 */
10
abstract class Database_Backup_Engine extends Backup_Engine {
11
12
	/**
13
	 * The filename for the resulting Backup
14
	 *
15
	 * @var string
16
	 */
17
	public $backup_filename = '';
18
19
	/**
20
	 * The database host string, typically the value of
21
	 * the `DB_HOST` Constant.
22
	 *
23
	 * @var string
24
	 */
25
	private $host = '';
26
27
	/**
28
	 * The database socket, if it's using a socket connection
29
	 *
30
	 * @var string
31
	 */
32
	private $socket = '';
33
34
	/**
35
	 * The database port, if a custom one is set
36
	 *
37
	 * @var integer
38
	 */
39
	private $port = 0;
40
41
	/**
42
	 * Individual Database Backup Engine implementations must include
43
	 * a backup method at a minimum.
44
	 *
45
	 * @return [type] [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
46
	 */
47
	abstract public function backup();
48
49
	/**
50
	 * Setup some general database backup settings
51
	 *
52
	 * Child classes must call `parent::__construct` in their own constructor.
53
	 */
54
	public function __construct() {
55
56
		parent::__construct();
57
58
		$this->parse_db_host_constant();
59
60
		// Set a default backup filename
61
		$this->set_backup_filename( 'database-' . $this->get_name() . '.sql' );
62
63
	}
64
65
	/**
66
	 * Get the database charset setting.
67
	 *
68
	 * @return [string The database charset.
0 ignored issues
show
Documentation introduced by
The doc-type string">[string could not be parsed: Unknown type name "[" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
69
	 */
70
	public function get_charset() {
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...
71
		global $wpdb;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
72
		return $wpdb->charset;
73
	}
74
75
	/**
76
	 * Get the database collate setting.
77
	 *
78
	 * @return string The database collage setting.
79
	 */
80
	public function get_collate() {
81
		global $wpdb;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
82
		return $wpdb->collate;
83
	}
84
85
	/**
86
	 * Get the database name.
87
	 *
88
	 * @return string The database name.
89
	 */
90
	public function get_name() {
91
		global $wpdb;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
92
		return $wpdb->dbname;
93
	}
94
95
	/**
96
	 * Get the database user.
97
	 *
98
	 * @return string The database user.
99
	 */
100
	public function get_user() {
101
		global $wpdb;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
102
		return $wpdb->dbuser;
103
	}
104
105
	/**
106
	 * Get the database password.
107
	 *
108
	 * @return string The database password.
109
	 */
110
	public function get_password() {
111
		global $wpdb;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
112
		return $wpdb->dbpassword;
113
	}
114
115
	/**
116
	 * Get the database hostname.
117
	 *
118
	 * @return string The database hostname.
119
	 */
120
	public function get_host() {
121
		return $this->host;
122
	}
123
124
	/**
125
	 * Get the database port.
126
	 *
127
	 * @return int The database port.
128
	 */
129
	public function get_port() {
130
		return $this->port;
131
	}
132
133
	/**
134
	 * Get the database socket.
135
	 *
136
	 * @return string The database socket.
137
	 */
138
	public function get_socket() {
139
		return $this->socket;
140
	}
141
142
	/**
143
	 * Parse the `DB_HOST` constant.
144
	 *
145
	 * The `DB_HOST` constant potentially contains the hostname, port or socket.
146
	 * We need to parse it to figure out the type of mysql connection to make.
147
	 *
148
	 * @param  string $constant The Constant to parse. If the string isn't a
149
	 *                          defined Constant then it will be parsed directly.
150
	 */
151
	public function parse_db_host_constant( $constant = 'DB_HOST' ) {
152
153
		// If we've been passed a Constant then grab it's contents
154
		if ( defined( $constant ) ) {
155
			$constant = constant( $constant );
156
		}
157
158
		// If we weren't passed a Constant then just parse the string directly.
159
		$this->host = (string) $constant;
160
161
		// Grab the part after :, it could either be a port or a socket
162
		$port_or_socket = strstr( $constant, ':' );
163
164
		if ( $port_or_socket ) {
165
166
			// The host is the bit up to the :
167
			$this->host = substr( $constant, 0, strpos( $constant, ':' ) );
168
169
			// Strip the :
170
			$port_or_socket = substr( $port_or_socket, 1 );
171
172
			if ( 0 !== strpos( $port_or_socket, '/' ) ) {
173
174
				$this->port = intval( $port_or_socket );
175
				$maybe_socket = strstr( $port_or_socket, ':' );
176
177
				if ( ! empty( $maybe_socket ) ) {
178
					$this->socket = substr( $maybe_socket, 1 );
179
				}
180
181
			} else {
182
				$this->socket = $port_or_socket;
183
			}
184
185
		}
186
187
	}
188
189
	/**
190
	 * Verify that the database backup was successful.
191
	 *
192
	 * It's important this function is performant as it's called after every
193
	 * backup.
194
	 *
195
	 * @return bool Whether the backup completed successfully
196
	 */
197
	public function verify_backup() {
198
199
		// If there are errors delete the database dump file
200
		if ( $this->get_errors( __CLASS__ ) && file_exists( $this->get_backup_filepath() ) ) {
201
			unlink( $this->get_backup_filepath() );
202
		}
203
204
		// If we have an empty file delete it
205
		if ( @filesize( $this->get_backup_filepath() ) === 0 ) {
206
			unlink( $this->get_backup_filepath() );
207
		}
208
209
		// If the database backup doesn't exist then the backup must have failed
210
		if ( ! file_exists( $this->get_backup_filepath() ) ) {
1 ignored issue
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return file_exists($this->get_backup_filepath());.
Loading history...
211
			return false;
212
		}
213
214
		return true;
215
216
	}
217
218
}
219