Completed
Push — master ( 8e89e8...9e8f4c )
by Freek
01:51
created

MySql::setDumpBinaryPath()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 10
Ratio 100 %

Importance

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