Completed
Push — master ( cc1f22...aa5c0e )
by Freek
02:10
created

MySql::setTimeout()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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