Completed
Push — master ( 55298c...13bbe1 )
by CodexShaper
02:05
created

PgsqlDumper::prepareCreateTables()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 1
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 2
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
    /*@var int*/
11
    protected $port = 5432;
12
    /*@var bool*/
13
    protected $useInserts = false;
14
    /*@var bool*/
15
    protected $createTables = true;
16
17 2
    public function useInserts()
18
    {
19 2
        $this->useInserts = true;
20 2
        return $this;
21
    }
22
23 2
    public function doNotCreateTables()
24
    {
25 2
        $this->createTables = false;
26
27 2
        return $this;
28
    }
29
30
    public function dump(string $destinationPath = "")
31
    {
32
        $destinationPath = !empty($destinationPath) ? '"' . $destinationPath . '"' : '"' . $this->destinationPath . '"';
33
        $dumpCommand     = $this->prepareDumpCommand($destinationPath);
34
        $this->command   = $this->removeExtraSpaces($dumpCommand);
35
        $this->runCommand();
36
    }
37
38
    public function restore(string $restorePath = "")
39
    {
40
        $restorePath    = !empty($restorePath) ? '"' . $restorePath . '"' : '"' . $this->restorePath . '"';
41
        $restoreCommand = $this->prepareRestoreCommand($restorePath);
42
        $this->command  = $this->removeExtraSpaces($restoreCommand);
43
        $this->runCommand();
44
    }
45
46 24
    protected function prepareDumpCommand(string $destinationPath): string
47
    {
48 24
        $dumpCommand = sprintf(
49 24
            '%s -U %s -h %s %s %s %s %s %s %s',
50 24
            $this->quoteCommand($this->commandBinaryPath . 'pg_dump'),
51 24
            $this->prepareUserName(),
52 24
            $this->prepareHost(),
53 24
            $this->preparePort(),
54 24
            $this->prepareUseInserts(),
55 24
            $this->prepareCreateTables(),
56 24
            $this->prepareIncludeTables(),
57 24
            $this->prepareIgnoreTables(),
58 24
            $this->prepareDatabase()
59
        );
60
61 24
        if ($this->isCompress) {
62 2
            $compressCommand = $this->quoteCommand("{$this->compressBinaryPath}{$this->compressCommand}");
63 2
            return "{$dumpCommand} | {$compressCommand} > \"{$destinationPath}{$this->compressExtension}\"";
64
        }
65 22
        return "{$dumpCommand} > \"{$destinationPath}\"";
66
    }
67
68 6
    protected function prepareRestoreCommand(string $filePath): string
69
    {
70 6
        $restoreCommand = sprintf("%s -U %s -h %s %s %s",
71 6
            $this->quoteCommand($this->commandBinaryPath . 'psql'),
72 6
            $this->prepareUserName(),
73 6
            $this->prepareHost(),
74 6
            $this->preparePort(),
75 6
            $this->prepareDatabase()
76
        );
77
78 6
        if ($this->isCompress) {
79 2
            $compressCommand = $this->quoteCommand("{$this->compressBinaryPath}{$this->compressCommand}");
80 2
            return "{$compressCommand} < \"{$filePath}\" | {$restoreCommand}";
81
        }
82
83 4
        return "{$restoreCommand} < \"{$filePath}\"";
84
    }
85
86
    protected function runCommand()
87
    {
88
        try {
89
90
            $credentials    = $this->host . ':' . $this->port . ':' . $this->dbName . ':' . $this->username . ':' . $this->password;
91
            $this->tempFile = tempnam(sys_get_temp_dir(), 'pgsqlpass');
0 ignored issues
show
Bug Best Practice introduced by
The property tempFile does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
92
            $handler        = fopen($this->tempFile, 'r+');
93
            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

93
            fwrite(/** @scrutinizer ignore-type */ $handler, $credentials);
Loading history...
94
            $env     = ['PGPASSFILE' => $this->tempFile];
95
            $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

95
            /** @scrutinizer ignore-call */ 
96
            $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...
96
            if ($this->debug) {
97
                $process->mustRun(null, $env);
98
            } else {
99
                $process->run(null, $env);
100
            }
101
102
            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

102
            fclose(/** @scrutinizer ignore-type */ $handler);
Loading history...
103
            unlink($this->tempFile);
104
105
        } catch (ProcessFailedException $e) {
106
            throw new \Exception($e->getMessage());
107
108
        }
109
    }
110
111 24
    public function prepareCreateTables()
112
    {
113 24
        return (!$this->createTables) ? '--data-only' : '';
114
    }
115
116 24
    public function prepareUseInserts()
117
    {
118 24
        return ($this->useInserts) ? '--inserts' : '';
119
    }
120
}
121