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

PrepareOptionsTrait::getDumperClassName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 1
rs 10
1
<?php
2
3
namespace CodexShaper\Dumper\Traits;
4
5
trait PrepareOptionsTrait
6
{
7 58
    public function prepareHost()
8
    {
9 58
        switch (strtolower($this->getDumperClassName())) {
10 58
            case 'pgsqldumper':
11 30
                return ($this->socket !== '') ? $this->socket : $this->host;
12 28
            case 'mongodumper';
13 28
                return !empty($this->host) ? "--host {$this->host}" : "";
14
        }
15
    }
16
17 58
    public function preparePort()
18
    {
19 58
        switch (strtolower($this->getDumperClassName())) {
20 58
            case 'pgsqldumper':
21 30
                return !empty($this->port) ? '-p ' . $this->port : '';
22 28
            case 'mongodumper':
23 28
                return !empty($this->port) ? "--port {$this->port}" : "";
24
        }
25
    }
26
27 28
    public function prepareSocket()
28
    {
29 28
        switch (strtolower($this->getDumperClassName())) {
30 28
            case 'mysqldumper':
31 28
                return ($this->socket !== '') ? "--socket={$this->socket}" : '';
32
        }
33
    }
34
35 80
    public function prepareDatabase()
36
    {
37 80
        switch (strtolower($this->getDumperClassName())) {
38 80
            case 'mysqldumper':
39 46
            case 'pgsqldumper':
40 64
                return !empty($this->dbName) ? $this->dbName : "";
41 16
            case 'mongodumper';
42 16
                return !empty($this->dbName) ? "--db {$this->dbName}" : "";
43
        }
44
    }
45
46 58
    public function prepareUserName()
47
    {
48 58
        switch (strtolower($this->getDumperClassName())) {
49 58
            case 'pgsqldumper':
50 30
                return !empty($this->username) ? $this->username : "";
51 28
            case 'mongodumper';
52 28
                return !empty($this->username) ? "--username {$this->username}" : "";
53
        }
54
    }
55
56 52
    public function prepareIncludeTables()
57
    {
58 52
        switch (strtolower($this->getDumperClassName())) {
59 52
            case 'mysqldumper':
60 28
                $includeTables = (count($this->tables) > 0) ? implode(' ', $this->tables) : '';
61 28
                return !empty($includeTables) ? "--tables {$includeTables}" : '';
62 24
            case 'pgsqldumper':
63 24
                return (count($this->tables) > 0) ? '-t ' . implode(' -t ', $this->tables) : "";
64
        }
65
    }
66
67 52
    public function prepareIgnoreTables()
68
    {
69 52
        switch (strtolower($this->getDumperClassName())) {
70 52
            case 'mysqldumper':
71 28
                $ignoreTablesArgs = [];
72 28
                foreach ($this->ignoreTables as $tableName) {
73 4
                    $ignoreTablesArgs[] = "--ignore-table={$this->dbName}.{$tableName}";
74
                }
75 28
                return (count($ignoreTablesArgs) > 0) ? implode(' ', $ignoreTablesArgs) : '';
76 24
            case 'pgsqldumper';
77 24
                return (count($this->ignoreTables) > 0) ? '-T ' . implode(' -T ', $this->ignoreTables) : '';
78
        }
79
    }
80
81 76
    public function getDumpCommand(string $credentialFile = '', $destinationPath = '')
82
    {
83 76
        $destinationPath = !empty($destinationPath) ? $destinationPath : $this->destinationPath;
84 76
        switch (strtolower($this->getDumperClassName())) {
85 76
            case 'mysqldumper':
86 28
                $dumpCommand = $this->prepareDumpCommand($credentialFile, $destinationPath);
0 ignored issues
show
Bug introduced by
It seems like prepareDumpCommand() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

86
                /** @scrutinizer ignore-call */ 
87
                $dumpCommand = $this->prepareDumpCommand($credentialFile, $destinationPath);
Loading history...
87 28
                break;
88
            default:
89 48
                $dumpCommand = $this->prepareDumpCommand($destinationPath);
90 48
                break;
91
        }
92
93 76
        return $this->removeExtraSpaces($dumpCommand);
0 ignored issues
show
Bug introduced by
It seems like removeExtraSpaces() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

93
        return $this->/** @scrutinizer ignore-call */ removeExtraSpaces($dumpCommand);
Loading history...
94
    }
95
96 32
    public function getRestoreCommand(string $credentialFile = '', string $filePath = '')
97
    {
98 32
        $filePath = !empty($filePath) ? '"' . $filePath : $this->restorePath;
99 32
        switch (strtolower($this->getDumperClassName())) {
100 32
            case 'mysqldumper':
101 6
                $restoreCommand = $this->prepareRestoreCommand($credentialFile, $filePath);
0 ignored issues
show
Bug introduced by
It seems like prepareRestoreCommand() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

101
                /** @scrutinizer ignore-call */ 
102
                $restoreCommand = $this->prepareRestoreCommand($credentialFile, $filePath);
Loading history...
102 6
                break;
103
            default:
104 26
                $restoreCommand = $this->prepareRestoreCommand($filePath);
105 26
                break;
106
        }
107
108 32
        return $this->removeExtraSpaces($restoreCommand);
109
    }
110
111 108
    public function getDumperClassName()
112
    {
113 108
        $classWithNamespace = static::class;
114 108
        $partials           = explode("\\", $classWithNamespace);
115 108
        $className          = end($partials);
116 108
        return $className;
117
    }
118
}
119