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