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

PostgreSql::getDbName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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 $socketDirectory = '';
12
13
    /**
14
     * PostgreSql constructor.
15
     */
16
    public function __construct()
17
    {
18
        $this->port = 5432;
19
    }
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 $socketDirectory
91
     *
92
     * @return \Spatie\DbDumper\Databases\PostgreSql
93
     */
94
    public function setSocketDirectory($socketDirectory)
95
    {
96
        $this->socketDirectory = $socketDirectory;
97
98
        return $this;
99
    }
100
101
    /**
102
     * Dump the contents of the database to the given file.
103
     *
104
     * @param string $dumpFile
105
     *
106
     * @throws \Spatie\DbDumper\Exceptions\CannotStartDump
107
     * @throws \Spatie\DbDumper\Exceptions\DumpFailed
108
     */
109
    public function dumpToFile($dumpFile)
110
    {
111
        $this->guardAgainstIncompleteCredentials();
112
113
        $command = $this->getDumpCommand($dumpFile);
114
115
        $process = new Process($command);
116
117
        $process->run();
118
119
        $this->checkIfDumpWasSuccessFul($process, $dumpFile);
120
    }
121
122
    /**
123
     * Get the command that should be performed to dump the database.
124
     *
125
     * @param string $dumpFile
126
     *
127
     * @return string
128
     */
129
    public function getDumpCommand($dumpFile)
130
    {
131
        $command = [
132
            "{$this->dumpBinaryPath}pg_dump",
133
            "-d {$this->dbName}",
134
            "-U {$this->userName}",
135
            "-W {$this->password}",
136
        ];
137
138
        if ($this->socketDirectory === '') {
139
            $command[] = "-h {$this->host}";
140
        } else {
141
            $command[] = "-h {$this->socketDirectory}";
142
        }
143
144
        $command[] = "-p {$this->port}";
145
        $command[] = "--file={$dumpFile}";
146
147
        return implode(' ', $command);
148
    }
149
150 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...
151
    {
152
        foreach (['userName', 'dbName', 'host'] as $requiredProperty) {
153
            if ($this->$requiredProperty === '') {
154
                throw CannotStartDump::emptyParameter($requiredProperty);
155
            }
156
        }
157
    }
158
}
159