Completed
Push — master ( cfb253...483bcf )
by CodexShaper
07:39
created

DumperTrait::quoteCommand()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 1
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 2
rs 10
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 112
    public function setDbName(string $name)
45
    {
46 112
        $this->dbName = $name;
47 112
        return $this;
48
    }
49
50 72
    public function setUserName(string $username)
51
    {
52 72
        $this->username = $username;
53
54 72
        return $this;
55
    }
56
57 72
    public function setPassword(string $password)
58
    {
59 72
        $this->password = $password;
60
61 72
        return $this;
62
    }
63
64 4
    public function setHost(string $host)
65
    {
66 4
        $this->host = $host;
67
68 4
        return $this;
69
    }
70
71 6
    public function setPort(int $port)
72
    {
73 6
        $this->port = $port;
74
75 6
        return $this;
76
    }
77
78 4
    public function setSocket(string $socket)
79
    {
80 4
        $this->socket = $socket;
81
82 4
        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 12
    public function setTables($tables)
96
    {
97 12
        if (!empty($this->ignoreTables)) {
98
            throw new \Exception("You can choose only once between tables and ignoreTables at a time");
99
        }
100
101 12
        if (is_string($tables)) {
102 4
            $tables = [$tables];
103
        }
104
105 12
        $this->tables = $tables;
106
107 12
        return $this;
108
    }
109
    /**
110
     * @param string|array $tables
111
     * @throws \Exception
112
     */
113 12
    public function setIgnoreTables($tables)
114
    {
115 12
        if (!empty($this->tables)) {
116 4
            throw new \Exception("You can choose only once between tables and ignoreTables at a time");
117
118
        }
119
120 8
        if (is_string($tables)) {
121 4
            $tables = [$tables];
122
        }
123
124 8
        $this->ignoreTables = $tables;
125
126 8
        return $this;
127
    }
128
129 12
    public function setCommandBinaryPath(string $path)
130
    {
131 12
        $this->commandBinaryPath = $path;
132
133 12
        return $this;
134
    }
135
136 76
    public function setDestinationPath(string $path)
137
    {
138 76
        $this->destinationPath = $path;
139
140 76
        return $this;
141
    }
142
143 32
    public function setRestorePath(string $path)
144
    {
145 32
        $this->restorePath = $path;
146
147 32
        return $this;
148
    }
149
150
    // Compress
151
    public function setCompressBinaryPath(string $path)
152
    {
153
        $this->compressBinaryPath = $path;
154
        return $this;
155
    }
156
157
    public function setCompressCommand(string $command)
158
    {
159
        $this->compressCommand = $command;
160
        $this->isCompress      = true;
161
        return $this;
162
    }
163
164
    public function setCompressExtension(string $extension)
165
    {
166
        $this->compressExtension = $extension;
167
        return $this;
168
    }
169
170 16
    public function useCompress(string $command = "gzip", string $extension = ".gz", string $binary_path = "")
171
    {
172 16
        $this->compressCommand    = $command;
173 16
        $this->compressExtension  = $extension;
174 16
        $this->compressBinaryPath = $binary_path;
175 16
        $this->isCompress         = true;
176
177 16
        return $this;
178
    }
179
180
    public function enableDebug()
181
    {
182
        $this->debug = true;
183
        return $this;
184
    }
185
186
    public function getDbName()
187
    {
188
        return $this->dbName;
189
    }
190
191
    public function getUserName()
192
    {
193
        return $this->username;
194
    }
195
196
    public function getPassword()
197
    {
198
        return $this->password;
199
    }
200
201
    public function getHost()
202
    {
203
        return $this->host;
204
    }
205
206
    public function getPort()
207
    {
208
        return $this->port;
209
    }
210
211
    public function getSocket()
212
    {
213
        return $this->socket;
214
    }
215
216
    public function getTimeOut()
217
    {
218
        return $this->timeout;
219
    }
220
221
    public function getCommandBinaryPath()
222
    {
223
        return $this->commandBinaryPath;
224
    }
225
226
    public function getDestinationPath()
227
    {
228
        return $this->destinationPath;
229
    }
230
231
    public function getRestorePath()
232
    {
233
        return $this->restorePath;
234
    }
235
236 76
    public function getDumpCommand(string $credentialFile = '', $destinationPath = '')
237
    {
238 76
        $destinationPath = !empty($destinationPath) ? $destinationPath : $this->destinationPath;
239 76
        switch (strtolower($this->getDumperClassName())) {
240 76
            case 'mysqldumper':
241 28
                $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

241
                /** @scrutinizer ignore-call */ 
242
                $dumpCommand = $this->prepareDumpCommand($credentialFile, $destinationPath);
Loading history...
242 28
                break;
243
            default:
244 48
                $dumpCommand = $this->prepareDumpCommand($destinationPath);
245 48
                break;
246
        }
247
248 76
        return $this->removeExtraSpaces($dumpCommand);
249
    }
250
251 32
    public function getRestoreCommand(string $credentialFile = '', string $filePath = '')
252
    {
253 32
        $filePath = !empty($filePath) ? '"' . $filePath : $this->restorePath;
254 32
        switch (strtolower($this->getDumperClassName())) {
255 32
            case 'mysqldumper':
256 6
                $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

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