Completed
Push — master ( ae1aa8...20d020 )
by CodexShaper
02:07
created

MysqlDumper::getRestoreCommand()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace CodexShaper\Dumper\Drivers;
4
5
use CodexShaper\Dumper\Dumper;
6
use Symfony\Component\Process\Exception\ProcessFailedException;
7
8
class MysqlDumper extends Dumper
9
{
10
    /*@var bool*/
11
    protected $singleTransaction = false;
12
    /*@var bool*/
13
    protected $skipLockTables = false;
14
    /*@var bool*/
15
    protected $quick = false;
16
    /*@var bool*/
17
    protected $skipComments = true;
18
    /*@var string*/
19
    protected $defaultCharacterSet = '';
20
    /*@var bool*/
21
    protected $createTables = true;
22
23 2
    public function useSingleTransaction()
24
    {
25 2
        $this->singleTransaction = true;
26 2
        return $this;
27
    }
28 2
    public function useSkipLockTables()
29
    {
30 2
        $this->skipLockTables = true;
31 2
        return $this;
32
    }
33 2
    public function useQuick()
34
    {
35 2
        $this->quick = true;
36 2
        return $this;
37
    }
38 2
    public function doNotUseSkipComments()
39
    {
40 2
        $this->skipComments = false;
41 2
        return $this;
42
    }
43 2
    public function doNotCreateTables()
44
    {
45 2
        $this->createTables = false;
46 2
        return $this;
47
    }
48
    public function setDefaultCharacterSet(string $charecterSet)
49
    {
50
        $this->defaultCharacterSet = $charecterSet;
51
        return $this;
52
    }
53
54
    public function dump(string $destinationPath = "")
55
    {
56
        $destinationPath = !empty($destinationPath) ? $destinationPath : $this->destinationPath;
57
        $this->runCommand($destinationPath, "dump");
58
        return $this;
59
    }
60
61
    public function restore(string $restorePath = "")
62
    {
63
        $restorePath = !empty($restorePath) ? $restorePath : $this->restorePath;
64
        $this->runCommand($restorePath, 'restore');
65
        return $this;
66
    }
67
68 28
    public function getDumpCommand(string $credentialFile = '', $destinationPath = '')
69
    {
70 28
        $destinationPath = !empty($destinationPath) ? $destinationPath : $this->destinationPath;
71 28
        $dumpCommand     = $this->prepareDumpCommand($credentialFile, $destinationPath);
72
73 28
        return $this->removeExtraSpaces($dumpCommand);
74
    }
75
76 6
    public function getRestoreCommand(string $credentialFile = '', string $filePath = '')
77
    {
78 6
        $filePath       = !empty($filePath) ? '"' . $filePath : $this->restorePath;
79 6
        $restoreCommand = $this->prepareRestoreCommand($credentialFile, $filePath);
80
81 6
        return $this->removeExtraSpaces($restoreCommand);
82
    }
83
84 28
    protected function prepareDumpCommand(string $credentialFile, string $destinationPath): string
85
    {
86 28
        $dumpCommand = sprintf(
87 28
            '%s %s %s %s %s %s %s %s %s %s %s %s',
88 28
            $this->quoteCommand("{$this->commandBinaryPath}mysqldump"),
89 28
            $this->prepareAuthentication($credentialFile),
90 28
            $this->prepareSocket(),
91 28
            $this->prepareSkipComments(),
92 28
            $this->prepareCreateTables(),
93 28
            $this->prepareSingleTransaction(),
94 28
            $this->prepareSkipLockTables(),
95 28
            $this->prepareQuick(),
96 28
            $this->prepareDefaultCharSet(),
97 28
            $this->prepareDatabase(),
98 28
            $this->prepareIncludeTables(),
99 28
            $this->prepareIgnoreTables()
100
        );
101
102 28
        if ($this->isCompress) {
103 2
            $compressCommand = $this->quoteCommand("{$this->compressBinaryPath}{$this->compressCommand}");
104 2
            return "{$dumpCommand} | {$compressCommand} > \"{$destinationPath}{$this->compressExtension}\"";
105
        }
106
107 26
        return "{$dumpCommand} > \"{$destinationPath}\"";
108
    }
109
110 6
    protected function prepareRestoreCommand(string $credentialFile, string $filePath): string
111
    {
112 6
        $restoreCommand = sprintf("%s %s %s",
113 6
            $this->quoteCommand("{$this->commandBinaryPath}mysql"),
114 6
            $this->prepareAuthentication($credentialFile),
115 6
            $this->prepareDatabase()
116
        );
117
118 6
        if ($this->isCompress) {
119
120 2
            $compressCommand = $this->quoteCommand("{$this->compressBinaryPath}{$this->compressCommand}");
121
122 2
            return "{$compressCommand} < \"{$filePath}\" | {$restoreCommand}";
123
        }
124
125 4
        return "{$restoreCommand} < \"{$filePath}\"";
126
    }
127
128
    protected function runCommand($filePath, $action)
129
    {
130
        try {
131
132
            $credentials = $this->getCredentials();
133
            $tempFile    = tempnam(sys_get_temp_dir(), 'mysqlpass');
134
            $handler     = fopen($tempFile, 'r+');
135
            if ($handler !== false) {
136
                fwrite($handler, $credentials);
137
138
                if ($action == 'dump') {
139
                    $dumpCommand   = $this->prepareDumpCommand($tempFile, $filePath);
140
                    $this->command = $this->removeExtraSpaces($dumpCommand);
141
                } else if ($action == 'restore') {
142
                    $dumpCommand   = $this->prepareRestoreCommand($tempFile, $filePath);
143
                    $this->command = $this->removeExtraSpaces($dumpCommand);
144
                }
145
146
                $process = $this->prepareProcessCommand();
147
148
                if ($this->debug) {
149
                    $process->mustRun();
150
                } else {
151
                    $process->run();
152
                }
153
154
                fclose($handler);
155
                unlink($tempFile);
156
            }
157
158
        } catch (ProcessFailedException $e) {
159
            throw new \Exception($e->getMessage());
160
161
        }
162
    }
163
164
    protected function getCredentials()
165
    {
166
        $contents = [
167
            '[client]',
168
            "user = '{$this->username}'",
169
            "password = '{$this->password}'",
170
            "host = '{$this->host}'",
171
            "port = '{$this->port}'",
172
        ];
173
        return implode(PHP_EOL, $contents);
174
    }
175
176 28
    public function prepareSingleTransaction()
177
    {
178 28
        return $this->singleTransaction ? '--single-transaction' : '';
179
    }
180
181 28
    public function prepareSkipLockTables()
182
    {
183 28
        return $this->skipLockTables ? '--skip-lock-tables' : '';
184
    }
185
186 28
    public function prepareQuick()
187
    {
188 28
        return $this->quick ? '--quick' : '';
189
    }
190
191 28
    public function prepareCreateTables()
192
    {
193 28
        return !$this->createTables ? '--no-create-info' : '';
194
    }
195
196 28
    public function prepareSkipComments()
197
    {
198 28
        return $this->skipComments ? '--skip-comments' : '';
199
    }
200
201 28
    public function prepareDefaultCharSet()
202
    {
203 28
        return ($this->defaultCharacterSet !== '') ? "--default-character-set={$this->defaultCharacterSet}" : '';
204
    }
205
206 34
    public function prepareAuthentication(string $credentialFile)
207
    {
208 34
        return "--defaults-extra-file=\"{$credentialFile}\"";
209
    }
210
}
211