Completed
Push — master ( bcd560...9ae212 )
by CodexShaper
02:01
created

Dumper::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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 118
    public function __construct(array $options = [])
15
    {
16 118
        foreach ($options as $option => $value) {
17
            if (property_exists($this, $option)) {
18
                $this->{$option} = $value;
19
            }
20
        }
21 118
    }
22
    /**
23
     * @return $this
24
     */
25 118
    public static function create(array $options = [])
26
    {
27 118
        return new static($options);
28
    }
29
    /**
30
     * @return \Symfony\Component\Process\Process
31
     */
32
    protected function prepareProcessCommand()
33
    {
34
        $process = Process::fromShellCommandline($this->command);
35
        $process->setTimeout($this->timeout);
36
        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 58
    public function prepareHost()
64
    {
65 58
        switch (strtolower($this->getDumperClassName())) {
66 58
            case 'pgsqldumper':
67 30
                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 28
            case 'mongodumper';
70 28
                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 58
    public function preparePort()
77
    {
78 58
        switch (strtolower($this->getDumperClassName())) {
79 58
            case 'pgsqldumper':
80 30
                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 28
            case 'mongodumper':
83 28
                return !empty($this->port) ? "--port {$this->port}" : "";
84
                break;
85
        }
86
    }
87
88 28
    public function prepareSocket()
89
    {
90 28
        switch (strtolower($this->getDumperClassName())) {
91 28
            case 'mysqldumper':
92 28
                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 80
    public function prepareDatabase()
98
    {
99 80
        switch (strtolower($this->getDumperClassName())) {
100 80
            case 'mysqldumper':
101 46
            case 'pgsqldumper':
102 64
                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 16
            case 'mongodumper';
105 16
                return !empty($this->dbName) ? "--db {$this->dbName}" : "";
106
                break;
107
        }
108
    }
109
110 58
    public function prepareUserName()
111
    {
112 58
        switch (strtolower($this->getDumperClassName())) {
113 58
            case 'pgsqldumper':
114 30
                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 28
            case 'mongodumper';
117 28
                return !empty($this->username) ? "--username {$this->username}" : "";
118
                break;
119
        }
120
    }
121
122 52
    public function prepareIncludeTables()
123
    {
124 52
        switch (strtolower($this->getDumperClassName())) {
125 52
            case 'mysqldumper':
126 28
                $includeTables = (count($this->tables) > 0) ? implode(' ', $this->tables) : '';
127 28
                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 24
            case 'pgsqldumper':
130 24
                return (count($this->tables) > 0) ? '-t ' . implode(' -t ', $this->tables) : "";
131
                break;
132
        }
133
    }
134
135 52
    public function prepareIgnoreTables()
136
    {
137 52
        switch (strtolower($this->getDumperClassName())) {
138 52
            case 'mysqldumper':
139 28
                $ignoreTablesArgs = [];
140 28
                foreach ($this->ignoreTables as $tableName) {
141 4
                    $ignoreTablesArgs[] = "--ignore-table={$this->dbName}.{$tableName}";
142
                }
143 28
                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 24
            case 'pgsqldumper';
146 24
                return (count($this->ignoreTables) > 0) ? '-T ' . implode(' -T ', $this->ignoreTables) : '';
147
                break;
148
        }
149
    }
150
151 52
    public function prepareCreateTables()
152
    {
153 52
        switch (strtolower($this->getDumperClassName())) {
154 52
            case 'mysqldumper':
155 28
                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 24
            case 'pgsqldumper':
158 24
                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 108
    public function getDumperClassName()
165
    {
166 108
        $classWithNamespace = static::class;
167 108
        $partials           = explode("\\", $classWithNamespace);
168 108
        $className          = end($partials);
169 108
        return $className;
170
    }
171
}
172