Completed
Pull Request — master (#10)
by Mike
03:00
created

PostgreSql::getDumpCommand()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 1
Metric Value
c 5
b 0
f 1
dl 0
loc 26
rs 8.439
cc 5
eloc 15
nc 16
nop 1
1
<?php
2
3
namespace Spatie\DbDumper\Databases;
4
5
use Spatie\DbDumper\DbDumper;
6
use Spatie\DbDumper\Exceptions\CannotStartDump;
7
use Spatie\DbDumper\Exceptions\CannotSetParameter;
8
use Symfony\Component\Process\Process;
9
10
class PostgreSql extends DbDumper
11
{
12
    protected $dbName;
13
    protected $userName;
14
    protected $password;
15
    protected $host = 'localhost';
16
    protected $port = 5432;
17
    protected $socket = '';
18
    protected $dumpBinaryPath = '';
19
    protected $useInserts = false;
20
    protected $timeout = null;
21
22
    /**
23
     * @return string
24
     */
25
    public function getDbName()
26
    {
27
        return $this->dbName;
28
    }
29
30
    /**
31
     * @param string $dbName
32
     *
33
     * @return \Spatie\DbDumper\Databases\PostgreSql
34
     */
35
    public function setDbName($dbName)
36
    {
37
        $this->dbName = $dbName;
38
39
        return $this;
40
    }
41
42
    /**
43
     * @param string $userName
44
     *
45
     * @return \Spatie\DbDumper\Databases\PostgreSql
46
     */
47
    public function setUserName($userName)
48
    {
49
        $this->userName = $userName;
50
51
        return $this;
52
    }
53
54
    /**
55
     * @param string $password
56
     *
57
     * @return \Spatie\DbDumper\Databases\PostgreSql
58
     */
59
    public function setPassword($password)
60
    {
61
        $this->password = $password;
62
63
        return $this;
64
    }
65
66
    /**
67
     * @param string $host
68
     *
69
     * @return \Spatie\DbDumper\Databases\PostgreSql
70
     */
71
    public function setHost($host)
72
    {
73
        $this->host = $host;
74
75
        return $this;
76
    }
77
78
    /**
79
     * @param int $port
80
     *
81
     * @return \Spatie\DbDumper\Databases\PostgreSql
82
     */
83
    public function setPort($port)
84
    {
85
        $this->port = $port;
86
87
        return $this;
88
    }
89
90
    /**
91
     * @param string $socket
92
     *
93
     * @return \Spatie\DbDumper\Databases\PostgreSql
94
     */
95
    public function setSocket($socket)
96
    {
97
        $this->socket = $socket;
98
99
        return $this;
100
    }
101
102
    /**
103
     * @param int $timeout
104
     *
105
     * @return \Spatie\DbDumper\Databases\PostgreSql
106
     */
107
    public function setTimeout($timeout)
108
    {
109
        $this->timeout = $timeout;
110
111
        return $this;
112
    }
113
114
    /**
115
     * @param string $dumpBinaryPath
116
     *
117
     * @return \Spatie\DbDumper\Databases\PostgreSql
118
     */
119 View Code Duplication
    public function setDumpBinaryPath($dumpBinaryPath)
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...
120
    {
121
        if ($dumpBinaryPath !== '' && substr($dumpBinaryPath, -1) !== '/') {
122
            $dumpBinaryPath .= '/';
123
        }
124
125
        $this->dumpBinaryPath = $dumpBinaryPath;
126
127
        return $this;
128
    }
129
130
    /**
131
     * @param string/array $tables
0 ignored issues
show
Documentation introduced by
The doc-type string/array could not be parsed: Unknown type name "string/array" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
132
     *
133
     * @return \Spatie\DbDumper\Databases\MySql
134
     */
135 View Code Duplication
    public function setTables($tables)
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...
136
    {
137
        if (!empty($this->excludeTables)) {
0 ignored issues
show
Bug introduced by
The property excludeTables does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
138
            throw CannotSetParameter::conflictParameters('tables', 'excludeTables');
139
        }
140
141
        if (is_array($tables)) {
142
            $this->tables = $tables;
0 ignored issues
show
Bug introduced by
The property tables does not seem to exist. Did you mean excludeTables?

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...
143
144
            return $this;
145
        }
146
147
        $this->tables = explode(' ', $tables);
0 ignored issues
show
Bug introduced by
The property tables does not seem to exist. Did you mean excludeTables?

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...
148
149
        return $this;
150
    }
151
152
    /**
153
     * @param string/array $tables
0 ignored issues
show
Documentation introduced by
The doc-type string/array could not be parsed: Unknown type name "string/array" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
154
     *
155
     * @return \Spatie\DbDumper\Databases\MySql
156
     */
157 View Code Duplication
    public function setExcludeTables($tables)
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...
158
    {
159
         if (!empty($this->tables)) {
0 ignored issues
show
Bug introduced by
The property tables does not seem to exist. Did you mean excludeTables?

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...
160
            throw CannotSetParameter::conflictParameters('excludeTables', 'tables');
161
        }
162
163
        if (is_array($tables)) {
164
            $this->excludeTables = $tables;
165
166
            return $this;
167
        }
168
169
        $this->excludeTables = explode(' ', $tables);
170
171
        return $this;
172
    }
173
    /**
174
     * @return \Spatie\DbDumper\Databases\PostgreSql
175
     */
176
    public function useInserts()
177
    {
178
        $this->useInserts = true;
179
180
        return $this;
181
    }
182
183
    /**
184
     * @return \Spatie\DbDumper\Databases\PostgreSql
185
     */
186
    public function dontUseInserts()
187
    {
188
        $this->useInserts = false;
189
190
        return $this;
191
    }
192
193
    /**
194
     * Dump the contents of the database to the given file.
195
     *
196
     * @param string $dumpFile
197
     *
198
     * @throws \Spatie\DbDumper\Exceptions\CannotStartDump
199
     * @throws \Spatie\DbDumper\Exceptions\DumpFailed
200
     */
201 View Code Duplication
    public function dumpToFile($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...
202
    {
203
        $this->guardAgainstIncompleteCredentials();
204
205
        $command = $this->getDumpCommand($dumpFile);
206
207
        $tempFileHandle = tmpfile();
208
        fwrite($tempFileHandle, $this->getContentsOfCredentialsFile());
209
        $temporaryCredentialsFile = stream_get_meta_data($tempFileHandle)['uri'];
210
211
        $process = new Process($command, null, $this->getEnvironmentVariablesForDumpCommand($temporaryCredentialsFile));
212
213
        if (!is_null($this->timeout)) {
214
            $process->setTimeout($this->timeout);
215
        }
216
217
        $process->run();
218
219
        $this->checkIfDumpWasSuccessFul($process, $dumpFile);
220
    }
221
222
    /**
223
     * Get the command that should be performed to dump the database.
224
     *
225
     * @param string $dumpFile
226
     *
227
     * @return string
228
     */
229
    public function getDumpCommand($dumpFile)
230
    {
231
        $command = [
232
            "{$this->dumpBinaryPath}pg_dump",
233
            "-d {$this->dbName}",
234
            "-U {$this->userName}",
235
            '-h '.($this->socket === '' ? $this->host : $this->socket),
236
            "-p {$this->port}",
237
            "--file=\"{$dumpFile}\"",
238
        ];
239
240
        if ($this->useInserts) {
241
            $command[] = '--inserts';
242
        }
243
244
        
245
        if (!empty($this->tables)) {
0 ignored issues
show
Bug introduced by
The property tables does not seem to exist. Did you mean excludeTables?

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...
246
            $command[] = '-t ' . implode(' -t ', $this->tables);
0 ignored issues
show
Bug introduced by
The property tables does not seem to exist. Did you mean excludeTables?

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...
247
        }
248
249
        if (!empty($this->excludeTables)) {
250
            $command[] = '-T ' . implode(' -T ', $this->excludeTables);
251
        }
252
253
        return implode(' ', $command);
254
    }
255
256
    /**
257
     * @return string
258
     */
259
    public function getContentsOfCredentialsFile()
260
    {
261
        $contents = [
262
            $this->host,
263
            $this->port,
264
            $this->dbName,
265
            $this->userName,
266
            $this->password,
267
        ];
268
269
        return implode(':', $contents);
270
    }
271
272 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...
273
    {
274
        foreach (['userName', 'dbName', 'host'] as $requiredProperty) {
275
            if ($this->$requiredProperty == '') {
276
                throw CannotStartDump::emptyParameter($requiredProperty);
277
            }
278
        }
279
    }
280
281
    /**
282
     * @param $temporaryCredentialsFile
283
     *
284
     * @return array
285
     */
286
    private function getEnvironmentVariablesForDumpCommand($temporaryCredentialsFile)
287
    {
288
        return [
289
            'PGPASSFILE' => $temporaryCredentialsFile,
290
        ];
291
    }
292
}
293