Completed
Push — master ( 8b31a3...e041f3 )
by Freek
02:29
created

MySql::excludeTables()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 14
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 14
loc 14
rs 9.4285
cc 3
eloc 7
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 $includeTables = 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 $includeTables
135
     *
136
     * @return \Spatie\DbDumper\Databases\MySql
137
     */
138 View Code Duplication
    public function includeTables($includeTables)
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::conflictingParameters('includeTables', 'excludeTables');
142
        }
143
144
        if (! is_array($includeTables)) {
145
            $includeTables = explode(', ', $includeTables);
146
        }
147
148
        $this->includeTables = $includeTables;
149
150
        return $this;
151
    }
152
153
    /**
154
     * @param string/array $excludeTables
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...
155
     *
156
     * @return \Spatie\DbDumper\Databases\MySql
157
     */
158 View Code Duplication
    public function excludeTables($excludeTables)
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...
159
    {
160
         if (!empty($this->includeTables)) {
161
            throw CannotSetParameter::conflictingParameters('excludeTables', 'tables');
162
        }
163
164
        if (! is_array($excludeTables)) {
165
            $excludeTables = explode(', ', $excludeTables);
166
        }
167
168
        $this->excludeTables = $excludeTables;
169
170
        return $this;
171
    }
172
173
    /**
174
     * @return \Spatie\DbDumper\Databases\MySql
175
     */
176
    public function useExtendedInserts()
177
    {
178
        $this->useExtendedInserts = true;
179
180
        return $this;
181
    }
182
183
    /**
184
     * @return \Spatie\DbDumper\Databases\MySql
185
     */
186
    public function dontUseExtendedInserts()
187
    {
188
        $this->useExtendedInserts = false;
189
190
        return $this;
191
    }
192
193
    /**
194
     * @return \Spatie\DbDumper\Databases\MySql
195
     */
196
    public function useSingleTransaction()
197
    {
198
        $this->useSingleTransaction = true;
199
200
        return $this;
201
    }
202
203
    /**
204
     * @return \Spatie\DbDumper\Databases\MySql
205
     */
206
    public function dontUseSingleTransaction()
207
    {
208
        $this->useSingleTransaction = false;
209
210
        return $this;
211
    }
212
213
    /**
214
     * Dump the contents of the database to the given file.
215
     *
216
     * @param string $dumpFile
217
     *
218
     * @throws \Spatie\DbDumper\Exceptions\CannotStartDump
219
     * @throws \Spatie\DbDumper\Exceptions\DumpFailed
220
     */
221 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...
222
    {
223
        $this->guardAgainstIncompleteCredentials();
224
225
        $tempFileHandle = tmpfile();
226
        fwrite($tempFileHandle, $this->getContentsOfCredentialsFile());
227
        $temporaryCredentialsFile = stream_get_meta_data($tempFileHandle)['uri'];
228
229
        $command = $this->getDumpCommand($dumpFile, $temporaryCredentialsFile);
230
231
        $process = new Process($command);
232
233
        if (!is_null($this->timeout)) {
234
            $process->setTimeout($this->timeout);
235
        }
236
237
        $process->run();
238
239
        $this->checkIfDumpWasSuccessFul($process, $dumpFile);
240
    }
241
242
    /**
243
     * Get the command that should be performed to dump the database.
244
     *
245
     * @param string $dumpFile
246
     * @param string $temporaryCredentialsFile
247
     *
248
     * @return string
249
     */
250
    public function getDumpCommand($dumpFile, $temporaryCredentialsFile)
251
    {
252
        $command = [
253
            "{$this->dumpBinaryPath}mysqldump",
254
            "--defaults-extra-file=\"{$temporaryCredentialsFile}\"",
255
            '--skip-comments',
256
            $this->useExtendedInserts ? '--extended-insert' : '--skip-extended-insert',
257
        ];
258
259
        if ($this->useSingleTransaction) {
260
            $command[] = "--single-transaction";
261
        }
262
263
        if ($this->socket != '') {
264
            $command[] = "--socket={$this->socket}";
265
        }
266
267
        if (!empty($this->excludeTables)) {
268
            $command[] = '--ignore-table=' . implode(' --ignore-table=', $this->excludeTables);
269
        }
270
271
        $command[] = "{$this->dbName}";
272
273
        if (!empty($this->includeTables)) {
274
            $command[] = implode(' ', $this->includeTables);
275
        }
276
277
        $command[] = "> \"{$dumpFile}\"";
278
279
        return implode(' ', $command);
280
    }
281
282
    /**
283
     * @return string
284
     */
285
    public function getContentsOfCredentialsFile()
286
    {
287
        $contents = [
288
            '[client]',
289
            "user = '{$this->userName}'",
290
            "password = '{$this->password}'",
291
            "host = '{$this->host}'",
292
            "port = '{$this->port}'",
293
        ];
294
295
        return implode(PHP_EOL, $contents);
296
    }
297
298 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...
299
    {
300
        foreach (['userName', 'dbName', 'host'] as $requiredProperty) {
301
            if (strlen($this->$requiredProperty) === 0) {
302
                throw CannotStartDump::emptyParameter($requiredProperty);
303
            }
304
        }
305
    }
306
}
307