Completed
Pull Request — master (#21)
by
unknown
01:32
created

MySql::getDumpCommand()   D

Complexity

Conditions 8
Paths 128

Size

Total Lines 39
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 39
rs 4.6666
c 0
b 0
f 0
cc 8
eloc 21
nc 128
nop 2
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
    /** @var bool */
12
    protected $useExtendedInserts = true;
13
14
    /** @var bool */
15
    protected $useSingleTransaction = false;
16
17
    public function __construct()
18
    {
19
        $this->port = 3306;
20
    }
21
22
    /**
23
     * @return $this
24
     */
25
    public function useExtendedInserts()
26
    {
27
        $this->useExtendedInserts = true;
28
29
        return $this;
30
    }
31
32
    /**
33
     * @return $this
34
     */
35
    public function dontUseExtendedInserts()
36
    {
37
        $this->useExtendedInserts = false;
38
39
        return $this;
40
    }
41
42
    /**
43
     * @return $this
44
     */
45
    public function useSingleTransaction()
46
    {
47
        $this->useSingleTransaction = true;
48
49
        return $this;
50
    }
51
52
    /**
53
     * @return $this
54
     */
55
    public function dontUseSingleTransaction()
56
    {
57
        $this->useSingleTransaction = false;
58
59
        return $this;
60
    }
61
62
    /**
63
     * Dump the contents of the database to the given file.
64
     *
65
     * @param string $dumpFile
66
     *
67
     * @throws \Spatie\DbDumper\Exceptions\CannotStartDump
68
     * @throws \Spatie\DbDumper\Exceptions\DumpFailed
69
     */
70 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...
71
    {
72
        $this->guardAgainstIncompleteCredentials();
73
74
        $tempFileHandle = tmpfile();
75
        fwrite($tempFileHandle, $this->getContentsOfCredentialsFile());
76
        $temporaryCredentialsFile = stream_get_meta_data($tempFileHandle)['uri'];
77
78
        $command = $this->getDumpCommand($dumpFile, $temporaryCredentialsFile);
79
80
        $process = new Process($command);
81
82
        if (! is_null($this->timeout)) {
83
            $process->setTimeout($this->timeout);
84
        }
85
86
        $process->run();
87
88
        $this->checkIfDumpWasSuccessFul($process, $dumpFile);
89
    }
90
91
    /**
92
     * Get the command that should be performed to dump the database.
93
     *
94
     * @param string $dumpFile
95
     * @param string $temporaryCredentialsFile
96
     *
97
     * @return string
98
     */
99
    public function getDumpCommand(string $dumpFile, string $temporaryCredentialsFile): string
100
    {
101
        $command = [
102
            "\"{$this->dumpBinaryPath}mysqldump\"",
103
            "--defaults-extra-file=\"{$temporaryCredentialsFile}\"",
104
            '--skip-comments',
105
            $this->useExtendedInserts ? '--extended-insert' : '--skip-extended-insert',
106
        ];
107
108
        if ($this->useSingleTransaction) {
109
            $command[] = '--single-transaction';
110
        }
111
112
        if ($this->socket !== '') {
113
            $command[] = "--socket={$this->socket}";
114
        }
115
116
        if (! empty($this->excludeTables)) {
117
            $command[] = '--ignore-table='.implode(' --ignore-table=', $this->excludeTables);
118
        }
119
120
        foreach ($this->extraOptions as $extraOption) {
121
            $command[] = $extraOption;
122
        }
123
124
        $command[] = "{$this->dbName}";
125
126
        if (! empty($this->includeTables)) {
127
            $command[] = implode(' ', $this->includeTables);
128
        }
129
130
        if($this->extraOption){
0 ignored issues
show
Bug introduced by
The property extraOption does not seem to exist. Did you mean extraOptions?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
131
            $command[] = $this->extraOption;
0 ignored issues
show
Bug introduced by
The property extraOption does not seem to exist. Did you mean extraOptions?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
132
        }
133
134
        $command[] = "> \"{$dumpFile}\"";
135
136
        return implode(' ', $command);
137
    }
138
139
    public function getContentsOfCredentialsFile(): string
140
    {
141
        $contents = [
142
            '[client]',
143
            "user = '{$this->userName}'",
144
            "password = '{$this->password}'",
145
            "host = '{$this->host}'",
146
            "port = '{$this->port}'",
147
        ];
148
149
        return implode(PHP_EOL, $contents);
150
    }
151
152 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...
153
    {
154
        foreach (['userName', 'dbName', 'host'] as $requiredProperty) {
155
            if (strlen($this->$requiredProperty) === 0) {
156
                throw CannotStartDump::emptyParameter($requiredProperty);
157
            }
158
        }
159
    }
160
}
161