Completed
Push — master ( e364be...61cf73 )
by Freek
01:22
created

MySql::doNotCreateTables()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Spatie\DbDumper\Databases;
4
5
use Spatie\DbDumper\DbDumper;
6
use Symfony\Component\Process\Process;
7
use Spatie\DbDumper\Exceptions\CannotStartDump;
8
9
class MySql extends DbDumper
10
{
11
    /** @var bool */
12
    protected $skipComments = true;
13
14
    /** @var bool */
15
    protected $useExtendedInserts = true;
16
17
    /** @var bool */
18
    protected $useSingleTransaction = false;
19
20
    /** @var string */
21
    protected $defaultCharacterSet = '';
22
23
    /** @var bool */
24
    protected $dbNameWasSetAsExtraOption = false;
25
26
    /** @var string */
27
    protected $setGtidPurged = 'AUTO';
28
29
    /** @var bool */
30
    protected $createTables = true;
31
32
    public function __construct()
33
    {
34
        $this->port = 3306;
35
    }
36
37
    /**
38
     * @return $this
39
     */
40
    public function skipComments()
41
    {
42
        $this->skipComments = true;
43
44
        return $this;
45
    }
46
47
    /**
48
     * @return $this
49
     */
50
    public function dontSkipComments()
51
    {
52
        $this->skipComments = false;
53
54
        return $this;
55
    }
56
57
    /**
58
     * @return $this
59
     */
60
    public function useExtendedInserts()
61
    {
62
        $this->useExtendedInserts = true;
63
64
        return $this;
65
    }
66
67
    /**
68
     * @return $this
69
     */
70
    public function dontUseExtendedInserts()
71
    {
72
        $this->useExtendedInserts = false;
73
74
        return $this;
75
    }
76
77
    /**
78
     * @return $this
79
     */
80
    public function useSingleTransaction()
81
    {
82
        $this->useSingleTransaction = true;
83
84
        return $this;
85
    }
86
87
    /**
88
     * @return $this
89
     */
90
    public function dontUseSingleTransaction()
91
    {
92
        $this->useSingleTransaction = false;
93
94
        return $this;
95
    }
96
97
    /**
98
     * @param string $characterSet
99
     *
100
     * @return $this
101
     */
102
    public function setDefaultCharacterSet(string $characterSet)
103
    {
104
        $this->defaultCharacterSet = $characterSet;
105
106
        return $this;
107
    }
108
109
    /**
110
     * @return $this
111
     */
112
    public function setGtidPurged(string $setGtidPurged)
113
    {
114
        $this->setGtidPurged = $setGtidPurged;
115
116
        return $this;
117
    }
118
119
    /**
120
     * Dump the contents of the database to the given file.
121
     *
122
     * @param string $dumpFile
123
     *
124
     * @throws \Spatie\DbDumper\Exceptions\CannotStartDump
125
     * @throws \Spatie\DbDumper\Exceptions\DumpFailed
126
     */
127 View Code Duplication
    public function dumpToFile(string $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...
128
    {
129
        $this->guardAgainstIncompleteCredentials();
130
131
        $tempFileHandle = tmpfile();
132
        fwrite($tempFileHandle, $this->getContentsOfCredentialsFile());
133
        $temporaryCredentialsFile = stream_get_meta_data($tempFileHandle)['uri'];
134
135
        $command = $this->getDumpCommand($dumpFile, $temporaryCredentialsFile);
136
137
        $process = new Process($command);
0 ignored issues
show
Documentation introduced by
$command is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
138
139
        if (! is_null($this->timeout)) {
140
            $process->setTimeout($this->timeout);
141
        }
142
143
        $process->run();
144
145
        $this->checkIfDumpWasSuccessFul($process, $dumpFile);
146
    }
147
148
    public function addExtraOption(string $extraOption)
149
    {
150
        if (preg_match('/^--databases (\S+)/', $extraOption, $matches) === 1) {
151
            $this->setDbName($matches[1]);
152
            $this->dbNameWasSetAsExtraOption = true;
153
        }
154
155
        return parent::addExtraOption($extraOption);
156
    }
157
158
    /**
159
     * @return $this
160
     */
161
    public function doNotCreateTables()
162
    {
163
        $this->createTables = false;
164
165
        return $this;
166
    }
167
168
    /**
169
     * Get the command that should be performed to dump the database.
170
     *
171
     * @param string $dumpFile
172
     * @param string $temporaryCredentialsFile
173
     *
174
     * @return string
175
     */
176
    public function getDumpCommand(string $dumpFile, string $temporaryCredentialsFile): string
177
    {
178
        $quote = $this->determineQuote();
179
180
        $command = [
181
            "{$quote}{$this->dumpBinaryPath}mysqldump{$quote}",
182
            "--defaults-extra-file=\"{$temporaryCredentialsFile}\"",
183
        ];
184
185
        if (! $this->createTables) {
186
            $command[] = '--no-create-info';
187
        }
188
189
        if ($this->skipComments) {
190
            $command[] = '--skip-comments';
191
        }
192
193
        $command[] = $this->useExtendedInserts ? '--extended-insert' : '--skip-extended-insert';
194
195
        if ($this->useSingleTransaction) {
196
            $command[] = '--single-transaction';
197
        }
198
199
        if ($this->socket !== '') {
200
            $command[] = "--socket={$this->socket}";
201
        }
202
203
        foreach ($this->excludeTables as $tableName) {
204
            $command[] = "--ignore-table={$this->dbName}.{$tableName}";
205
        }
206
207
        if (! empty($this->defaultCharacterSet)) {
208
            $command[] = '--default-character-set='.$this->defaultCharacterSet;
209
        }
210
211
        foreach ($this->extraOptions as $extraOption) {
212
            $command[] = $extraOption;
213
        }
214
215
        if ($this->setGtidPurged !== 'AUTO') {
216
            $command[] = '--set-gtid-purged='.$this->setGtidPurged;
217
        }
218
219
        if (! $this->dbNameWasSetAsExtraOption) {
220
            $command[] = $this->dbName;
221
        }
222
223
        if (! empty($this->includeTables)) {
224
            $includeTables = implode(' ', $this->includeTables);
225
            $command[] = "--tables {$includeTables}";
226
        }
227
228
        return $this->echoToFile(implode(' ', $command), $dumpFile);
229
    }
230
231
    public function getContentsOfCredentialsFile(): string
232
    {
233
        $contents = [
234
            '[client]',
235
            "user = '{$this->userName}'",
236
            "password = '{$this->password}'",
237
            "host = '{$this->host}'",
238
            "port = '{$this->port}'",
239
        ];
240
241
        return implode(PHP_EOL, $contents);
242
    }
243
244 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...
245
    {
246
        foreach (['userName', 'dbName', 'host'] as $requiredProperty) {
247
            if (strlen($this->$requiredProperty) === 0) {
248
                throw CannotStartDump::emptyParameter($requiredProperty);
249
            }
250
        }
251
    }
252
253
    protected function determineQuote(): string
254
    {
255
        return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' ? '"' : "'";
256
    }
257
}
258