MySQLDatabase::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
nc 1
nop 6
dl 0
loc 13
rs 9.9666
c 1
b 0
f 0
1
<?php
2
3
namespace EDouna\LaravelDBBackup\Databases;
4
5
use EDouna\LaravelDBBackup\ProcessHandler;
6
use Illuminate\Support\Facades\Log;
7
8
class MySQLDatabase implements DatabaseInterface
9
{
10
    protected $database;
11
    protected $user;
12
    protected $password;
13
    protected $host;
14
    protected $port;
15
    protected $fileExtension;
16
    protected $databaseIdentifier;
17
    protected $processHandler;
18
19
    /**
20
     * MySQLDatabase constructor.
21
     *
22
     * @param string         $database
23
     * @param string         $user
24
     * @param string         $password
25
     * @param string         $host
26
     * @param string         $port
27
     * @param ProcessHandler $processHandler
28
     */
29
    public function __construct(string $database, string $user, string $password, string $host, string $port, ProcessHandler $processHandler)
30
    {
31
        Log::debug('Constructing MySQL database class.');
32
        $this->database = $database;
33
        $this->user = $user;
34
        $this->password = $password;
35
        $this->host = $host;
36
        $this->port = $port;
37
38
        $this->fileExtension = 'sql';
39
        $this->databaseIdentifier = 'mysql';
40
41
        $this->processHandler = $processHandler;
42
    }
43
44
    /**
45
     * @param string $backupFilename
46
     *
47
     * @return bool
48
     */
49
    public function backup(string $backupFilename): bool
50
    {
51
        Log::debug('Start creating MySQL dump file.');
52
        $command = sprintf('mysqldump %s --skip-comments %s > %s', $this->getCredentials(), $this->database, $backupFilename);
53
54
        if (false === $this->processHandler->run($command)) {
55
            return false;
56
        }
57
58
        Log::debug('Finished running MySQL dump.');
59
60
        return true;
61
    }
62
63
    /**
64
     * @return string
65
     */
66
    public function getDatabaseIdentifier(): string
67
    {
68
        return $this->databaseIdentifier;
69
    }
70
71
    /**
72
     * @return string
73
     */
74
    public function getFileExtension(): string
75
    {
76
        return $this->fileExtension;
77
    }
78
79
    /**
80
     * Dedicated string to be used when performing MySQL commands.
81
     *
82
     * @return string
83
     */
84
    protected function getCredentials(): string
85
    {
86
        return sprintf('--user=%s --password=%s --host=%s --port=%s', $this->user, $this->password, $this->host, $this->port);
87
    }
88
89
    /**
90
     * @param string $backupFile
91
     *
92
     * @return bool
93
     */
94
    public function restore(string $backupFile): bool
95
    {
96
        Log::debug('Starting MySQL import procedure.');
97
98
        $startTimeImport = microtime(true);
99
        $backupFile = '"'.addcslashes($backupFile, '\\"').'"';
100
        $command = sprintf('mysql %s %s < %s', $this->getCredentials(), $this->database, $backupFile);
101
102
        if (false === $this->processHandler->run($command)) {
103
            return false;
104
        }
105
106
        $endTimeImport = round(microtime(true) - $startTimeImport, 2);
107
        Log::debug(sprintf('Import successfully run in %s second(s).', $endTimeImport));
108
109
        return true;
110
    }
111
}
112