Completed
Pull Request — master (#30)
by
unknown
01:42
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
        $command = [
125
            "\"{$this->dumpBinaryPath}mysqldump\"",
126
            "--defaults-extra-file=\"{$temporaryCredentialsFile}\"",
127
            $this->useExtendedInserts ? '--extended-insert' : '--skip-extended-insert',
128
        ];
129
130
        if ($this->useSkipComments) {
131
            $command[] = '--skip-comments';
132
        }
133
134
        if ($this->useSingleTransaction) {
135
            $command[] = '--single-transaction';
136
        }
137
138
        if ($this->socket !== '') {
139
            $command[] = "--socket={$this->socket}";
140
        }
141
142
        if (! empty($this->excludeTables)) {
143
            $command[] = '--ignore-table='.implode(' --ignore-table=', $this->excludeTables);
144
        }
145
146
        foreach ($this->extraOptions as $extraOption) {
147
            $command[] = $extraOption;
148
        }
149
150
        $command[] = "{$this->dbName}";
151
152
        if (! empty($this->includeTables)) {
153
            $command[] = implode(' ', $this->includeTables);
154
        }
155
156
        $command[] = "> \"{$dumpFile}\"";
157
158
        return implode(' ', $command);
159
    }
160
161
    public function getContentsOfCredentialsFile(): string
162
    {
163
        $contents = [
164
            '[client]',
165
            "user = '{$this->userName}'",
166
            "password = '{$this->password}'",
167
            "host = '{$this->host}'",
168
            "port = '{$this->port}'",
169
        ];
170
171
        return implode(PHP_EOL, $contents);
172
    }
173
174 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...
175
    {
176
        foreach (['userName', 'dbName', 'host'] as $requiredProperty) {
177
            if (strlen($this->$requiredProperty) === 0) {
178
                throw CannotStartDump::emptyParameter($requiredProperty);
179
            }
180
        }
181
    }
182
}
183