Skip to content

Commit 5a22edd

Browse files
authored
Merge 1e80ab3 into a4c0091
2 parents a4c0091 + 1e80ab3 commit 5a22edd

File tree

10 files changed

+56
-44
lines changed

10 files changed

+56
-44
lines changed

config/db-backup.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

33
return [
4-
'backup_folder' => storage_path('db-backups'),
4+
'backup_folder' => storage_path('db-backups'),
55
'max_threshold_archive' => 5,
6-
'max_days_archived' => 30,
7-
'garbage_collector' => true
6+
'max_days_archived' => 30,
7+
'garbage_collector' => true,
88
];

src/Commands/Backup.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,13 @@ public function handle(): int
3535

3636
if (false === $this->database->isDatabaseSupported()) {
3737
$this->error(sprintf('The current selected %s database driver is not (yet) supported.', $this->database->getRealDatabase()->getDatabaseIdentifier()));
38+
3839
return 1;
3940
}
4041

4142
if (false === $this->storage->initializeStorageFolder()) {
4243
$this->error('Error in the back-up directory. Please see the error log for further details.');
44+
4345
return 1;
4446
}
4547

src/Commands/BaseCommand.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ class BaseCommand extends Command
1313

1414
/**
1515
* BaseCommand constructor.
16-
* @param Storage $storage
16+
*
17+
* @param Storage $storage
1718
* @param Database $database
1819
*/
1920
public function __construct(Storage $storage, Database $database)

src/Commands/Restore.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public function handle(): int
3636

3737
if (false === $this->storage->initializeStorageFolder()) {
3838
$this->error('Error in the back-up directory. Please see the error log for further details.');
39+
3940
return 1;
4041
}
4142

@@ -87,6 +88,4 @@ public function handle(): int
8788

8889
return 0;
8990
}
90-
91-
9291
}

src/Databases/Database.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class Database
3232
public function __construct()
3333
{
3434
$this->database = Config::get('database.default');
35-
$this->realDatabase = Config::get('database.connections.' . $this->database);
35+
$this->realDatabase = Config::get('database.connections.'.$this->database);
3636
$this->processHandler = new ProcessHandler();
3737
}
3838

@@ -48,7 +48,6 @@ public function buildDatabaseClass(): void
4848
}
4949
}
5050

51-
5251
/**
5352
* @return bool
5453
*/

src/Databases/MySQLDatabase.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ class MySQLDatabase implements DatabaseInterface
1919
/**
2020
* MySQLDatabase constructor.
2121
*
22-
* @param string $database
23-
* @param string $user
24-
* @param string $password
25-
* @param string $host
26-
* @param string $port
22+
* @param string $database
23+
* @param string $user
24+
* @param string $password
25+
* @param string $host
26+
* @param string $port
2727
* @param ProcessHandler $processHandler
2828
*/
2929
public function __construct(string $database, string $user, string $password, string $host, string $port, ProcessHandler $processHandler)
@@ -42,8 +42,8 @@ public function __construct(string $database, string $user, string $password, st
4242
}
4343

4444
/**
45-
*
4645
* @param string $backupFilename
46+
*
4747
* @return bool
4848
*/
4949
public function backup(string $backupFilename): bool
@@ -88,14 +88,15 @@ protected function getCredentials(): string
8888

8989
/**
9090
* @param string $backupFile
91+
*
9192
* @return bool
9293
*/
9394
public function restore(string $backupFile): bool
9495
{
9596
Log::debug('Starting MySQL import procedure.');
9697

9798
$startTimeImport = microtime(true);
98-
$backupFile = '"' . addcslashes($backupFile, '\\"') . '"';
99+
$backupFile = '"'.addcslashes($backupFile, '\\"').'"';
99100
$command = sprintf('mysql %s %s < %s', $this->getCredentials(), $this->database, $backupFile);
100101

101102
if (false === $this->processHandler->run($command)) {

src/Databases/Storage.php

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function sanitizeStoragePath(): void
2222
$configStoragePath = Config::get('db-backup.backup_folder');
2323
if (substr($configStoragePath, -1, 1) !== DIRECTORY_SEPARATOR) {
2424
Log::debug('Stored back-up folder is not probably set. Fixing.');
25-
$configStoragePath = $configStoragePath . DIRECTORY_SEPARATOR;
25+
$configStoragePath = $configStoragePath.DIRECTORY_SEPARATOR;
2626
}
2727

2828
$this->storagePath = $configStoragePath;
@@ -39,6 +39,7 @@ public function initializeStorageFolder(): bool
3939
Log::debug('Storage path does not exist. Attempting to create.');
4040
if (false === File::makeDirectory($this->storagePath)) {
4141
Log::error('Unable to create create storage path.');
42+
4243
return false;
4344
}
4445
Log::debug('Storage path successfully created.');
@@ -51,6 +52,7 @@ public function initializeStorageFolder(): bool
5152

5253
/**
5354
* @param string $backupFilePath
55+
*
5456
* @return bool
5557
*/
5658
public function createArchiveFile(string $backupFilePath): bool
@@ -61,24 +63,25 @@ public function createArchiveFile(string $backupFilePath): bool
6163
}
6264

6365
Log::debug('Finished creating an archive file.');
66+
6467
return true;
6568
}
6669

67-
6870
/**
6971
* @param string $databaseIdentifier
7072
* @param string $databaseFileExtension
73+
*
7174
* @return string
7275
*/
7376
public function generateBackupFilename(string $databaseIdentifier, string $databaseFileExtension): string
7477
{
75-
return $this->backupFilename = $this->storagePath . $databaseIdentifier . '-' . time() . '.' . $databaseFileExtension;
78+
return $this->backupFilename = $this->storagePath.$databaseIdentifier.'-'.time().'.'.$databaseFileExtension;
7679
}
7780

7881
/**
79-
* @param string $backupFile
80-
*
82+
* @param string $backupFile
8183
* @param Database $database
84+
*
8285
* @return string|string[]|null
8386
*/
8487
public function decompressBackupFile(string $backupFile, Database $database)
@@ -93,10 +96,10 @@ public function decompressBackupFile(string $backupFile, Database $database)
9396
$process = new Process(['gzip', '-d', $workableFile]);
9497
$process->run(function ($type, $buffer): bool {
9598
if (Process::OUT === $type) {
96-
Log::debug('gzip buffer: ' . $buffer);
99+
Log::debug('gzip buffer: '.$buffer);
97100
}
98101
if (Process::ERR === $type) {
99-
Log::error('Error whilst performing zip action. Output of buffer: ' . $buffer);
102+
Log::error('Error whilst performing zip action. Output of buffer: '.$buffer);
100103

101104
return false;
102105
}
@@ -111,6 +114,7 @@ public function decompressBackupFile(string $backupFile, Database $database)
111114

112115
/**
113116
* @param string $databaseIdentifier
117+
*
114118
* @return array|null
115119
*/
116120
public function getMostRecentBackups(string $databaseIdentifier): ?array
@@ -140,20 +144,21 @@ public function getMostRecentBackups(string $databaseIdentifier): ?array
140144
}
141145

142146
/**
143-
* @param string $backupFile
147+
* @param string $backupFile
144148
* @param Database $database
149+
*
145150
* @return string|null
146151
*/
147152
protected function createTmpFile(string $backupFile, Database $database): ?string
148153
{
149154
Log::debug('Creating temp back-up file to not corrupt the archives.');
150-
$tmpFilename = 'tmp.' . microtime(true) . '.' . $database->getRealDatabase()->getFileExtension() . '.gz';
151-
$filePath = $this->storagePath . $tmpFilename;
155+
$tmpFilename = 'tmp.'.microtime(true).'.'.$database->getRealDatabase()->getFileExtension().'.gz';
156+
$filePath = $this->storagePath.$tmpFilename;
152157

153158
try {
154159
File::copy($backupFile, $filePath);
155160
} catch (Exception $e) {
156-
Log::error('Could not create temporary archive file. Exception throw: ' . $e->getMessage());
161+
Log::error('Could not create temporary archive file. Exception throw: '.$e->getMessage());
157162

158163
return null;
159164
}

src/ProcessHandler.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ class ProcessHandler
99
{
1010
/**
1111
* @param string $command
12-
* @return boolean
12+
*
13+
* @return bool
1314
*/
1415
public function run(string $command): bool
1516
{
@@ -18,6 +19,7 @@ public function run(string $command): bool
1819

1920
if ($process->getExitCode() !== 0) {
2021
Log::error(sprintf('Failure in the processor. Please verify if the command is recognized. Exit code text returned: "%s". Error output: %s', $process->getExitCodeText(), $process->getErrorOutput()));
22+
2123
return false;
2224
}
2325

@@ -26,6 +28,7 @@ public function run(string $command): bool
2628

2729
/**
2830
* @param array $command
31+
*
2932
* @return bool
3033
*/
3134
public static function runArray(array $command): bool
@@ -35,6 +38,7 @@ public static function runArray(array $command): bool
3538

3639
if ($process->getExitCode() !== 0) {
3740
Log::error(sprintf('Failure in the processor. Please verify if the command is recognized. Exit code text returned: "%s". Error output: %s', $process->getExitCodeText(), $process->getErrorOutput()));
41+
3842
return false;
3943
}
4044

tests/BaseTestCase.php

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function setUp(): void
1717
public function getPackageProviders($app)
1818
{
1919
return [
20-
LaravelDBBackupProvider::class
20+
LaravelDBBackupProvider::class,
2121
];
2222
}
2323

@@ -30,14 +30,15 @@ public function getEnvironmentSetUp($app)
3030
* @param $object
3131
* @param $property
3232
* @param $value
33+
*
3334
* @throws ReflectionException
3435
*/
3536
public function setProtectedProperty($object, $property, $value): void
3637
{
3738
try {
3839
$reflection = new ReflectionClass($object);
3940
} catch (ReflectionException $e) {
40-
throw new ReflectionException("Failed setting Protected property. Exception thrown: " . $e->getMessage());
41+
throw new ReflectionException('Failed setting Protected property. Exception thrown: '.$e->getMessage());
4142
}
4243

4344
$reflection_property = $reflection->getProperty($property);
@@ -48,14 +49,15 @@ public function setProtectedProperty($object, $property, $value): void
4849
/**
4950
* @param $object
5051
* @param $property
52+
*
5153
* @throws ReflectionException
5254
*/
5355
public function getProtectedProperty($object, $property): void
5456
{
5557
try {
5658
$reflection = new ReflectionClass($object);
5759
} catch (ReflectionException $e) {
58-
throw new ReflectionException("Failed getting Protected property. Exception thrown: " . $e->getMessage());
60+
throw new ReflectionException('Failed getting Protected property. Exception thrown: '.$e->getMessage());
5961
}
6062

6163
$reflection_property = $reflection->getProperty($property);
@@ -68,20 +70,20 @@ protected function useMySqlConnection($app)
6870
$app->config->set('database.default', 'mysql');
6971

7072
$app->config->set('database.connections.mysql', [
71-
'driver' => 'mysql',
72-
'url' => env('DATABASE_URL'),
73-
'host' => env('DB_HOST', '127.0.0.1'),
74-
'port' => env('DB_PORT', '3306'),
75-
'database' => env('DB_DATABASE', 'forge'),
76-
'username' => env('DB_USERNAME', 'forge'),
77-
'password' => env('DB_PASSWORD', ''),
78-
'unix_socket' => env('DB_SOCKET', ''),
79-
'charset' => 'utf8mb4',
80-
'collation' => 'utf8mb4_unicode_ci',
81-
'prefix' => '',
73+
'driver' => 'mysql',
74+
'url' => env('DATABASE_URL'),
75+
'host' => env('DB_HOST', '127.0.0.1'),
76+
'port' => env('DB_PORT', '3306'),
77+
'database' => env('DB_DATABASE', 'forge'),
78+
'username' => env('DB_USERNAME', 'forge'),
79+
'password' => env('DB_PASSWORD', ''),
80+
'unix_socket' => env('DB_SOCKET', ''),
81+
'charset' => 'utf8mb4',
82+
'collation' => 'utf8mb4_unicode_ci',
83+
'prefix' => '',
8284
'prefix_indexes' => true,
83-
'strict' => true,
84-
'engine' => null
85+
'strict' => true,
86+
'engine' => null,
8587
]);
8688
}
8789

tests/Databases/MySQLDatabaseTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace EDouna\LaravelDBBackup\Test\Databases;
44

5-
use Config;
65
use EDouna\LaravelDBBackup\Databases\MySQLDatabase;
76
use Mockery as m;
87
use Orchestra\Testbench\TestCase;

0 commit comments

Comments
 (0)