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

DumperTrait::getRestoreCommand()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 9
c 0
b 0
f 0
nc 4
nop 2
dl 0
loc 13
ccs 0
cts 0
cp 0
crap 12
rs 9.9666
1
<?php
2
3
namespace CodexShaper\Dumper\Traits;
4
5
trait DumperTrait
6
{
7
    /*@var string*/
8
    protected $dbName;
9
    /*@var string*/
10
    protected $username;
11
    /*@var string*/
12
    protected $password;
13
    /*@var string*/
14
    protected $host = 'localhost';
15
    /*@var int*/
16
    protected $port = 3306;
17
    /*@var string*/
18
    protected $socket = '';
19
    /*@var string*/
20
    protected $commandBinaryPath = '';
21
    /*@var int*/
22
    protected $timeout = 0;
23
    /*@var array*/
24
    protected $tables = [];
25
    /*@var array*/
26
    protected $ignoreTables = [];
27
    /*@var string*/
28
    protected $destinationPath = 'dump.sql';
29
    /*@var string*/
30
    protected $restorePath = 'dump.sql';
31
    /*@var bool*/
32
    protected $isCompress = false;
33
    /*@var string*/
34
    protected $compressCommand = "gzip";
35
    /*@var string*/
36
    protected $compressBinaryPath = "";
37
    /*@var string*/
38
    protected $compressExtension = ".gz";
39
    /*@var bool*/
40
    protected $debug = false;
41
    /*@var string*/
42
    protected $command = "";
43
44
    public function setDbName(string $name)
45
    {
46 16
        $this->dbName = $name;
47
        return $this;
48 16
    }
49 16
50
    public function setUserName(string $username)
51
    {
52 16
        $this->username = $username;
53
54 16
        return $this;
55
    }
56 16
57
    public function setPassword(string $password)
58
    {
59 16
        $this->password = $password;
60
61 16
        return $this;
62
    }
63 16
64
    public function setHost(string $host)
65
    {
66
        $this->host = $host;
67
68
        return $this;
69
    }
70
71
    public function setPort(int $port)
72
    {
73
        $this->port = $port;
74
75
        return $this;
76
    }
77
78
    public function setSocket(string $socket)
79
    {
80
        $this->socket = $socket;
81
82
        return $this;
83
    }
84
85
    public function setTimeOut(int $timeout)
86
    {
87
        $this->timeout = $timeout;
88
89
        return $this;
90
    }
91
    /**
92
     * @param string|array $tables
93
     * @throws \Exception
94
     */
95
    public function setTables($tables)
96
    {
97
        if (!empty($this->ignoreTables)) {
98
            throw new \Exception("You can choose only once between tables and ignoreTables at a time");
99
        }
100
101
        if (is_string($tables)) {
102
            $tables = [$tables];
103
        }
104
105
        if (!is_array($tables)) {
0 ignored issues
show
introduced by
The condition is_array($tables) is always true.
Loading history...
106
            throw new \Exception("You can pass string or array");
107
        }
108
109
        $this->tables = $tables;
110
111
        return $this;
112
    }
113
    /**
114
     * @param string|array $tables
115
     * @throws \Exception
116
     */
117
    public function setIgnoreTables($tables)
118
    {
119
        if (!empty($this->tables)) {
120
            throw new \Exception("You can choose only once between tables and ignoreTables at a time");
121
122
        }
123
124
        if (is_string($tables)) {
125
            $tables = [$tables];
126
        }
127
128
        if (!is_array($tables)) {
0 ignored issues
show
introduced by
The condition is_array($tables) is always true.
Loading history...
129
            throw new \Exception("You can pass string or array");
130
        }
131
132
        $this->ignoreTables = $tables;
133
134
        return $this;
135
    }
136
137
    public function setCommandBinaryPath(string $path)
138
    {
139 2
        $this->commandBinaryPath = $path;
140
141 2
        return $this;
142
    }
143 2
144
    public function setDestinationPath(string $path)
145
    {
146
        $this->destinationPath = $path;
147
148
        return $this;
149
    }
150
151
    public function setRestorePath(string $path)
152
    {
153
        $this->restorePath = $path;
154
155
        return $this;
156
    }
157
158
    // Compress
159
    public function setCompressBinaryPath(string $path)
160
    {
161
        $this->compressBinaryPath = $path;
162
        return $this;
163
    }
164
165
    public function setCompressCommand(string $command)
166
    {
167
        $this->compressCommand = $command;
168
        $this->isCompress      = true;
169
        return $this;
170
    }
171
172
    public function setCompressExtension(string $extension)
173
    {
174
        $this->compressExtension = $extension;
175
        return $this;
176
    }
177
178
    public function useCompress(string $command = "gzip", string $extension = ".gz", string $binary_path = "")
179
    {
180 4
        $this->compressCommand    = $command;
181
        $this->compressExtension  = $extension;
182 4
        $this->compressBinaryPath = $binary_path;
183 4
        $this->isCompress         = true;
184 4
185 4
        return $this;
186
    }
187 4
188
    public function enableDebug()
189
    {
190
        $this->debug = true;
191
        return $this;
192
    }
193
194
    public function getDbName()
195
    {
196
        return $this->dbName;
197
    }
198
199
    public function getUserName()
200
    {
201
        return $this->username;
202
    }
203
204
    public function getPassword()
205
    {
206
        return $this->password;
207
    }
208
209
    public function getHost()
210
    {
211
        return $this->host;
212
    }
213
214
    public function getPort()
215
    {
216
        return $this->port;
217
    }
218
219
    public function getSocket()
220
    {
221
        return $this->socket;
222
    }
223
224
    public function getTimeOut()
225
    {
226
        return $this->timeout;
227
    }
228
229
    public function getCommandBinaryPath()
230
    {
231
        return $this->commandBinaryPath;
232
    }
233
234
    public function getDestinationPath()
235
    {
236
        return $this->destinationPath;
237
    }
238
239
    public function getRestorePath()
240
    {
241
        return $this->restorePath;
242
    }
243
244
    public function getDumpCommand(string $credentialFile = '', $destinationPath = '')
245
    {
246 16
        $destinationPath = !empty($destinationPath) ? $destinationPath : $this->destinationPath;
247
        switch (strtolower($this->getClassName())) {
0 ignored issues
show
Bug introduced by
It seems like getClassName() 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

247
        switch (strtolower($this->/** @scrutinizer ignore-call */ getClassName())) {
Loading history...
248 16
            case 'mysqldumper':
249
                $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

249
                /** @scrutinizer ignore-call */ 
250
                $dumpCommand = $this->prepareDumpCommand($credentialFile, $destinationPath);
Loading history...
250
                break;
251 16
            default:
252
                $dumpCommand = $this->prepareDumpCommand($destinationPath);
253 16
                break;
254
        }
255
256 16
        return $this->removeExtraSpaces($dumpCommand);
257
    }
258 16
259
    public function getRestoreCommand(string $credentialFile = '', string $filePath = '')
260
    {
261
        $filePath = !empty($filePath) ? '"' . $filePath : $this->restorePath;
262
        switch (strtolower($this->getClassName())) {
263
            case 'mysqldumper':
264
                $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

264
                /** @scrutinizer ignore-call */ 
265
                $restoreCommand = $this->prepareRestoreCommand($credentialFile, $filePath);
Loading history...
265
                break;
266
            default:
267
                $restoreCommand = $this->prepareRestoreCommand($filePath);
268
                break;
269
        }
270
271
        return $this->removeExtraSpaces($restoreCommand);
272
    }
273
274
    public function removeExtraSpaces(string $str)
275
    {
276
        return preg_replace('/\s+/', ' ', $str);
277
    }
278
279
    public static function isWindows()
280
    {
281
        return strcasecmp(substr(PHP_OS, 0, 3), 'WIN') == 0 ? true : false;
282
    }
283
284
    public function quoteCommand(string $command)
285
    {
286
        return static::isWindows() ? "\"{$command}\"" : "'{$command}'";
287
    }
288
}
289