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

Dumper::preparePort()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 8
c 0
b 0
f 0
nc 5
nop 0
dl 0
loc 11
ccs 0
cts 0
cp 0
crap 30
rs 9.6111
1
<?php
2
3
namespace CodexShaper\Dumper;
4
5
use CodexShaper\Dumper\Contracts\Dumper as DumperContract;
6
use CodexShaper\Dumper\Traits\DumperTrait;
7
use Symfony\Component\Process\Exception\ProcessFailedException;
8
use Symfony\Component\Process\Process;
9
10
abstract class Dumper implements DumperContract
11
{
12
    use DumperTrait;
13
14 18
    public function __construct(array $options = [])
15
    {
16 18
        foreach ($options as $option => $value) {
17
            if (property_exists($this, $option)) {
18
                $this->{$option} = $value;
19
            }
20
        }
21 18
    }
22
    /**
23
     * @return $this
24
     */
25 18
    public static function create(array $options = [])
26
    {
27 18
        return new static($options);
28
    }
29
    /**
30
     * @return \Symfony\Component\Process\Process
31
     */
32 16
    protected function prepareProcessCommand()
33
    {
34 16
        $process = Process::fromShellCommandline($this->command);
35 16
        $process->setTimeout($this->timeout);
36 16
        return $process;
37
    }
38
    /**
39
     * @return \Symfony\Component\Process\Process
40
     */
41
    protected function run()
42
    {
43
        try {
44
45
            $process = Process::fromShellCommandline($this->command);
46
            $process->setTimeout($this->timeout);
47
48
            if ($this->debug) {
49
                return $process->mustRun();
50
            }
51
52
            return $process->run();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $process->run() returns the type integer which is incompatible with the documented return type Symfony\Component\Process\Process.
Loading history...
53
54
        } catch (ProcessFailedException $e) {
55
            throw new \Exception($e->getMessage());
56
57
        }
58
    }
59
60
    abstract public function dump();
61
    abstract public function restore();
62
63
    public function prepareHost()
64
    {
65
        switch (strtolower($this->getClassName())) {
66
            case 'pgsqldumper':
67
                $host = ($this->socket !== '') ? $this->socket : $this->host;
68
                break;
69
            case 'mongodumper';
70
                $host = !empty($this->host) ? "--host {$this->host}" : "";
71
                break;
72
        }
73
        return $host;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $host does not seem to be defined for all execution paths leading up to this point.
Loading history...
74
    }
75
76
    public function preparePort()
77
    {
78
        switch (strtolower($this->getClassName())) {
79
            case 'pgsqldumper':
80
                $port = !empty($this->port) ? '-p ' . $this->port : '';
81
                break;
82
            case 'mongodumper':
83
                $port = !empty($this->port) ? "--port {$this->port}" : "";
84
                break;
85
        }
86
        return $port;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $port does not seem to be defined for all execution paths leading up to this point.
Loading history...
87
    }
88
89
    public function prepareSocket()
90
    {
91
        switch (strtolower($this->getClassName())) {
92
            case 'mysqldumper':
93
                $socket = ($this->socket !== '') ? "--socket={$this->socket}" : '';
94
                break;
95
        }
96
97
        return $socket;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $socket does not seem to be defined for all execution paths leading up to this point.
Loading history...
98
    }
99
100
    public function prepareDatabase()
101
    {
102
        switch (strtolower($this->getClassName())) {
103
            case 'mysqldumper':
104
            case 'pgsqldumper':
105
                $databse = !empty($this->dbName) ? $this->dbName : "";
106
                break;
107
            case 'mongodumper';
108
                $databse = !empty($this->dbName) ? "--db {$this->dbName}" : "";
109
                break;
110
        }
111
        return $databse;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $databse does not seem to be defined for all execution paths leading up to this point.
Loading history...
112
    }
113
114
    public function prepareUserName()
115
    {
116
        switch (strtolower($this->getClassName())) {
117
            case 'pgsqldumper':
118
                $username = !empty($this->username) ? $this->username : "";
119
                break;
120
            case 'mongodumper';
121
                $username = !empty($this->username) ? "--username {$this->username}" : "";
122
                break;
123
        }
124
        return $username;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $username does not seem to be defined for all execution paths leading up to this point.
Loading history...
125
    }
126
127
    public function prepareIncludeTables()
128
    {
129
        switch (strtolower($this->getClassName())) {
130
            case 'mysqldumper':
131
                $includeTables    = (count($this->tables) > 0) ? implode(' ', $this->tables) : '';
132
                $includeTablesArg = !empty($includeTables) ? "--tables {$includeTables}" : '';
133
                break;
134
            case 'pgsqldumper':
135
                $includeTablesArg = (count($this->tables) > 0) ? '-t ' . implode(' -t ', $this->tables) : "";
136
                break;
137
        }
138
139
        return $includeTablesArg;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $includeTablesArg does not seem to be defined for all execution paths leading up to this point.
Loading history...
140
    }
141
142
    public function prepareIgnoreTables()
143
    {
144
        switch (strtolower($this->getClassName())) {
145
            case 'mysqldumper':
146
                $ignoreTablesArgs = [];
147
                foreach ($this->ignoreTables as $tableName) {
148
                    $ignoreTablesArgs[] = "--ignore-table={$this->dbName}.{$tableName}";
149
                }
150
                $ignoreTablesArg = (count($ignoreTablesArgs) > 0) ? implode(' ', $ignoreTablesArgs) : '';
151
                break;
152
            case 'pgsqldumper';
153
                $ignoreTablesArg = (count($this->ignoreTables) > 0) ? '-T ' . implode(' -T ', $this->ignoreTables) : '';
154
                break;
155
        }
156
157
        return $ignoreTablesArg;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $ignoreTablesArg does not seem to be defined for all execution paths leading up to this point.
Loading history...
158
    }
159
160
    public function prepareCreateTables()
161
    {
162
        switch (strtolower($this->getClassName())) {
163
            case 'mysqldumper':
164
                $createTables = !$this->createTables ? '--no-create-info' : '';
165
                break;
166
            case 'pgsqldumper':
167
                $createTables = (!$this->createTables) ? '--data-only' : '';
168
                break;
169
        }
170
        return $createTables;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $createTables does not seem to be defined for all execution paths leading up to this point.
Loading history...
171
    }
172
173
    public function getClassName()
174
    {
175
        $classWithNamespace = static::class;
176
        $partials           = explode("\\", $classWithNamespace);
177
        $className          = end($partials);
178
        return $className;
179
    }
180
}
181