Completed
Push — master ( 8b31a3...e041f3 )
by Freek
02:29
created

PostgreSql::getDumpCommand()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
c 6
b 0
f 0
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 $includeTables = 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 $includeTables
134
     *
135
     * @return \Spatie\DbDumper\Databases\MySql
136
     */
137 View Code Duplication
    public function includeTables($includeTables)
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::conflictingParameters('includeTables', 'excludeTables');
141
        }
142
143
        if (! is_array($includeTables)) {
144
            $includeTables = explode(', ', $includeTables);
145
        }
146
147
        $this->includeTables = $includeTables;
148
149
        return $this;
150
    }
151
152
    /**
153
     * @param string|array $excludeTables
154
     *
155
     * @return \Spatie\DbDumper\Databases\MySql
156
     */
157 View Code Duplication
    public function excludeTables($excludeTables)
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->includeTables)) {
160
            throw CannotSetParameter::conflictingParameters('excludeTables', 'tables');
161
        }
162
163
        if (! is_array($excludeTables)) {
164
            $excludeTables = explode(', ', $excludeTables);
165
        }
166
167
        $this->excludeTables = $excludeTables;
168
169
        return $this;
170
    }
171
    /**
172
     * @return \Spatie\DbDumper\Databases\PostgreSql
173
     */
174
    public function useInserts()
175
    {
176
        $this->useInserts = true;
177
178
        return $this;
179
    }
180
181
    /**
182
     * @return \Spatie\DbDumper\Databases\PostgreSql
183
     */
184
    public function dontUseInserts()
185
    {
186
        $this->useInserts = false;
187
188
        return $this;
189
    }
190
191
    /**
192
     * Dump the contents of the database to the given file.
193
     *
194
     * @param string $dumpFile
195
     *
196
     * @throws \Spatie\DbDumper\Exceptions\CannotStartDump
197
     * @throws \Spatie\DbDumper\Exceptions\DumpFailed
198
     */
199 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...
200
    {
201
        $this->guardAgainstIncompleteCredentials();
202
203
        $command = $this->getDumpCommand($dumpFile);
204
205
        $tempFileHandle = tmpfile();
206
        fwrite($tempFileHandle, $this->getContentsOfCredentialsFile());
207
        $temporaryCredentialsFile = stream_get_meta_data($tempFileHandle)['uri'];
208
209
        $process = new Process($command, null, $this->getEnvironmentVariablesForDumpCommand($temporaryCredentialsFile));
210
211
        if (!is_null($this->timeout)) {
212
            $process->setTimeout($this->timeout);
213
        }
214
215
        $process->run();
216
217
        $this->checkIfDumpWasSuccessFul($process, $dumpFile);
218
    }
219
220
    /**
221
     * Get the command that should be performed to dump the database.
222
     *
223
     * @param string $dumpFile
224
     *
225
     * @return string
226
     */
227
    public function getDumpCommand($dumpFile)
228
    {
229
        $command = [
230
            "{$this->dumpBinaryPath}pg_dump",
231
            "-d {$this->dbName}",
232
            "-U {$this->userName}",
233
            '-h '.($this->socket === '' ? $this->host : $this->socket),
234
            "-p {$this->port}",
235
            "--file=\"{$dumpFile}\"",
236
        ];
237
238
        if ($this->useInserts) {
239
            $command[] = '--inserts';
240
        }
241
242
        
243
        if (!empty($this->includeTables)) {
244
            $command[] = '-t ' . implode(' -t ', $this->includeTables);
245
        }
246
247
        if (!empty($this->excludeTables)) {
248
            $command[] = '-T ' . implode(' -T ', $this->excludeTables);
249
        }
250
251
        return implode(' ', $command);
252
    }
253
254
    /**
255
     * @return string
256
     */
257
    public function getContentsOfCredentialsFile()
258
    {
259
        $contents = [
260
            $this->host,
261
            $this->port,
262
            $this->dbName,
263
            $this->userName,
264
            $this->password,
265
        ];
266
267
        return implode(':', $contents);
268
    }
269
270 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...
271
    {
272
        foreach (['userName', 'dbName', 'host'] as $requiredProperty) {
273
            if ($this->$requiredProperty == '') {
274
                throw CannotStartDump::emptyParameter($requiredProperty);
275
            }
276
        }
277
    }
278
279
    /**
280
     * @param $temporaryCredentialsFile
281
     *
282
     * @return array
283
     */
284
    private function getEnvironmentVariablesForDumpCommand($temporaryCredentialsFile)
285
    {
286
        return [
287
            'PGPASSFILE' => $temporaryCredentialsFile,
288
        ];
289
    }
290
}
291