Completed
Pull Request — master (#3)
by
unknown
03:01
created

PostgreSql::setPassword()   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 int $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\PostgreSql
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
    public function dumpToFile($dumpFile)
124
    {
125
        $this->guardAgainstIncompleteCredentials();
126
127
        $command = $this->getDumpCommand($dumpFile);
128
129
        $process = new Process($command);
130
131
        $process->run();
132
133
        $this->checkIfDumpWasSuccessFul($process, $dumpFile);
134
    }
135
136
    /**
137
     * Get the command that should be performed to dump the database.
138
     *
139
     * @param string $dumpFile
140
     *
141
     * @return string
142
     */
143
    public function getDumpCommand($dumpFile)
144
    {
145
        $command = [
146
            "{$this->dumpBinaryPath}pg_dump",
147
            "-d {$this->dbName}",
148
            "-U {$this->userName}",
149
            "-W {$this->password}",
150
        ];
151
152
        if ($this->socket === '') {
0 ignored issues
show
Unused Code Bug introduced by
The strict comparison === seems to always evaluate to false as the types of $this->socket (integer) and '' (string) can never be identical. Maybe you want to use a loose comparison == instead?
Loading history...
153
            $command[] = "-h {$this->host}";
154
        } else {
155
            $command[] = "-h {$this->socket}";
156
        }
157
158
        $command[] = "-p {$this->port}";
159
        $command[] = "--file={$dumpFile}";
160
161
        return implode(' ', $command);
162
    }
163
164 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...
165
    {
166
        foreach (['userName', 'dbName', 'host'] as $requiredProperty) {
167
            if ($this->$requiredProperty === '') {
168
                throw CannotStartDump::emptyParameter($requiredProperty);
169
            }
170
        }
171
    }
172
}
173