Completed
Push — master ( f7f2c0...1cf736 )
by Freek
7s
created

PostgreSql::getDumpCommand()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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