Completed
Push — master ( 8b2ec6...0e6a6f )
by CodexShaper
02:08
created

MysqlDumper   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 180
Duplicated Lines 0 %

Test Coverage

Coverage 80.21%

Importance

Changes 13
Bugs 0 Features 0
Metric Value
eloc 85
c 13
b 0
f 0
dl 0
loc 180
ccs 77
cts 96
cp 0.8021
rs 9.84
wmc 32

18 Methods

Rating   Name   Duplication   Size   Complexity  
A dump() 0 5 2
A useQuick() 0 4 1
A useSkipLockTables() 0 4 1
A useSingleTransaction() 0 4 1
A restore() 0 5 2
A doNotUseSkipComments() 0 4 1
A setDefaultCharacterSet() 0 4 1
A doNotCreateTables() 0 4 1
A prepareSkipLockTables() 0 3 2
B runCommand() 0 32 6
A prepareAuthentication() 0 3 1
A prepareQuick() 0 3 2
A prepareDefaultCharSet() 0 3 2
A prepareRestoreCommand() 0 16 2
A prepareDumpCommand() 0 24 2
A getCredentials() 0 10 1
A prepareSkipComments() 0 3 2
A prepareSingleTransaction() 0 3 2
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
    public function useSingleTransaction()
24
    {
25
        $this->singleTransaction = true;
26
        return $this;
27
    }
28
    public function useSkipLockTables()
29
    {
30
        $this->skipLockTables = true;
31
        return $this;
32
    }
33
    public function useQuick()
34
    {
35
        $this->quick = true;
36
        return $this;
37
    }
38 2
    public function doNotUseSkipComments()
39
    {
40 2
        $this->skipComments = false;
41 2
        return $this;
42
    }
43
    public function doNotCreateTables()
44
    {
45
        $this->createTables = false;
46
        return $this;
47
    }
48
    public function setDefaultCharacterSet(string $charecterSet)
49
    {
50
        $this->defaultCharacterSet = $charecterSet;
51
        return $this;
52
    }
53
54 10
    public function dump(string $destinationPath = "")
55
    {
56 10
        $destinationPath = !empty($destinationPath) ? $destinationPath : $this->destinationPath;
57 10
        $this->runCommand($destinationPath, "dump");
58 10
        return $this;
59
    }
60
61 6
    public function restore(string $restorePath = "")
62
    {
63 6
        $restorePath = !empty($restorePath) ? $restorePath : $this->restorePath;
64 6
        $this->runCommand($restorePath, 'restore');
65 6
        return $this;
66
    }
67
68 10
    protected function prepareDumpCommand(string $credentialFile, string $destinationPath): string
69
    {
70 10
        $dumpCommand = sprintf(
71 10
            '%s %s %s %s %s %s %s %s %s %s %s %s',
72 10
            $this->quoteCommand("{$this->commandBinaryPath}mysqldump"),
73 10
            $this->prepareAuthentication($credentialFile),
74 10
            $this->prepareSocket(),
75 10
            $this->prepareSkipComments(),
76 10
            $this->prepareCreateTables(),
77 10
            $this->prepareSingleTransaction(),
78 10
            $this->prepareSkipLockTables(),
79 10
            $this->prepareQuick(),
80 10
            $this->prepareDefaultCharSet(),
81 10
            $this->prepareDatabase(),
82 10
            $this->prepareIncludeTables(),
83 10
            $this->prepareIgnoreTables()
84
        );
85
86 10
        if ($this->isCompress) {
87
            $compressCommand = $this->quoteCommand("{$this->compressBinaryPath}{$this->compressCommand}");
88 2
            return "{$dumpCommand} | {$compressCommand} > \"{$destinationPath}{$this->compressExtension}\"";
89
        }
90
91 8
        return "{$dumpCommand} > \"{$destinationPath}\"";
92
    }
93
94 6
    protected function prepareRestoreCommand(string $credentialFile, string $filePath): string
95
    {
96 6
        $restoreCommand = sprintf("%s %s %s",
97 6
            $this->quoteCommand("{$this->commandBinaryPath}mysql"),
98 6
            $this->prepareAuthentication($credentialFile),
99 6
            $this->prepareDatabase()
100
        );
101
102 6
        if ($this->isCompress) {
103
104 2
            $compressCommand = $this->quoteCommand("{$this->compressBinaryPath}{$this->compressCommand}");
105
106
            return "{$compressCommand} < \"{$filePath}\" | {$restoreCommand}";
107 4
        }
108
109
        return "{$restoreCommand} < \"{$filePath}\"";
110 16
    }
111
112
    protected function runCommand($filePath, $action)
113
    {
114 16
        try {
115 16
116 16
            $credentials = $this->getCredentials();
117 16
            $tempFile    = tempnam(sys_get_temp_dir(), 'mysqlpass');
118 16
            $handler     = fopen($tempFile, 'r+');
119
            if ($handler !== false) {
120 16
                fwrite($handler, $credentials);
121 10
122 10
                if ($action == 'dump') {
123 6
                    $dumpCommand   = $this->prepareDumpCommand($tempFile, $filePath);
124 6
                    $this->command = $this->removeExtraSpaces($dumpCommand);
125 6
                } else if ($action == 'restore') {
126
                    $dumpCommand   = $this->prepareRestoreCommand($tempFile, $filePath);
127
                    $this->command = $this->removeExtraSpaces($dumpCommand);
128 16
                }
129
130 16
                $process = $this->prepareProcessCommand();
131
132
                if ($this->debug) {
133 16
                    $process->mustRun();
134
                } else {
135
                    $process->run();
136 16
                }
137 16
138
                fclose($handler);
139
                unlink($tempFile);
140
            }
141
142
        } catch (ProcessFailedException $e) {
143
            throw new \Exception($e->getMessage());
144 16
145
        }
146 16
    }
147
148
    protected function getCredentials()
149 16
    {
150 16
        $contents = [
151 16
            '[client]',
152 16
            "user = '{$this->username}'",
153 16
            "password = '{$this->password}'",
154
            "host = '{$this->host}'",
155 16
            "port = '{$this->port}'",
156
        ];
157
        return implode(PHP_EOL, $contents);
158 16
    }
159
160 16
    public function prepareSingleTransaction()
161
    {
162
        return $this->singleTransaction ? '--single-transaction' : '';
163 10
    }
164
165 10
    public function prepareSkipLockTables()
166 10
    {
167 10
        return $this->skipLockTables ? '--skip-lock-tables' : '';
168
    }
169
170 10
    public function prepareQuick()
171
    {
172 10
        return $this->quick ? '--quick' : '';
173 10
    }
174
175
    public function prepareSkipComments()
176 10
    {
177 10
        return $this->skipComments ? '--skip-comments' : '';
178
    }
179
180 10
    public function prepareDefaultCharSet()
181
    {
182 10
        return ($this->defaultCharacterSet !== '') ? "--default-character-set={$this->defaultCharacterSet}" : '';
183
    }
184
185 10
    public function prepareAuthentication(string $credentialFile)
186
    {
187 10
        return "--defaults-extra-file=\"{$credentialFile}\"";
188
    }
189
}
190