Completed
Push — master ( 8b2ec6...0e6a6f )
by CodexShaper
02:08
created

PgsqlDumper   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 57
c 1
b 0
f 0
dl 0
loc 106
ccs 0
cts 62
cp 0
rs 10
wmc 15

8 Methods

Rating   Name   Duplication   Size   Complexity  
A doNotCreateTables() 0 5 1
A useInserts() 0 4 1
A runCommand() 0 21 3
A prepareDumpCommand() 0 20 2
A prepareUseInserts() 0 3 2
A restore() 0 6 2
A dump() 0 6 2
A prepareRestoreCommand() 0 16 2
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
    public function useInserts()
18
    {
19
        $this->useInserts = true;
20
        return $this;
21
    }
22
23
    public function doNotCreateTables()
24
    {
25
        $this->createTables = false;
26
27
        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
    protected function prepareDumpCommand(string $destinationPath): string
47
    {
48
        $dumpCommand = sprintf(
49
            '%s -U %s -h %s %s %s %s %s %s %s',
50
            $this->quoteCommand($this->commandBinaryPath . 'pg_dump'),
51
            $this->prepareUserName(),
52
            $this->prepareHost(),
53
            $this->preparePort(),
54
            $this->prepareUseInserts(),
55
            $this->prepareCreateTables(),
56
            $this->prepareIncludeTables(),
57
            $this->prepareIgnoreTables(),
58
            $this->prepareDatabase()
59
        );
60
61
        if ($this->isCompress) {
62
            $compressCommand = $this->quoteCommand("{$this->compressBinaryPath}{$this->compressCommand}");
63
            return "{$dumpCommand} | {$compressCommand} > \"{$destinationPath}{$this->compressExtension}\"";
64
        }
65
        return "{$dumpCommand} > \"{$destinationPath}\"";
66
    }
67
68
    protected function prepareRestoreCommand(string $filePath): string
69
    {
70
        $restoreCommand = sprintf("%s -U %s -h %s %s %s",
71
            $this->quoteCommand($this->commandBinaryPath . 'psql'),
72
            $this->prepareUserName(),
73
            $this->prepareHost(),
74
            $this->preparePort(),
75
            $this->prepareDatabase()
76
        );
77
78
        if ($this->isCompress) {
79
            $compressCommand = $this->quoteCommand("{$this->compressBinaryPath}{$this->compressCommand}");
80
            return "{$compressCommand} < \"{$filePath}\" | {$restoreCommand}";
81
        }
82
83
        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
    public function prepareUseInserts()
112
    {
113
        return ($this->useInserts) ? '--inserts' : '';
114
    }
115
}
116