Passed
Push — master ( da3a14...046d78 )
by Slye
01:27
created
src/System.php 1 patch
Indentation   +183 added lines, -183 removed lines patch added patch discarded remove patch
@@ -24,201 +24,201 @@
 block discarded – undo
24 24
  */
25 25
 class System
26 26
 {
27
-    /**
28
-     * @var Dotenv
29
-     */
30
-    protected Dotenv $env;
31
-    /**
32
-     * @var array
33
-     */
34
-    protected array $errors = [];
35
-    /**
36
-     * @var bool
37
-     */
38
-    protected bool $isCli;
39
-    /**
40
-     * @var array
41
-     */
42
-    protected array $files = [];
27
+	/**
28
+	 * @var Dotenv
29
+	 */
30
+	protected Dotenv $env;
31
+	/**
32
+	 * @var array
33
+	 */
34
+	protected array $errors = [];
35
+	/**
36
+	 * @var bool
37
+	 */
38
+	protected bool $isCli;
39
+	/**
40
+	 * @var array
41
+	 */
42
+	protected array $files = [];
43 43
 
44
-    /**
45
-     * System constructor.
46
-     */
47
-    public function __construct()
48
-    {
49
-        $this->isCli = PHP_SAPI === 'cli';
50
-        $this->loadConfigurationEnvironment();
51
-    }
44
+	/**
45
+	 * System constructor.
46
+	 */
47
+	public function __construct()
48
+	{
49
+		$this->isCli = PHP_SAPI === 'cli';
50
+		$this->loadConfigurationEnvironment();
51
+	}
52 52
 
53
-    /**
54
-     * Start System initialization
55
-     * @return void
56
-     * @throws RuntimeException
57
-     */
58
-    public function loadConfigurationEnvironment(): void
59
-    {
60
-        if (!file_exists(dirname(__DIR__) . DIRECTORY_SEPARATOR . '.env')) {
61
-            throw new RuntimeException('Please configure this script with .env-dist to .env file');
62
-        }
63
-        $this->env = Dotenv::createImmutable(dirname(__DIR__));
64
-        $this->env->load();
65
-        if (!$this->isCli && !(bool)$_ENV['ALLOW_EXECUTE_IN_HTTP_BROWSER']) {
66
-            die('Unauthorized to execute this script in your browser !');
67
-        }
68
-        if (PHP_VERSION_ID < 70400) {
69
-            die('PHP VERSION IS NOT SUPPORTED, PLEASE USE THIS SCRIPT WITH TO PHP 7.4 VERSION OR HIGHTER');
70
-        }
71
-    }
53
+	/**
54
+	 * Start System initialization
55
+	 * @return void
56
+	 * @throws RuntimeException
57
+	 */
58
+	public function loadConfigurationEnvironment(): void
59
+	{
60
+		if (!file_exists(dirname(__DIR__) . DIRECTORY_SEPARATOR . '.env')) {
61
+			throw new RuntimeException('Please configure this script with .env-dist to .env file');
62
+		}
63
+		$this->env = Dotenv::createImmutable(dirname(__DIR__));
64
+		$this->env->load();
65
+		if (!$this->isCli && !(bool)$_ENV['ALLOW_EXECUTE_IN_HTTP_BROWSER']) {
66
+			die('Unauthorized to execute this script in your browser !');
67
+		}
68
+		if (PHP_VERSION_ID < 70400) {
69
+			die('PHP VERSION IS NOT SUPPORTED, PLEASE USE THIS SCRIPT WITH TO PHP 7.4 VERSION OR HIGHTER');
70
+		}
71
+	}
72 72
 
73
-    /**
74
-     * @return Validator
75
-     */
76
-    public function checkRequirements(): Validator
77
-    {
78
-        return $this->env->required(
79
-            [
80
-                'DB_HOST',
81
-                'DB_USER',
82
-                'DB_PASSWORD',
83
-                'MAIL_FROM',
84
-                'MAIL_FROM_NAME',
85
-                'MAIL_TO',
86
-                'MAIL_TO_NAME',
87
-                'MAIL_SEND_ON_ERROR',
88
-                'MAIL_SEND_ON_SUCCESS',
89
-                'MAIL_SMTP_HOST',
90
-                'MAIL_SMTP_PORT',
91
-                'FILES_DAYS_HISTORY',
92
-                'FILES_PATH_TO_SAVE_BACKUP',
93
-                'ALLOW_EXECUTE_IN_HTTP_BROWSER'
94
-            ]
95
-        )->notEmpty();
96
-    }
73
+	/**
74
+	 * @return Validator
75
+	 */
76
+	public function checkRequirements(): Validator
77
+	{
78
+		return $this->env->required(
79
+			[
80
+				'DB_HOST',
81
+				'DB_USER',
82
+				'DB_PASSWORD',
83
+				'MAIL_FROM',
84
+				'MAIL_FROM_NAME',
85
+				'MAIL_TO',
86
+				'MAIL_TO_NAME',
87
+				'MAIL_SEND_ON_ERROR',
88
+				'MAIL_SEND_ON_SUCCESS',
89
+				'MAIL_SMTP_HOST',
90
+				'MAIL_SMTP_PORT',
91
+				'FILES_DAYS_HISTORY',
92
+				'FILES_PATH_TO_SAVE_BACKUP',
93
+				'ALLOW_EXECUTE_IN_HTTP_BROWSER'
94
+			]
95
+		)->notEmpty();
96
+	}
97 97
 
98
-    /**
99
-     * @return array
100
-     */
101
-    public function getExcludedDatabases(): array
102
-    {
103
-        if (empty(trim($_ENV['DB_EXCLUDE_DATABASES']))) {
104
-            return [];
105
-        }
106
-        return (array)$this->parseAndSanitize($_ENV['DB_EXCLUDE_DATABASES']);
107
-    }
98
+	/**
99
+	 * @return array
100
+	 */
101
+	public function getExcludedDatabases(): array
102
+	{
103
+		if (empty(trim($_ENV['DB_EXCLUDE_DATABASES']))) {
104
+			return [];
105
+		}
106
+		return (array)$this->parseAndSanitize($_ENV['DB_EXCLUDE_DATABASES']);
107
+	}
108 108
 
109
-    /**
110
-     * @return array
111
-     */
112
-    public function getDatabases(): array
113
-    {
114
-        $pdo = new PDO(
115
-            'mysql:host=' . $_ENV['DB_HOST'] . ';charset=UTF8', $_ENV['DB_USER'], $_ENV['DB_PASSWORD'],
116
-            [
117
-                PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ,
118
-                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
119
-            ]
120
-        );
121
-        return $pdo->query('SHOW DATABASES')->fetchAll();
122
-    }
109
+	/**
110
+	 * @return array
111
+	 */
112
+	public function getDatabases(): array
113
+	{
114
+		$pdo = new PDO(
115
+			'mysql:host=' . $_ENV['DB_HOST'] . ';charset=UTF8', $_ENV['DB_USER'], $_ENV['DB_PASSWORD'],
116
+			[
117
+				PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ,
118
+				PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
119
+			]
120
+		);
121
+		return $pdo->query('SHOW DATABASES')->fetchAll();
122
+	}
123 123
 
124
-    /**
125
-     * Process to backup databases
126
-     * @return bool
127
-     */
128
-    public function process(): bool
129
-    {
130
-        foreach ($this->getDatabases() as $database) {
131
-            if (!\in_array($database->Database, $this->getExcludedDatabases(), true)) {
132
-                $file_format = $database->Database . '-' . time() . '.sql';
133
-                try {
134
-                    $dumper = new Mysqldump(
135
-                        'mysql:host=' . $_ENV['DB_HOST'] . ';dbname=' . $database->Database . ';charset=UTF8',
136
-                        $_ENV['DB_USER'], $_ENV['DB_PASSWORD']
137
-                    );
138
-                    $dumper->start($_ENV['FILES_PATH_TO_SAVE_BACKUP'] . DIRECTORY_SEPARATOR . $file_format);
139
-                    $this->files[] = $_ENV['FILES_PATH_TO_SAVE_BACKUP'] . DIRECTORY_SEPARATOR . $file_format;
140
-                } catch (Exception $e) {
141
-                    $this->errors[] = [
142
-                        'dbname' => $database->Database,
143
-                        'error_message' => $e->getMessage(),
144
-                        'error_code' => $e->getCode()
145
-                    ];
146
-                    return false;
147
-                }
148
-            }
149
-        }
150
-        $this->sendMail();
151
-        return true;
152
-    }
124
+	/**
125
+	 * Process to backup databases
126
+	 * @return bool
127
+	 */
128
+	public function process(): bool
129
+	{
130
+		foreach ($this->getDatabases() as $database) {
131
+			if (!\in_array($database->Database, $this->getExcludedDatabases(), true)) {
132
+				$file_format = $database->Database . '-' . time() . '.sql';
133
+				try {
134
+					$dumper = new Mysqldump(
135
+						'mysql:host=' . $_ENV['DB_HOST'] . ';dbname=' . $database->Database . ';charset=UTF8',
136
+						$_ENV['DB_USER'], $_ENV['DB_PASSWORD']
137
+					);
138
+					$dumper->start($_ENV['FILES_PATH_TO_SAVE_BACKUP'] . DIRECTORY_SEPARATOR . $file_format);
139
+					$this->files[] = $_ENV['FILES_PATH_TO_SAVE_BACKUP'] . DIRECTORY_SEPARATOR . $file_format;
140
+				} catch (Exception $e) {
141
+					$this->errors[] = [
142
+						'dbname' => $database->Database,
143
+						'error_message' => $e->getMessage(),
144
+						'error_code' => $e->getCode()
145
+					];
146
+					return false;
147
+				}
148
+			}
149
+		}
150
+		$this->sendMail();
151
+		return true;
152
+	}
153 153
 
154
-    /**
155
-     * @param string $data
156
-     * @return array|string
157
-     */
158
-    private function parseAndSanitize(string $data)
159
-    {
160
-        $results = explode(',', $data);
161
-        if (\count($results) > 1) {
162
-            foreach ($results as $k => $v) {
163
-                $results[$k] = trim($v);
164
-                if (empty($v)) {
165
-                    unset($results[$k]);
166
-                }
167
-            }
168
-            return $results;
169
-        }
170
-        return trim($results[0]);
171
-    }
154
+	/**
155
+	 * @param string $data
156
+	 * @return array|string
157
+	 */
158
+	private function parseAndSanitize(string $data)
159
+	{
160
+		$results = explode(',', $data);
161
+		if (\count($results) > 1) {
162
+			foreach ($results as $k => $v) {
163
+				$results[$k] = trim($v);
164
+				if (empty($v)) {
165
+					unset($results[$k]);
166
+				}
167
+			}
168
+			return $results;
169
+		}
170
+		return trim($results[0]);
171
+	}
172 172
 
173
-    /**
174
-     * Send a mail if error or success backup database
175
-     */
176
-    private function sendMail(): void
177
-    {
178
-        $smtpTransport = new Swift_SmtpTransport($_ENV['MAIL_SMTP_HOST'], (int)$_ENV['MAIL_SMTP_PORT']);
179
-        $smtpTransport->setUsername($_ENV['MAIL_SMTP_USER'])->setPassword($_ENV['MAIL_SMTP_PASSWORD']);
180
-        $mailer = new Swift_Mailer($smtpTransport);
181
-        if (empty($this->errors)) {
182
-            if ((bool)$_ENV['MAIL_SEND_ON_SUCCESS']) {
183
-                $body = "<strong>The backup of the databases has been successful!</strong>";
184
-                if ((bool)$_ENV['MAIL_SEND_BACKUP_FILE']) {
185
-                    $body .= "<br><br>You will find a copy of the backup attached to this email.";
186
-                }
187
-                $message = (new Swift_Message('Backup performed!'))
188
-                    ->setFrom($_ENV['MAIL_FROM'], $_ENV['MAIL_FROM_NAME'])
189
-                    ->setTo($_ENV['MAIL_TO'], $_ENV['MAIL_TO_NAME'])
190
-                    ->setBody($body)
191
-                    ->setCharset('utf-8')
192
-                    ->setContentType('text/html');
193
-                if ((bool)$_ENV['MAIL_SEND_BACKUP_FILE']) {
194
-                    foreach ($this->files as $file) {
195
-                        $attachment = Swift_Attachment::fromPath($file)->setContentType('application/sql');
196
-                        $message->attach($attachment);
197
-                    }
198
-                }
199
-                $mailer->send($message);
200
-            }
201
-        } else {
202
-            if ((bool)$_ENV['MAIL_SEND_ON_ERROR']) {
203
-                $body = "<strong>The backup of databases has encountered errors: </strong><br><br><ul>";
204
-                foreach ($this->errors as $error) {
205
-                    $body .= "<li>
173
+	/**
174
+	 * Send a mail if error or success backup database
175
+	 */
176
+	private function sendMail(): void
177
+	{
178
+		$smtpTransport = new Swift_SmtpTransport($_ENV['MAIL_SMTP_HOST'], (int)$_ENV['MAIL_SMTP_PORT']);
179
+		$smtpTransport->setUsername($_ENV['MAIL_SMTP_USER'])->setPassword($_ENV['MAIL_SMTP_PASSWORD']);
180
+		$mailer = new Swift_Mailer($smtpTransport);
181
+		if (empty($this->errors)) {
182
+			if ((bool)$_ENV['MAIL_SEND_ON_SUCCESS']) {
183
+				$body = "<strong>The backup of the databases has been successful!</strong>";
184
+				if ((bool)$_ENV['MAIL_SEND_BACKUP_FILE']) {
185
+					$body .= "<br><br>You will find a copy of the backup attached to this email.";
186
+				}
187
+				$message = (new Swift_Message('Backup performed!'))
188
+					->setFrom($_ENV['MAIL_FROM'], $_ENV['MAIL_FROM_NAME'])
189
+					->setTo($_ENV['MAIL_TO'], $_ENV['MAIL_TO_NAME'])
190
+					->setBody($body)
191
+					->setCharset('utf-8')
192
+					->setContentType('text/html');
193
+				if ((bool)$_ENV['MAIL_SEND_BACKUP_FILE']) {
194
+					foreach ($this->files as $file) {
195
+						$attachment = Swift_Attachment::fromPath($file)->setContentType('application/sql');
196
+						$message->attach($attachment);
197
+					}
198
+				}
199
+				$mailer->send($message);
200
+			}
201
+		} else {
202
+			if ((bool)$_ENV['MAIL_SEND_ON_ERROR']) {
203
+				$body = "<strong>The backup of databases has encountered errors: </strong><br><br><ul>";
204
+				foreach ($this->errors as $error) {
205
+					$body .= "<li>
206 206
                             <ul>
207 207
                                 <li>Database: {$error['dbname']}</li>
208 208
                                 <li>Error code: {$error['error_code']}</li>
209 209
                                 <li>Error message: {$error['error_message']}</li>
210 210
                             </ul>
211 211
                            </li>";
212
-                }
213
-                $body .= '</ul>';
214
-                $message = (new Swift_Message('Backup failed!'))
215
-                    ->setFrom($_ENV['MAIL_FROM'], $_ENV['MAIL_FROM_NAME'])
216
-                    ->setTo($_ENV['MAIL_TO'], $_ENV['MAIL_TO_NAME'])
217
-                    ->setBody($body)
218
-                    ->setCharset('utf-8')
219
-                    ->setContentType('text/html');
220
-                $mailer->send($message);
221
-            }
222
-        }
223
-    }
212
+				}
213
+				$body .= '</ul>';
214
+				$message = (new Swift_Message('Backup failed!'))
215
+					->setFrom($_ENV['MAIL_FROM'], $_ENV['MAIL_FROM_NAME'])
216
+					->setTo($_ENV['MAIL_TO'], $_ENV['MAIL_TO_NAME'])
217
+					->setBody($body)
218
+					->setCharset('utf-8')
219
+					->setContentType('text/html');
220
+				$mailer->send($message);
221
+			}
222
+		}
223
+	}
224 224
 }
Please login to merge, or discard this patch.
src/FileManager.php 1 patch
Indentation   +51 added lines, -51 removed lines patch added patch discarded remove patch
@@ -10,58 +10,58 @@
 block discarded – undo
10 10
  */
11 11
 class FileManager
12 12
 {
13
-    /**
14
-     * @var array
15
-     */
16
-    private array $params;
17
-    /**
18
-     * @var array
19
-     */
20
-    private array $files = [];
13
+	/**
14
+	 * @var array
15
+	 */
16
+	private array $params;
17
+	/**
18
+	 * @var array
19
+	 */
20
+	private array $files = [];
21 21
 
22
-    /**
23
-     * FileManager constructor.
24
-     */
25
-    public function __construct()
26
-    {
27
-        (new System())->loadConfigurationEnvironment();
28
-        $this->params = [
29
-            'files_path' => $_ENV['FILES_PATH_TO_SAVE_BACKUP'],
30
-            'days_interval' => (int)$_ENV['FILES_DAYS_HISTORY']
31
-        ];
32
-        $this->_getFiles();
33
-    }
22
+	/**
23
+	 * FileManager constructor.
24
+	 */
25
+	public function __construct()
26
+	{
27
+		(new System())->loadConfigurationEnvironment();
28
+		$this->params = [
29
+			'files_path' => $_ENV['FILES_PATH_TO_SAVE_BACKUP'],
30
+			'days_interval' => (int)$_ENV['FILES_DAYS_HISTORY']
31
+		];
32
+		$this->_getFiles();
33
+	}
34 34
 
35
-    /**
36
-     * @return void
37
-     */
38
-    private function _getFiles(): void
39
-    {
40
-        $files = scandir($this->params['files_path'], SCANDIR_SORT_ASCENDING);
41
-        $this->files = array_filter(
42
-            $files,
43
-            function ($k) {
44
-                $return = [];
45
-                if ($k !== '..' && $k !== '.') {
46
-                    $return[] = $k;
47
-                }
48
-                return $return;
49
-            }
50
-        );
51
-    }
35
+	/**
36
+	 * @return void
37
+	 */
38
+	private function _getFiles(): void
39
+	{
40
+		$files = scandir($this->params['files_path'], SCANDIR_SORT_ASCENDING);
41
+		$this->files = array_filter(
42
+			$files,
43
+			function ($k) {
44
+				$return = [];
45
+				if ($k !== '..' && $k !== '.') {
46
+					$return[] = $k;
47
+				}
48
+				return $return;
49
+			}
50
+		);
51
+	}
52 52
 
53
-    /**
54
-     * @return void
55
-     */
56
-    public function removeOldFilesByIntervalDays(): void
57
-    {
58
-        $time_before = time() - $this->params['days_interval'] * 86400;
59
-        foreach ($this->files as $file) {
60
-            $f = explode('-', $file);
61
-            $f = str_replace('.sql', '', $f);
62
-            if ($f[1] < $time_before) {
63
-                @unlink($this->params['files_path'] . '/' . $file);
64
-            }
65
-        }
66
-    }
53
+	/**
54
+	 * @return void
55
+	 */
56
+	public function removeOldFilesByIntervalDays(): void
57
+	{
58
+		$time_before = time() - $this->params['days_interval'] * 86400;
59
+		foreach ($this->files as $file) {
60
+			$f = explode('-', $file);
61
+			$f = str_replace('.sql', '', $f);
62
+			if ($f[1] < $time_before) {
63
+				@unlink($this->params['files_path'] . '/' . $file);
64
+			}
65
+		}
66
+	}
67 67
 }
Please login to merge, or discard this patch.