Completed
Push — master ( 6d53b6...13dade )
by Freek
01:34
created

MySql::determineQuote()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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