Completed
Push — master ( 4c81c5...bcd560 )
by CodexShaper
02:04
created

Dumper::getDumperClassName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 6
ccs 0
cts 0
cp 0
crap 2
rs 10
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->getDumperClassName())) {
66
            case 'pgsqldumper':
67
                return ($this->socket !== '') ? $this->socket : $this->host;
68
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
69
            case 'mongodumper';
70
                return !empty($this->host) ? "--host {$this->host}" : "";
71
                break;
72
        }
73
        return $host;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $host seems to be never defined.
Loading history...
74
    }
75
76
    public function preparePort()
77
    {
78
        switch (strtolower($this->getDumperClassName())) {
79
            case 'pgsqldumper':
80
                return !empty($this->port) ? '-p ' . $this->port : '';
81
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
82
            case 'mongodumper':
83
                return !empty($this->port) ? "--port {$this->port}" : "";
84
                break;
85
        }
86
    }
87
88
    public function prepareSocket()
89
    {
90
        switch (strtolower($this->getDumperClassName())) {
91
            case 'mysqldumper':
92
                return ($this->socket !== '') ? "--socket={$this->socket}" : '';
93
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
94
        }
95
    }
96
97
    public function prepareDatabase()
98
    {
99
        switch (strtolower($this->getDumperClassName())) {
100
            case 'mysqldumper':
101
            case 'pgsqldumper':
102
                return !empty($this->dbName) ? $this->dbName : "";
103
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
104
            case 'mongodumper';
105
                return !empty($this->dbName) ? "--db {$this->dbName}" : "";
106
                break;
107
        }
108
    }
109
110
    public function prepareUserName()
111
    {
112
        switch (strtolower($this->getDumperClassName())) {
113
            case 'pgsqldumper':
114
                return !empty($this->username) ? $this->username : "";
115
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
116
            case 'mongodumper';
117
                return !empty($this->username) ? "--username {$this->username}" : "";
118
                break;
119
        }
120
    }
121
122
    public function prepareIncludeTables()
123
    {
124
        switch (strtolower($this->getDumperClassName())) {
125
            case 'mysqldumper':
126
                $includeTables = (count($this->tables) > 0) ? implode(' ', $this->tables) : '';
127
                return !empty($includeTables) ? "--tables {$includeTables}" : '';
128
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
129
            case 'pgsqldumper':
130
                return (count($this->tables) > 0) ? '-t ' . implode(' -t ', $this->tables) : "";
131
                break;
132
        }
133
    }
134
135
    public function prepareIgnoreTables()
136
    {
137
        switch (strtolower($this->getDumperClassName())) {
138
            case 'mysqldumper':
139
                $ignoreTablesArgs = [];
140
                foreach ($this->ignoreTables as $tableName) {
141
                    $ignoreTablesArgs[] = "--ignore-table={$this->dbName}.{$tableName}";
142
                }
143
                return (count($ignoreTablesArgs) > 0) ? implode(' ', $ignoreTablesArgs) : '';
144
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
145
            case 'pgsqldumper';
146
                return (count($this->ignoreTables) > 0) ? '-T ' . implode(' -T ', $this->ignoreTables) : '';
147
                break;
148
        }
149
    }
150
151
    public function prepareCreateTables()
152
    {
153
        switch (strtolower($this->getDumperClassName())) {
154
            case 'mysqldumper':
155
                return !$this->createTables ? '--no-create-info' : '';
156
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
157
            case 'pgsqldumper':
158
                return (!$this->createTables) ? '--data-only' : '';
159
                break;
160
        }
161
        return $createTables;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $createTables seems to be never defined.
Loading history...
162
    }
163
164
    public function getDumperClassName()
165
    {
166
        $classWithNamespace = static::class;
167
        $partials           = explode("\\", $classWithNamespace);
168
        $className          = end($partials);
169
        return $className;
170
    }
171
}
172