Completed
Pull Request — master (#10)
by Mike
03:00
created

MySql::setTables()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 16
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 16
loc 16
rs 9.4285
cc 3
eloc 8
nc 3
nop 1
1
<?php
2
3
namespace Spatie\DbDumper\Databases;
4
5
use Spatie\DbDumper\DbDumper;
6
use Spatie\DbDumper\Exceptions\CannotStartDump;
7
use Spatie\DbDumper\Exceptions\CannotSetParameter;
8
use Symfony\Component\Process\Process;
9
10
class MySql extends DbDumper
11
{
12
    protected $dbName;
13
    protected $userName;
14
    protected $password;
15
    protected $host = 'localhost';
16
    protected $port = 3306;
17
    protected $socket;
18
    protected $dumpBinaryPath = '';
19
    protected $useExtendedInserts = true;
20
    protected $useSingleTransaction = false;
21
    protected $tables = array();
22
    protected $excludeTables = array();
23
    protected $timeout;
24
25
    /**
26
     * @return string
27
     */
28
    public function getDbName()
29
    {
30
        return $this->dbName;
31
    }
32
33
    /**
34
     * @param string $dbName
35
     *
36
     * @return \Spatie\DbDumper\Databases\MySql
37
     */
38
    public function setDbName($dbName)
39
    {
40
        $this->dbName = $dbName;
41
42
        return $this;
43
    }
44
45
    /**
46
     * @param string $userName
47
     *
48
     * @return \Spatie\DbDumper\Databases\MySql
49
     */
50
    public function setUserName($userName)
51
    {
52
        $this->userName = $userName;
53
54
        return $this;
55
    }
56
57
    /**
58
     * @param string $password
59
     *
60
     * @return \Spatie\DbDumper\Databases\MySql
61
     */
62
    public function setPassword($password)
63
    {
64
        $this->password = $password;
65
66
        return $this;
67
    }
68
69
    /**
70
     * @param string $host
71
     *
72
     * @return \Spatie\DbDumper\Databases\MySql
73
     */
74
    public function setHost($host)
75
    {
76
        $this->host = $host;
77
78
        return $this;
79
    }
80
81
    /**
82
     * @param int $port
83
     *
84
     * @return \Spatie\DbDumper\Databases\MySql
85
     */
86
    public function setPort($port)
87
    {
88
        $this->port = $port;
89
90
        return $this;
91
    }
92
93
    /**
94
     * @param int $socket
95
     *
96
     * @return \Spatie\DbDumper\Databases\MySql
97
     */
98
    public function setSocket($socket)
99
    {
100
        $this->socket = $socket;
101
102
        return $this;
103
    }
104
105
    /**
106
     * @param int $timeout
107
     *
108
     * @return \Spatie\DbDumper\Databases\PostgreSql
109
     */
110
    public function setTimeout($timeout)
111
    {
112
        $this->timeout = $timeout;
113
114
        return $this;
115
    }
116
117
    /**
118
     * @param string $dumpBinaryPath
119
     *
120
     * @return \Spatie\DbDumper\Databases\MySql
121
     */
122 View Code Duplication
    public function setDumpBinaryPath($dumpBinaryPath)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
123
    {
124
        if ($dumpBinaryPath !== '' && substr($dumpBinaryPath, -1) !== '/') {
125
            $dumpBinaryPath .= '/';
126
        }
127
128
        $this->dumpBinaryPath = $dumpBinaryPath;
129
130
        return $this;
131
    }
132
133
    /**
134
     * @param string/array $tables
0 ignored issues
show
Documentation introduced by
The doc-type string/array could not be parsed: Unknown type name "string/array" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
135
     *
136
     * @return \Spatie\DbDumper\Databases\MySql
137
     */
138 View Code Duplication
    public function setTables($tables)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
139
    {
140
        if (!empty($this->excludeTables)) {
141
            throw CannotSetParameter::conflictParameters('tables', 'excludeTables');
142
        }
143
144
        if (is_array($tables)) {
145
            $this->tables = $tables;
146
147
            return $this;
148
        }
149
150
        $this->tables = explode(' ', $tables);
151
152
        return $this;
153
    }
154
155
    /**
156
     * @param string/array $tables
0 ignored issues
show
Documentation introduced by
The doc-type string/array could not be parsed: Unknown type name "string/array" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
157
     *
158
     * @return \Spatie\DbDumper\Databases\MySql
159
     */
160 View Code Duplication
    public function setExcludeTables($tables)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
161
    {
162
         if (!empty($this->tables)) {
163
            throw CannotSetParameter::conflictParameters('excludeTables', 'tables');
164
        }
165
166
        if (is_array($tables)) {
167
            $this->excludeTables = $tables;
168
169
            return $this;
170
        }
171
172
        $this->excludeTables = explode(' ', $tables);
173
174
        return $this;
175
    }
176
177
    /**
178
     * @return \Spatie\DbDumper\Databases\MySql
179
     */
180
    public function useExtendedInserts()
181
    {
182
        $this->useExtendedInserts = true;
183
184
        return $this;
185
    }
186
187
    /**
188
     * @return \Spatie\DbDumper\Databases\MySql
189
     */
190
    public function dontUseExtendedInserts()
191
    {
192
        $this->useExtendedInserts = false;
193
194
        return $this;
195
    }
196
197
    /**
198
     * @return \Spatie\DbDumper\Databases\MySql
199
     */
200
    public function useSingleTransaction()
201
    {
202
        $this->useSingleTransaction = true;
203
204
        return $this;
205
    }
206
207
    /**
208
     * @return \Spatie\DbDumper\Databases\MySql
209
     */
210
    public function dontUseSingleTransaction()
211
    {
212
        $this->useSingleTransaction = false;
213
214
        return $this;
215
    }
216
217
    /**
218
     * Dump the contents of the database to the given file.
219
     *
220
     * @param string $dumpFile
221
     *
222
     * @throws \Spatie\DbDumper\Exceptions\CannotStartDump
223
     * @throws \Spatie\DbDumper\Exceptions\DumpFailed
224
     */
225 View Code Duplication
    public function dumpToFile($dumpFile)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
226
    {
227
        $this->guardAgainstIncompleteCredentials();
228
229
        $tempFileHandle = tmpfile();
230
        fwrite($tempFileHandle, $this->getContentsOfCredentialsFile());
231
        $temporaryCredentialsFile = stream_get_meta_data($tempFileHandle)['uri'];
232
233
        $command = $this->getDumpCommand($dumpFile, $temporaryCredentialsFile);
234
235
        $process = new Process($command);
236
237
        if (!is_null($this->timeout)) {
238
            $process->setTimeout($this->timeout);
239
        }
240
241
        $process->run();
242
243
        $this->checkIfDumpWasSuccessFul($process, $dumpFile);
244
    }
245
246
    /**
247
     * Get the command that should be performed to dump the database.
248
     *
249
     * @param string $dumpFile
250
     * @param string $temporaryCredentialsFile
251
     *
252
     * @return string
253
     */
254
    public function getDumpCommand($dumpFile, $temporaryCredentialsFile)
255
    {
256
        $command = [
257
            "{$this->dumpBinaryPath}mysqldump",
258
            "--defaults-extra-file=\"{$temporaryCredentialsFile}\"",
259
            '--skip-comments',
260
            $this->useExtendedInserts ? '--extended-insert' : '--skip-extended-insert',
261
        ];
262
263
        if ($this->useSingleTransaction) {
264
            $command[] = "--single-transaction";
265
        }
266
267
        if ($this->socket != '') {
268
            $command[] = "--socket={$this->socket}";
269
        }
270
271
        if (!empty($this->excludeTables)) {
272
            $command[] = '--ignore-table=' . implode(' --ignore-table=', $this->excludeTables);
273
        }
274
275
        $command[] = "{$this->dbName}";
276
277
        if (!empty($this->tables)) {
278
            $command[] = implode(' ', $this->tables);
279
        }
280
281
        $command[] = "> \"{$dumpFile}\"";
282
283
        return implode(' ', $command);
284
    }
285
286
    /**
287
     * @return string
288
     */
289
    public function getContentsOfCredentialsFile()
290
    {
291
        $contents = [
292
            '[client]',
293
            "user = '{$this->userName}'",
294
            "password = '{$this->password}'",
295
            "host = '{$this->host}'",
296
            "port = '{$this->port}'",
297
        ];
298
299
        return implode(PHP_EOL, $contents);
300
    }
301
302 View Code Duplication
    protected function guardAgainstIncompleteCredentials()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
303
    {
304
        foreach (['userName', 'dbName', 'host'] as $requiredProperty) {
305
            if (strlen($this->$requiredProperty) === 0) {
306
                throw CannotStartDump::emptyParameter($requiredProperty);
307
            }
308
        }
309
    }
310
}
311