Completed
Push — master ( 25e41e...31f911 )
by Freek
01:51
created

PostgreSql::setTimeout()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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