Completed
Push — master ( 1cf736...113717 )
by Freek
01:58
created

PostgreSql::getEnvForDumpCommand()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
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
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 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...
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
        $process = new Process($command, null, $this->getEnvironmentVariablesForDumpCommand($temporaryCredentialsFile));
134
135
        $process->run();
136
137
        $this->checkIfDumpWasSuccessFul($process, $dumpFile);
138
    }
139
140
    /**
141
     * Get the command that should be performed to dump the database.
142
     *
143
     * @param string $dumpFile
144
     *
145
     * @return string
146
     */
147
    public function getDumpCommand($dumpFile)
148
    {
149
        $command = [
150
            "{$this->dumpBinaryPath}pg_dump",
151
            "-d {$this->dbName}",
152
            "-U {$this->userName}",
153
        ];
154
155
        $command[] = '-h '.($this->socket === '' ? $this->host : $this->socket);
156
        $command[] = "-p {$this->port}";
157
        $command[] = "--file={$dumpFile}";
158
159
        return implode(' ', $command);
160
    }
161
162
    /**
163
     * @return string
164
     */
165
    public function getContentsOfCredentialsFile()
166
    {
167
        $contents = [
168
            $this->host,
169
            $this->port,
170
            $this->dbName,
171
            $this->userName,
172
            $this->password,
173
        ];
174
175
        return implode(':', $contents);
176
    }
177
178 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...
179
    {
180
        foreach (['userName', 'dbName', 'host'] as $requiredProperty) {
181
            if ($this->$requiredProperty == '') {
182
                throw CannotStartDump::emptyParameter($requiredProperty);
183
            }
184
        }
185
    }
186
187
    /**
188
     * @param $temporaryCredentialsFile
189
     *
190
     * @return array
191
     */
192
    private function getEnvironmentVariablesForDumpCommand($temporaryCredentialsFile)
193
    {
194
        return [
195
            'PGPASSFILE' => $temporaryCredentialsFile,
196
        ];
197
    }
198
}
199