Completed
Push — master ( d26594...3115e1 )
by CodexShaper
02:12
created

Dumper::getDumpCommand()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 4
nop 2
dl 0
loc 13
ccs 5
cts 5
cp 1
crap 3
rs 9.9666
c 0
b 0
f 0
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->getClassName())) {
0 ignored issues
show
Bug introduced by
The method getClassName() does not exist on CodexShaper\Dumper\Dumper. ( Ignorable by Annotation )

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

65
        switch (strtolower($this->/** @scrutinizer ignore-call */ getClassName())) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
66 58
            case 'pgsqldumper':
67 30
                $host = ($this->socket !== '') ? $this->socket : $this->host;
68 30
                break;
69 28
            case 'mongodumper';
70 28
                $host = !empty($this->host) ? "--host {$this->host}" : "";
71 28
                break;
72
            default:
73
                $host = $this->host;
74
                break;
75
        }
76 58
        return $host;
77
    }
78
79 58
    public function preparePort()
80
    {
81 58
        switch (strtolower($this->getClassName())) {
82 58
            case 'pgsqldumper':
83 30
                $port = !empty($this->port) ? '-p ' . $this->port : '';
84 30
                break;
85 28
            case 'mongodumper':
86 28
                $port = !empty($this->port) ? "--port {$this->port}" : "";
87 28
                break;
88
            default:
89
                $port = $this->port;
90
                break;
91
        }
92 58
        return $port;
93
    }
94
95 28
    public function prepareSocket()
96
    {
97 28
        switch (strtolower($this->getClassName())) {
98 28
            case 'mysqldumper':
99 28
                $socket = ($this->socket !== '') ? "--socket={$this->socket}" : '';
100 28
                break;
101
            default:
102
                $socket = $this->socket;
103
                break;
104
        }
105
106 28
        return $socket;
107
    }
108
109 80
    public function prepareDatabase()
110
    {
111 80
        switch (strtolower($this->getClassName())) {
112 80
            case 'mysqldumper':
113 46
            case 'pgsqldumper':
114 64
                $database = !empty($this->dbName) ? $this->dbName : "";
115 64
                break;
116 16
            case 'mongodumper';
117 16
                $database = !empty($this->dbName) ? "--db {$this->dbName}" : "";
118 16
                break;
119
            default:
120
                $database = $this->dbName;
121
                break;
122
        }
123 80
        return $database;
124
    }
125
126 58
    public function prepareUserName()
127
    {
128 58
        switch (strtolower($this->getClassName())) {
129 58
            case 'pgsqldumper':
130 30
                $username = !empty($this->username) ? $this->username : "";
131 30
                break;
132 28
            case 'mongodumper';
133 28
                $username = !empty($this->username) ? "--username {$this->username}" : "";
134 28
                break;
135
            default:
136
                $username = $this->username;
137
                break;
138
        }
139 58
        return $username;
140
    }
141
142 52
    public function prepareIncludeTables()
143
    {
144 52
        switch (strtolower($this->getClassName())) {
145 52
            case 'mysqldumper':
146 28
                $includeTables    = (count($this->tables) > 0) ? implode(' ', $this->tables) : '';
147 28
                $includeTablesArg = !empty($includeTables) ? "--tables {$includeTables}" : '';
148 28
                break;
149 24
            case 'pgsqldumper':
150 24
                $includeTablesArg = (count($this->tables) > 0) ? '-t ' . implode(' -t ', $this->tables) : "";
151 24
                break;
152
            default:
153
                $includeTablesArg = $this->tables;
154
                break;
155
        }
156
157 52
        return $includeTablesArg;
158
    }
159
160 52
    public function prepareIgnoreTables()
161
    {
162 52
        switch (strtolower($this->getClassName())) {
163 52
            case 'mysqldumper':
164 28
                $ignoreTablesArgs = [];
165 28
                foreach ($this->ignoreTables as $tableName) {
166 4
                    $ignoreTablesArgs[] = "--ignore-table={$this->dbName}.{$tableName}";
167
                }
168 28
                $ignoreTablesArg = (count($ignoreTablesArgs) > 0) ? implode(' ', $ignoreTablesArgs) : '';
169 28
                break;
170 24
            case 'pgsqldumper';
171 24
                $ignoreTablesArg = (count($this->ignoreTables) > 0) ? '-T ' . implode(' -T ', $this->ignoreTables) : '';
172 24
                break;
173
            default:
174
                $ignoreTablesArg = $this->ignoreTables;
175
                break;
176
        }
177
178 52
        return $ignoreTablesArg;
179
    }
180
181 52
    public function prepareCreateTables()
182
    {
183 52
        switch (strtolower($this->getClassName())) {
184 52
            case 'mysqldumper':
185 28
                $createTables = !$this->createTables ? '--no-create-info' : '';
186 28
                break;
187 24
            case 'pgsqldumper':
188 24
                $createTables = (!$this->createTables) ? '--data-only' : '';
189 24
                break;
190
            default:
191
                $createTables = $this->createTables;
192
                break;
193
        }
194 52
        return $createTables;
195
    }
196
197 92
    public function getDumpCommand(string $credentialFile = '', $destinationPath = '')
198
    {
199 92
        $destinationPath = !empty($destinationPath) ? $destinationPath : $this->destinationPath;
200 92
        switch (strtolower($this->getDumperClassName())) {
201 92
            case 'mysqldumper':
202 92
                $dumpCommand = $this->prepareDumpCommand($credentialFile, $destinationPath);
0 ignored issues
show
Bug introduced by
The method prepareDumpCommand() does not exist on CodexShaper\Dumper\Dumper. Since it exists in all sub-types, consider adding an abstract or default implementation to CodexShaper\Dumper\Dumper. ( Ignorable by Annotation )

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

202
                /** @scrutinizer ignore-call */ 
203
                $dumpCommand = $this->prepareDumpCommand($credentialFile, $destinationPath);
Loading history...
203
                break;
204
            default:
205
                $dumpCommand = $this->prepareDumpCommand($destinationPath);
206
                break;
207
        }
208
209
        return $this->removeExtraSpaces($dumpCommand);
210
    }
211
212
    public function getRestoreCommand(string $credentialFile = '', string $filePath = '')
213
    {
214
        $filePath = !empty($filePath) ? '"' . $filePath : $this->restorePath;
215
        switch (strtolower($this->getDumperClassName())) {
216
            case 'mysqldumper':
217
                $restoreCommand = $this->prepareRestoreCommand($credentialFile, $filePath);
0 ignored issues
show
Bug introduced by
The method prepareRestoreCommand() does not exist on CodexShaper\Dumper\Dumper. Did you maybe mean prepareProcessCommand()? ( Ignorable by Annotation )

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

217
                /** @scrutinizer ignore-call */ 
218
                $restoreCommand = $this->prepareRestoreCommand($credentialFile, $filePath);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
218
                break;
219
            default:
220
                $restoreCommand = $this->prepareRestoreCommand($filePath);
221
                break;
222
        }
223
224
        return $this->removeExtraSpaces($restoreCommand);
225
    }
226
227
    public function removeExtraSpaces(string $str)
228
    {
229
        return preg_replace('/\s+/', ' ', $str);
230
    }
231
232
    public static function isWindows()
233
    {
234
        return strcasecmp(substr(PHP_OS, 0, 3), 'WIN') == 0 ? true : false;
235
    }
236
237
    public function quoteCommand(string $command)
238
    {
239
        return static::isWindows() ? "\"{$command}\"" : "'{$command}'";
240
    }
241
242
    public function getDumperClassName()
243
    {
244
        $classWithNamespace = static::class;
245
        $partials           = explode("\\", $classWithNamespace);
246
        $className          = end($partials);
247
        return $className;
248
    }
249
}
250