Completed
Pull Request — master (#31)
by
unknown
02:08 queued 39s
created

MySql   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 189
Duplicated Lines 14.81 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 3
dl 28
loc 189
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A dumpToFile() 20 20 2
A skipComments() 0 6 1
A dontSkipComments() 0 6 1
A useExtendedInserts() 0 6 1
A dontUseExtendedInserts() 0 6 1
A useSingleTransaction() 0 6 1
A dontUseSingleTransaction() 0 6 1
D getDumpCommand() 0 41 8
A getContentsOfCredentialsFile() 0 12 1
A guardAgainstIncompleteCredentials() 8 8 3
A determineQuote() 0 11 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
        $quote = $this->determineQuote($temporaryCredentialsFile);
125
126
        $command = [
127
            "{$quote}{$this->dumpBinaryPath}mysqldump{$quote}",
128
            "--defaults-extra-file=\"{$temporaryCredentialsFile}\""
129
        ];
130
131
        if ($this->skipComments) {
132
            $command[] = '--skip-comments';
133
        }
134
135
        $command[] = $this->useExtendedInserts ? '--extended-insert' : '--skip-extended-insert';
136
137
        if ($this->useSingleTransaction) {
138
            $command[] = '--single-transaction';
139
        }
140
141
        if ($this->socket !== '') {
142
            $command[] = "--socket={$this->socket}";
143
        }
144
145
        if (! empty($this->excludeTables)) {
146
            $command[] = '--ignore-table='.implode(' --ignore-table=', $this->excludeTables);
147
        }
148
149
        foreach ($this->extraOptions as $extraOption) {
150
            $command[] = $extraOption;
151
        }
152
153
        $command[] = "{$this->dbName}";
154
155
        if (! empty($this->includeTables)) {
156
            $command[] = implode(' ', $this->includeTables);
157
        }
158
159
        $command[] = "> \"{$dumpFile}\"";
160
161
        return implode(' ', $command);
162
    }
163
164
    public function getContentsOfCredentialsFile(): string
165
    {
166
        $contents = [
167
            '[client]',
168
            "user = '{$this->userName}'",
169
            "password = '{$this->password}'",
170
            "host = '{$this->host}'",
171
            "port = '{$this->port}'",
172
        ];
173
174
        return implode(PHP_EOL, $contents);
175
    }
176
177 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...
178
    {
179
        foreach (['userName', 'dbName', 'host'] as $requiredProperty) {
180
            if (strlen($this->$requiredProperty) === 0) {
181
                throw CannotStartDump::emptyParameter($requiredProperty);
182
            }
183
        }
184
    }
185
186
    public function determineQuote($temporaryCredentialsFile)
187
    {
188
        $quote = '"';
189
190
        /* if call is not made from MySqlTest */
191
        if ($temporaryCredentialsFile != 'credentials.txt') {
192
            $quote = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' ? '"' : "'");
193
        }
194
195
        return $quote;
196
    }
197
}
198