PostgreSql::getDumpCommand()   B
last analyzed

Complexity

Conditions 7
Paths 64

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 8.4586
c 0
b 0
f 0
cc 7
nc 64
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
    /** @var bool */
12
    protected $useInserts = false;
13
14
    /** @var bool */
15
    protected $createTables = true;
16
17
    public function __construct()
18
    {
19
        $this->port = 5432;
20
    }
21
22
    /**
23
     * @return $this
24
     */
25
    public function useInserts()
26
    {
27
        $this->useInserts = true;
28
29
        return $this;
30
    }
31
32
    /**
33
     * Dump the contents of the database to the given file.
34
     *
35
     * @param string $dumpFile
36
     *
37
     * @throws \Spatie\DbDumper\Exceptions\CannotStartDump
38
     * @throws \Spatie\DbDumper\Exceptions\DumpFailed
39
     */
40 View Code Duplication
    public function dumpToFile(string $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...
41
    {
42
        $this->guardAgainstIncompleteCredentials();
43
44
        $command = $this->getDumpCommand($dumpFile);
45
46
        $tempFileHandle = tmpfile();
47
        fwrite($tempFileHandle, $this->getContentsOfCredentialsFile());
48
        $temporaryCredentialsFile = stream_get_meta_data($tempFileHandle)['uri'];
49
50
        $envVars = $this->getEnvironmentVariablesForDumpCommand($temporaryCredentialsFile);
51
52
        $process = Process::fromShellCommandline($command, null, $envVars, null, $this->timeout);
53
54
        $process->run();
55
56
        $this->checkIfDumpWasSuccessFul($process, $dumpFile);
57
    }
58
59
    /**
60
     * Get the command that should be performed to dump the database.
61
     *
62
     * @param string $dumpFile
63
     *
64
     * @return string
65
     */
66
    public function getDumpCommand(string $dumpFile): string
67
    {
68
        $quote = $this->determineQuote();
69
70
        $command = [
71
            "{$quote}{$this->dumpBinaryPath}pg_dump{$quote}",
72
            "-U {$this->userName}",
73
            '-h '.($this->socket === '' ? $this->host : $this->socket),
74
            "-p {$this->port}",
75
        ];
76
77
        if ($this->useInserts) {
78
            $command[] = '--inserts';
79
        }
80
81
        if (! $this->createTables) {
82
            $command[] = '--data-only';
83
        }
84
85
        foreach ($this->extraOptions as $extraOption) {
86
            $command[] = $extraOption;
87
        }
88
89
        if (! empty($this->includeTables)) {
90
            $command[] = '-t '.implode(' -t ', $this->includeTables);
91
        }
92
93
        if (! empty($this->excludeTables)) {
94
            $command[] = '-T '.implode(' -T ', $this->excludeTables);
95
        }
96
97
        return $this->echoToFile(implode(' ', $command), $dumpFile);
98
    }
99
100
    public function getContentsOfCredentialsFile(): string
101
    {
102
        $contents = [
103
            $this->host,
104
            $this->port,
105
            $this->dbName,
106
            $this->userName,
107
            $this->password,
108
        ];
109
110
        return implode(':', $contents);
111
    }
112
113
    protected function guardAgainstIncompleteCredentials()
114
    {
115
        foreach (['userName', 'dbName', 'host'] as $requiredProperty) {
116
            if (empty($this->$requiredProperty)) {
117
                throw CannotStartDump::emptyParameter($requiredProperty);
118
            }
119
        }
120
    }
121
122
    protected function getEnvironmentVariablesForDumpCommand(string $temporaryCredentialsFile): array
123
    {
124
        return [
125
            'PGPASSFILE' => $temporaryCredentialsFile,
126
            'PGDATABASE' => $this->dbName,
127
        ];
128
    }
129
130
    /**
131
     * @return $this
132
     */
133
    public function doNotCreateTables()
134
    {
135
        $this->createTables = false;
136
137
        return $this;
138
    }
139
}
140