Completed
Pull Request — master (#31)
by
unknown
01:35
created

MySql::useSkipComments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
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 $useExtendedInserts = true;
13
14
    /** @var bool */
15
    protected $useSingleTransaction = false;
16
17
    /** @var bool */
18
    protected $useSkipComments = true;
19
20
    public function __construct()
21
    {
22
        $this->port = 3306;
23
    }
24
25
    /**
26
     * @return $this
27
     */
28
    public function useExtendedInserts()
29
    {
30
        $this->useExtendedInserts = true;
31
32
        return $this;
33
    }
34
35
    /**
36
     * @return $this
37
     */
38
    public function dontUseExtendedInserts()
39
    {
40
        $this->useExtendedInserts = false;
41
42
        return $this;
43
    }
44
45
    /**
46
     * @return $this
47
     */
48
    public function useSingleTransaction()
49
    {
50
        $this->useSingleTransaction = true;
51
52
        return $this;
53
    }
54
55
    /**
56
     * @return $this
57
     */
58
    public function dontUseSingleTransaction()
59
    {
60
        $this->useSingleTransaction = false;
61
62
        return $this;
63
    }
64
65
    /**
66
     * @return $this
67
     */
68
    public function useSkipComments()
69
    {
70
        $this->useSkipComments = true;
71
72
        return $this;
73
    }
74
75
    /**
76
     * @return $this
77
     */
78
    public function dontUseSkipComments()
79
    {
80
        $this->useSkipComments = 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
        /* if call is made from MySqlTest */
125
        if ($temporaryCredentialsFile == 'credentials.txt') {
126
            $quote = '"';
127
        } else {
128
            $quote = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' ? '"' : "'");
129
        }
130
131
        $command = [];
132
133
        $command[] = $quote.$this->dumpBinaryPath.'mysqldump'.$quote;
134
        $command[] = '--defaults-extra-file="'.$temporaryCredentialsFile.'"';
135
        $command[] = $this->useExtendedInserts ? '--extended-insert' : '--skip-extended-insert';
136
137
        if ($this->useSkipComments) {
138
            $command[] = '--skip-comments';
139
        }
140
141
        if ($this->useSingleTransaction) {
142
            $command[] = '--single-transaction';
143
        }
144
145
        if ($this->socket !== '') {
146
            $command[] = '--socket='.$this->socket;
147
        }
148
149
        if (! empty($this->excludeTables)) {
150
            $command[] = '--ignore-table='.implode(' --ignore-table=', $this->excludeTables);
151
        }
152
153
        foreach ($this->extraOptions as $extraOption) {
154
            $command[] = $extraOption;
155
        }
156
157
        $command[] = "{$this->dbName}";
158
159
        if (! empty($this->includeTables)) {
160
            $command[] = implode(' ', $this->includeTables);
161
        }
162
163
        $command[] = "> \"{$dumpFile}\"";
164
165
        return implode(' ', $command);
166
    }
167
168
    public function getContentsOfCredentialsFile(): string
169
    {
170
        $contents = [
171
            '[client]',
172
            "user = '{$this->userName}'",
173
            "password = '{$this->password}'",
174
            "host = '{$this->host}'",
175
            "port = '{$this->port}'",
176
        ];
177
178
        return implode(PHP_EOL, $contents);
179
    }
180
181 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...
182
    {
183
        foreach (['userName', 'dbName', 'host'] as $requiredProperty) {
184
            if (strlen($this->$requiredProperty) === 0) {
185
                throw CannotStartDump::emptyParameter($requiredProperty);
186
            }
187
        }
188
    }
189
}
190