Completed
Push — master ( 3bfcfc...8b31a3 )
by Freek
02:33
created

PostgreSql::getDbName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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 $tables = array();
21
    protected $excludeTables = array();
22
    protected $timeout = null;
23
24
    /**
25
     * @return string
26
     */
27
    public function getDbName()
28
    {
29
        return $this->dbName;
30
    }
31
32
    /**
33
     * @param string $dbName
34
     *
35
     * @return \Spatie\DbDumper\Databases\PostgreSql
36
     */
37
    public function setDbName($dbName)
38
    {
39
        $this->dbName = $dbName;
40
41
        return $this;
42
    }
43
44
    /**
45
     * @param string $userName
46
     *
47
     * @return \Spatie\DbDumper\Databases\PostgreSql
48
     */
49
    public function setUserName($userName)
50
    {
51
        $this->userName = $userName;
52
53
        return $this;
54
    }
55
56
    /**
57
     * @param string $password
58
     *
59
     * @return \Spatie\DbDumper\Databases\PostgreSql
60
     */
61
    public function setPassword($password)
62
    {
63
        $this->password = $password;
64
65
        return $this;
66
    }
67
68
    /**
69
     * @param string $host
70
     *
71
     * @return \Spatie\DbDumper\Databases\PostgreSql
72
     */
73
    public function setHost($host)
74
    {
75
        $this->host = $host;
76
77
        return $this;
78
    }
79
80
    /**
81
     * @param int $port
82
     *
83
     * @return \Spatie\DbDumper\Databases\PostgreSql
84
     */
85
    public function setPort($port)
86
    {
87
        $this->port = $port;
88
89
        return $this;
90
    }
91
92
    /**
93
     * @param string $socket
94
     *
95
     * @return \Spatie\DbDumper\Databases\PostgreSql
96
     */
97
    public function setSocket($socket)
98
    {
99
        $this->socket = $socket;
100
101
        return $this;
102
    }
103
104
    /**
105
     * @param int $timeout
106
     *
107
     * @return \Spatie\DbDumper\Databases\PostgreSql
108
     */
109
    public function setTimeout($timeout)
110
    {
111
        $this->timeout = $timeout;
112
113
        return $this;
114
    }
115
116
    /**
117
     * @param string $dumpBinaryPath
118
     *
119
     * @return \Spatie\DbDumper\Databases\PostgreSql
120
     */
121 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...
122
    {
123
        if ($dumpBinaryPath !== '' && substr($dumpBinaryPath, -1) !== '/') {
124
            $dumpBinaryPath .= '/';
125
        }
126
127
        $this->dumpBinaryPath = $dumpBinaryPath;
128
129
        return $this;
130
    }
131
132
    /**
133
     * @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...
134
     *
135
     * @return \Spatie\DbDumper\Databases\MySql
136
     */
137 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...
138
    {
139
        if (!empty($this->excludeTables)) {
140
            throw CannotSetParameter::conflictParameters('tables', 'excludeTables');
141
        }
142
143
        if (is_array($tables)) {
144
            $this->tables = $tables;
145
146
            return $this;
147
        }
148
149
        $this->tables = explode(' ', $tables);
150
151
        return $this;
152
    }
153
154
    /**
155
     * @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...
156
     *
157
     * @return \Spatie\DbDumper\Databases\MySql
158
     */
159 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...
160
    {
161
         if (!empty($this->tables)) {
162
            throw CannotSetParameter::conflictParameters('excludeTables', 'tables');
163
        }
164
165
        if (is_array($tables)) {
166
            $this->excludeTables = $tables;
167
168
            return $this;
169
        }
170
171
        $this->excludeTables = explode(' ', $tables);
172
173
        return $this;
174
    }
175
    /**
176
     * @return \Spatie\DbDumper\Databases\PostgreSql
177
     */
178
    public function useInserts()
179
    {
180
        $this->useInserts = true;
181
182
        return $this;
183
    }
184
185
    /**
186
     * @return \Spatie\DbDumper\Databases\PostgreSql
187
     */
188
    public function dontUseInserts()
189
    {
190
        $this->useInserts = false;
191
192
        return $this;
193
    }
194
195
    /**
196
     * Dump the contents of the database to the given file.
197
     *
198
     * @param string $dumpFile
199
     *
200
     * @throws \Spatie\DbDumper\Exceptions\CannotStartDump
201
     * @throws \Spatie\DbDumper\Exceptions\DumpFailed
202
     */
203 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...
204
    {
205
        $this->guardAgainstIncompleteCredentials();
206
207
        $command = $this->getDumpCommand($dumpFile);
208
209
        $tempFileHandle = tmpfile();
210
        fwrite($tempFileHandle, $this->getContentsOfCredentialsFile());
211
        $temporaryCredentialsFile = stream_get_meta_data($tempFileHandle)['uri'];
212
213
        $process = new Process($command, null, $this->getEnvironmentVariablesForDumpCommand($temporaryCredentialsFile));
214
215
        if (!is_null($this->timeout)) {
216
            $process->setTimeout($this->timeout);
217
        }
218
219
        $process->run();
220
221
        $this->checkIfDumpWasSuccessFul($process, $dumpFile);
222
    }
223
224
    /**
225
     * Get the command that should be performed to dump the database.
226
     *
227
     * @param string $dumpFile
228
     *
229
     * @return string
230
     */
231
    public function getDumpCommand($dumpFile)
232
    {
233
        $command = [
234
            "{$this->dumpBinaryPath}pg_dump",
235
            "-d {$this->dbName}",
236
            "-U {$this->userName}",
237
            '-h '.($this->socket === '' ? $this->host : $this->socket),
238
            "-p {$this->port}",
239
            "--file=\"{$dumpFile}\"",
240
        ];
241
242
        if ($this->useInserts) {
243
            $command[] = '--inserts';
244
        }
245
246
        
247
        if (!empty($this->tables)) {
248
            $command[] = '-t ' . implode(' -t ', $this->tables);
249
        }
250
251
        if (!empty($this->excludeTables)) {
252
            $command[] = '-T ' . implode(' -T ', $this->excludeTables);
253
        }
254
255
        return implode(' ', $command);
256
    }
257
258
    /**
259
     * @return string
260
     */
261
    public function getContentsOfCredentialsFile()
262
    {
263
        $contents = [
264
            $this->host,
265
            $this->port,
266
            $this->dbName,
267
            $this->userName,
268
            $this->password,
269
        ];
270
271
        return implode(':', $contents);
272
    }
273
274 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...
275
    {
276
        foreach (['userName', 'dbName', 'host'] as $requiredProperty) {
277
            if ($this->$requiredProperty == '') {
278
                throw CannotStartDump::emptyParameter($requiredProperty);
279
            }
280
        }
281
    }
282
283
    /**
284
     * @param $temporaryCredentialsFile
285
     *
286
     * @return array
287
     */
288
    private function getEnvironmentVariablesForDumpCommand($temporaryCredentialsFile)
289
    {
290
        return [
291
            'PGPASSFILE' => $temporaryCredentialsFile,
292
        ];
293
    }
294
}
295