Completed
Push — master ( 08159b...58d02c )
by CodexShaper
08:25
created

PgsqlDumper::prepareUseInserts()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 1
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 6
rs 10
1
<?php
2
3
namespace CodexShaper\Dumper\Drivers;
4
5
use CodexShaper\Dumper\Dumper;
6
use Symfony\Component\Process\Exception\ProcessFailedException;
7
8
class PgsqlDumper extends Dumper
9
{
10
    protected $useInserts   = false;
11
    protected $createTables = true;
12
13
    public function useInserts()
14
    {
15
        $this->useInserts = true;
16
        return $this;
17
    }
18
19
    public function doNotUseCreateTables()
20
    {
21
        $this->createTables = false;
22
23
        return $this;
24
    }
25
26
    public function dump(string $destinationPath = "")
27
    {
28
        $destinationPath = !empty($destinationPath) ? $destinationPath : $this->destinationPath;
29
        $dumpCommand     = $this->prepareDumpCommand($destinationPath);
30
        $this->command   = $this->removeExtraSpaces($dumpCommand);
31
        $this->runCommand();
32
    }
33
34
    public function restore(string $restorePath = "")
35
    {
36
        $restorePath    = !empty($restorePath) ? $restorePath : $this->restorePath;
37
        $restoreCommand = $this->prepareRestoreCommand($restorePath);
38
        $this->command  = $this->removeExtraSpaces($restoreCommand);
39
        $this->runCommand();
40
    }
41
42
    protected function prepareDumpCommand(string $destinationPath): string
43
    {
44
        $dumpCommand = sprintf(
45
            '%spg_dump -U %s -h %s %s %s %s %s %s %s',
46
            $this->dumpCommandPath,
47
            $this->prepareUsername(),
48
            $this->prepareHost(),
49
            $this->preparePort(),
50
            $this->prepareUseInserts(),
51
            $this->prepareCreateTables(),
52
            $this->prepareIncludeTables(),
53
            $this->prepareIgnoreTables(),
54
            $this->prepareDatabase()
55
        );
56
57
        if ($this->isCompress) {
58
            return "{$dumpCommand} | {$this->compressBinaryPath}{$this->compressCommand} > {$destinationPath}{$this->compressExtension}";
59
        }
60
        return "{$dumpCommand} > {$destinationPath}";
61
    }
62
63
    protected function prepareRestoreCommand(string $filePath): string
64
    {
65
        $restoreCommand = sprintf("%spsql -U %s -h %s %s %s",
66
            $this->dumpCommandPath,
67
            $this->prepareUsername(),
68
            $this->prepareHost(),
69
            $this->preparePort(),
70
            $this->prepareDatabase()
71
        );
72
73
        if ($this->isCompress) {
74
            return "{$this->compressBinaryPath}{$this->compressCommand} < {$filePath} | {$restoreCommand}";
75
        }
76
77
        return "{$restoreCommand} < {$filePath}";
78
    }
79
80
    protected function runCommand()
81
    {
82
        try {
83
84
            $credentials    = $this->host . ':' . $this->port . ':' . $this->dbName . ':' . $this->username . ':' . $this->password;
85
            $this->tempFile = tempnam(sys_get_temp_dir(), 'pgsqlpass');
86
            $handler        = fopen($this->tempFile, 'r+');
87
            fwrite($handler, $credentials);
0 ignored issues
show
Bug introduced by
It seems like $handler can also be of type false; however, parameter $handle of fwrite() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

87
            fwrite(/** @scrutinizer ignore-type */ $handler, $credentials);
Loading history...
88
            $env     = ['PGPASSFILE' => $this->tempFile];
89
            $process = $this->prepareProcessCommand($this->command);
0 ignored issues
show
Unused Code introduced by
The call to CodexShaper\Dumper\Dumper::prepareProcessCommand() has too many arguments starting with $this->command. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

89
            /** @scrutinizer ignore-call */ 
90
            $process = $this->prepareProcessCommand($this->command);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
90
            if ($this->debug) {
91
                $process->mustRun(null, $env);
92
            } else {
93
                $process->run(null, $env);
94
            }
95
96
            fclose($handler);
0 ignored issues
show
Bug introduced by
It seems like $handler can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

96
            fclose(/** @scrutinizer ignore-type */ $handler);
Loading history...
97
            unlink($this->tempFile);
98
99
        } catch (ProcessFailedException $e) {
100
            throw new \Exception($e->getMessage());
101
102
        }
103
    }
104
105
    public function prepareDatabase()
106
    {
107
        return !empty($this->dbName) ? $this->dbName : "";
108
    }
109
110
    public function prepareUsername()
111
    {
112
        return !empty($this->username) ? $this->username : "";
113
    }
114
115
    public function prepareHost()
116
    {
117
        return ($this->socket !== '') ? $this->socket : $this->host;
118
    }
119
120
    public function preparePort()
121
    {
122
        return !empty($this->port) ? '-p ' . $this->port : '';
123
    }
124
125
    public function prepareIncludeTables()
126
    {
127
        return (count($this->tables) > 0) ? '-t ' . implode(' -t ', $this->tables) : "";
128
    }
129
130
    public function prepareIgnoreTables()
131
    {
132
        return (count($this->ignoreTables) > 0) ? '-T ' . implode(' -T ', $this->ignoreTables) : '';
133
    }
134
135
    public function prepareCreateTables()
136
    {
137
        return (!$this->createTables) ? '--data-only' : '';
138
    }
139
140
    public function prepareUseInserts()
141
    {
142
        return (!$this->useInserts) ? '--inserts' : '';
143
    }
144
}
145