Completed
Push — master ( 49a884...259713 )
by Freek
01:50
created

MySql::dumpToFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 11

Duplication

Lines 20
Ratio 100 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 20
loc 20
rs 9.4285
cc 2
eloc 11
nc 2
nop 1
1
<?php
2
3
namespace Spatie\DbDumper\Databases;
4
5
use Spatie\DbDumper\DbDumper;
6
use Spatie\DbDumper\Exceptions\CannotStartDump;
7
use Symfony\Component\Process\Process;
8
9
class MySql extends DbDumper
10
{
11
    protected $dbName;
12
    protected $userName;
13
    protected $password;
14
    protected $host = 'localhost';
15
    protected $port = 3306;
16
    protected $socket;
17
    protected $dumpBinaryPath = '';
18
    protected $useExtendedInserts = true;
19
    protected $useSingleTransaction = false;
20
    protected $timeout;
21
22
    /**
23
     * @return string
24
     */
25
    public function getDbName()
26
    {
27
        return $this->dbName;
28
    }
29
30
    /**
31
     * @param string $dbName
32
     *
33
     * @return \Spatie\DbDumper\Databases\MySql
34
     */
35
    public function setDbName($dbName)
36
    {
37
        $this->dbName = $dbName;
38
39
        return $this;
40
    }
41
42
    /**
43
     * @param string $userName
44
     *
45
     * @return \Spatie\DbDumper\Databases\MySql
46
     */
47
    public function setUserName($userName)
48
    {
49
        $this->userName = $userName;
50
51
        return $this;
52
    }
53
54
    /**
55
     * @param string $password
56
     *
57
     * @return \Spatie\DbDumper\Databases\MySql
58
     */
59
    public function setPassword($password)
60
    {
61
        $this->password = $password;
62
63
        return $this;
64
    }
65
66
    /**
67
     * @param string $host
68
     *
69
     * @return \Spatie\DbDumper\Databases\MySql
70
     */
71
    public function setHost($host)
72
    {
73
        $this->host = $host;
74
75
        return $this;
76
    }
77
78
    /**
79
     * @param int $port
80
     *
81
     * @return \Spatie\DbDumper\Databases\MySql
82
     */
83
    public function setPort($port)
84
    {
85
        $this->port = $port;
86
87
        return $this;
88
    }
89
90
    /**
91
     * @param int $socket
92
     *
93
     * @return \Spatie\DbDumper\Databases\MySql
94
     */
95
    public function setSocket($socket)
96
    {
97
        $this->socket = $socket;
98
99
        return $this;
100
    }
101
102
    /**
103
     * @param int $timeout
104
     *
105
     * @return \Spatie\DbDumper\Databases\PostgreSql
106
     */
107
    public function setTimeout($timeout)
108
    {
109
        $this->timeout = $timeout;
110
111
        return $this;
112
    }
113
114
    /**
115
     * @param string $dumpBinaryPath
116
     *
117
     * @return \Spatie\DbDumper\Databases\MySql
118
     */
119 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...
120
    {
121
        if ($dumpBinaryPath !== '' && substr($dumpBinaryPath, -1) !== '/') {
122
            $dumpBinaryPath .= '/';
123
        }
124
125
        $this->dumpBinaryPath = $dumpBinaryPath;
126
127
        return $this;
128
    }
129
130
    /**
131
     * @return \Spatie\DbDumper\Databases\MySql
132
     */
133
    public function useExtendedInserts()
134
    {
135
        $this->useExtendedInserts = true;
136
137
        return $this;
138
    }
139
140
    /**
141
     * @return \Spatie\DbDumper\Databases\MySql
142
     */
143
    public function dontUseExtendedInserts()
144
    {
145
        $this->useExtendedInserts = false;
146
147
        return $this;
148
    }
149
150
    /**
151
     * @return \Spatie\DbDumper\Databases\MySql
152
     */
153
    public function useSingleTransaction()
154
    {
155
        $this->useSingleTransaction = true;
156
157
        return $this;
158
    }
159
160
    /**
161
     * @return \Spatie\DbDumper\Databases\MySql
162
     */
163
    public function dontUseSingleTransaction()
164
    {
165
        $this->useSingleTransaction = false;
166
167
        return $this;
168
    }
169
170
    /**
171
     * Dump the contents of the database to the given file.
172
     *
173
     * @param string $dumpFile
174
     *
175
     * @throws \Spatie\DbDumper\Exceptions\CannotStartDump
176
     * @throws \Spatie\DbDumper\Exceptions\DumpFailed
177
     */
178 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...
179
    {
180
        $this->guardAgainstIncompleteCredentials();
181
182
        $tempFileHandle = tmpfile();
183
        fwrite($tempFileHandle, $this->getContentsOfCredentialsFile());
184
        $temporaryCredentialsFile = stream_get_meta_data($tempFileHandle)['uri'];
185
186
        $command = $this->getDumpCommand($dumpFile, $temporaryCredentialsFile);
187
188
        $process = new Process($command);
189
190
        if (!is_null($this->timeout)) {
191
            $process->setTimeout($this->timeout);
192
        }
193
194
        $process->run();
195
196
        $this->checkIfDumpWasSuccessFul($process, $dumpFile);
197
    }
198
199
    /**
200
     * Get the command that should be performed to dump the database.
201
     *
202
     * @param string $dumpFile
203
     * @param string $temporaryCredentialsFile
204
     *
205
     * @return string
206
     */
207
    public function getDumpCommand($dumpFile, $temporaryCredentialsFile)
208
    {
209
        $command = [
210
            "{$this->dumpBinaryPath}mysqldump",
211
            "--defaults-extra-file=\"{$temporaryCredentialsFile}\"",
212
            '--skip-comments',
213
            $this->useExtendedInserts ? '--extended-insert' : '--skip-extended-insert',
214
        ];
215
216
        if ($this->useSingleTransaction) {
217
            $command[] = "--single-transaction";
218
        }
219
220
        if ($this->socket != '') {
221
            $command[] = "--socket={$this->socket}";
222
        }
223
224
        $command[] = "{$this->dbName} > \"{$dumpFile}\"";
225
226
        return implode(' ', $command);
227
    }
228
229
    /**
230
     * @return string
231
     */
232
    public function getContentsOfCredentialsFile()
233
    {
234
        $contents = [
235
            '[client]',
236
            "user = '{$this->userName}'",
237
            "password = '{$this->password}'",
238
            "host = '{$this->host}'",
239
            "port = '{$this->port}'",
240
        ];
241
242
        return implode(PHP_EOL, $contents);
243
    }
244
245 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...
246
    {
247
        foreach (['userName', 'dbName', 'host'] as $requiredProperty) {
248
            if (strlen($this->$requiredProperty) === 0) {
249
                throw CannotStartDump::emptyParameter($requiredProperty);
250
            }
251
        }
252
    }
253
}
254