Completed
Pull Request — master (#12)
by
unknown
102:08 queued 89:44
created

MySql::setSSLCaPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
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 $ssl_ca_path;
18
    protected $ssl_cert_path;
19
    protected $ssl_key_path;
20
    protected $socket;
21
    protected $dumpBinaryPath = '';
22
    protected $useExtendedInserts = true;
23
    protected $useSingleTransaction = false;
24
    protected $includeTables = array();
25
    protected $excludeTables = array();
26
    protected $timeout;
27
28
    /**
29
     * @return string
30
     */
31
    public function getDbName()
32
    {
33
        return $this->dbName;
34
    }
35
36
    /**
37
     * @param string $dbName
38
     *
39
     * @return \Spatie\DbDumper\Databases\MySql
40
     */
41
    public function setDbName($dbName)
42
    {
43
        $this->dbName = $dbName;
44
45
        return $this;
46
    }
47
48
    /**
49
     * @param string $userName
50
     *
51
     * @return \Spatie\DbDumper\Databases\MySql
52
     */
53
    public function setUserName($userName)
54
    {
55
        $this->userName = $userName;
56
57
        return $this;
58
    }
59
60
    /**
61
     * @param string $password
62
     *
63
     * @return \Spatie\DbDumper\Databases\MySql
64
     */
65
    public function setPassword($password)
66
    {
67
        $this->password = $password;
68
69
        return $this;
70
    }
71
72
    /**
73
     * @param string $host
74
     *
75
     * @return \Spatie\DbDumper\Databases\MySql
76
     */
77
    public function setHost($host)
78
    {
79
        $this->host = $host;
80
81
        return $this;
82
    }
83
84
    /**
85
     * @param int $port
86
     *
87
     * @return \Spatie\DbDumper\Databases\MySql
88
     */
89
    public function setPort($port)
90
    {
91
        $this->port = $port;
92
93
        return $this;
94
    }
95
    
96
    /**
97
     * @param string $options
0 ignored issues
show
Bug introduced by
There is no parameter named $options. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
98
     *
99
     * @return \Spatie\DbDumper\Databases\MySql
100
     */
101
    public function setSSLCaPath($path)
102
    {
103
        $this->ssl_ca_path = $path;
104
105
        return $this;
106
    }
107
    
108
    /**
109
     * @param string $path
110
     *
111
     * @return \Spatie\DbDumper\Databases\MySql
112
     */
113
    public function setSSLCertPath($path)
114
    {
115
        $this->ssl_cert_path = $path;
116
117
        return $this;
118
    }
119
    
120
    /**
121
     * @param string $path
122
     *
123
     * @return \Spatie\DbDumper\Databases\MySql
124
     */
125
    public function setSSLKeyPath($path)
126
    {
127
        $this->ssl_key_path = $path;
128
129
        return $this;
130
    }
131
132
    /**
133
     * @param int $socket
134
     *
135
     * @return \Spatie\DbDumper\Databases\MySql
136
     */
137
    public function setSocket($socket)
138
    {
139
        $this->socket = $socket;
140
141
        return $this;
142
    }
143
144
    /**
145
     * @param int $timeout
146
     *
147
     * @return \Spatie\DbDumper\Databases\PostgreSql
148
     */
149
    public function setTimeout($timeout)
150
    {
151
        $this->timeout = $timeout;
152
153
        return $this;
154
    }
155
156
    /**
157
     * @param string $dumpBinaryPath
158
     *
159
     * @return \Spatie\DbDumper\Databases\MySql
160
     */
161
    public function setDumpBinaryPath($dumpBinaryPath)
162
    {
163
        if ($dumpBinaryPath !== '' && substr($dumpBinaryPath, -1) !== '/') {
164
            $dumpBinaryPath .= '/';
165
        }
166
167
        $this->dumpBinaryPath = $dumpBinaryPath;
168
169
        return $this;
170
    }
171
172
    /**
173
     * @param string|array $includeTables
174
     *
175
     * @return \Spatie\DbDumper\Databases\MySql
176
     */
177 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...
178
    {
179
        if (!empty($this->excludeTables)) {
180
            throw CannotSetParameter::conflictingParameters('includeTables', 'excludeTables');
181
        }
182
183
        if (!is_array($includeTables)) {
184
            $includeTables = explode(', ', $includeTables);
185
        }
186
187
        $this->includeTables = $includeTables;
188
189
        return $this;
190
    }
191
192
    /**
193
     * @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...
194
     *
195
     * @return \Spatie\DbDumper\Databases\MySql
196
     */
197 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...
198
    {
199
        if (!empty($this->includeTables)) {
200
            throw CannotSetParameter::conflictingParameters('excludeTables', 'tables');
201
        }
202
203
        if (!is_array($excludeTables)) {
204
            $excludeTables = explode(', ', $excludeTables);
205
        }
206
207
        $this->excludeTables = $excludeTables;
208
209
        return $this;
210
    }
211
212
    /**
213
     * @return \Spatie\DbDumper\Databases\MySql
214
     */
215
    public function useExtendedInserts()
216
    {
217
        $this->useExtendedInserts = true;
218
219
        return $this;
220
    }
221
222
    /**
223
     * @return \Spatie\DbDumper\Databases\MySql
224
     */
225
    public function dontUseExtendedInserts()
226
    {
227
        $this->useExtendedInserts = false;
228
229
        return $this;
230
    }
231
232
    /**
233
     * @return \Spatie\DbDumper\Databases\MySql
234
     */
235
    public function useSingleTransaction()
236
    {
237
        $this->useSingleTransaction = true;
238
239
        return $this;
240
    }
241
242
    /**
243
     * @return \Spatie\DbDumper\Databases\MySql
244
     */
245
    public function dontUseSingleTransaction()
246
    {
247
        $this->useSingleTransaction = false;
248
249
        return $this;
250
    }
251
252
    /**
253
     * Dump the contents of the database to the given file.
254
     *
255
     * @param string $dumpFile
256
     *
257
     * @throws \Spatie\DbDumper\Exceptions\CannotStartDump
258
     * @throws \Spatie\DbDumper\Exceptions\DumpFailed
259
     */
260 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...
261
    {
262
        $this->guardAgainstIncompleteCredentials();
263
264
        $tempFileHandle = tmpfile();
265
        fwrite($tempFileHandle, $this->getContentsOfCredentialsFile());
266
        $temporaryCredentialsFile = stream_get_meta_data($tempFileHandle)['uri'];
267
268
        $command = $this->getDumpCommand($dumpFile, $temporaryCredentialsFile);
269
270
        $process = new Process($command);
271
272
        if (!is_null($this->timeout)) {
273
            $process->setTimeout($this->timeout);
274
        }
275
276
        $process->run();
277
278
        $this->checkIfDumpWasSuccessFul($process, $dumpFile);
279
    }
280
281
    /**
282
     * Get the command that should be performed to dump the database.
283
     *
284
     * @param string $dumpFile
285
     * @param string $temporaryCredentialsFile
286
     *
287
     * @return string
288
     */
289
    public function getDumpCommand($dumpFile, $temporaryCredentialsFile)
290
    {
291
        $command = [
292
            "{$this->dumpBinaryPath}mysqldump",
293
            "--defaults-extra-file=\"{$temporaryCredentialsFile}\"",
294
            '--skip-comments',
295
            $this->useExtendedInserts ? '--extended-insert' : '--skip-extended-insert',
296
        ];
297
298
        if ($this->useSingleTransaction) {
299
            $command[] = '--single-transaction';
300
        }
301
302
        if ($this->socket != '') {
303
            $command[] = "--socket={$this->socket}";
304
        }
305
306
        if (!empty($this->excludeTables)) {
307
            $command[] = '--ignore-table='.implode(' --ignore-table=', $this->excludeTables);
308
        }
309
310
        $command[] = "{$this->dbName}";
311
312
        if (!empty($this->includeTables)) {
313
            $command[] = implode(' ', $this->includeTables);
314
        }
315
316
        $command[] = "> \"{$dumpFile}\"";
317
318
        return implode(' ', $command);
319
    }
320
321
    /**
322
     * @return string
323
     */
324
    public function getContentsOfCredentialsFile()
325
    {
326
        $contents = [
327
            '[client]',
328
            "user = '{$this->userName}'",
329
            "password = '{$this->password}'",
330
            "host = '{$this->host}'",
331
            "port = '{$this->port}'",
332
        ];
333
        
334
        if($this->ssl_ca_path && $this->ssl_cert_path && $this->ssl_key_path)
335
        {
336
            $contents = array_push($contents,'ssl-ca',$this->ssl_ca_path);
337
            $contents = array_push($contents,'ssl-cert',$this->ssl_cert_path);
338
            $contents = array_push($contents,'ssl-key',$this->ssl_key_path);
339
        }
340
341
        return implode(PHP_EOL, $contents);
342
    }
343
344 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...
345
    {
346
        foreach (['userName', 'dbName', 'host'] as $requiredProperty) {
347
            if (strlen($this->$requiredProperty) === 0) {
348
                throw CannotStartDump::emptyParameter($requiredProperty);
349
            }
350
        }
351
    }
352
}
353