DbDumper::setTimeout()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\DbDumper;
4
5
use Spatie\DbDumper\Compressors\Compressor;
6
use Spatie\DbDumper\Compressors\GzipCompressor;
7
use Spatie\DbDumper\Exceptions\CannotSetParameter;
8
use Spatie\DbDumper\Exceptions\DumpFailed;
9
use Symfony\Component\Process\Process;
10
11
abstract class DbDumper
12
{
13
    /** @var string */
14
    protected $dbName;
15
16
    /** @var string */
17
    protected $userName;
18
19
    /** @var string */
20
    protected $password;
21
22
    /** @var string */
23
    protected $host = 'localhost';
24
25
    /** @var int */
26
    protected $port = 5432;
27
28
    /** @var string */
29
    protected $socket = '';
30
31
    /** @var int */
32
    protected $timeout = 0;
33
34
    /** @var string */
35
    protected $dumpBinaryPath = '';
36
37
    /** @var array */
38
    protected $includeTables = [];
39
40
    /** @var array */
41
    protected $excludeTables = [];
42
43
    /** @var array */
44
    protected $extraOptions = [];
45
46
    /** @var array */
47
    protected $extraOptionsAfterDbName = [];
48
49
    /** @var object */
50
    protected $compressor = null;
51
52
    public static function create()
53
    {
54
        return new static();
55
    }
56
57
    public function getDbName(): string
58
    {
59
        return $this->dbName;
60
    }
61
62
    /**
63
     * @param string $dbName
64
     *
65
     * @return $this
66
     */
67
    public function setDbName(string $dbName)
68
    {
69
        $this->dbName = $dbName;
70
71
        return $this;
72
    }
73
74
    /**
75
     * @param string $userName
76
     *
77
     * @return $this
78
     */
79
    public function setUserName(string $userName)
80
    {
81
        $this->userName = $userName;
82
83
        return $this;
84
    }
85
86
    /**
87
     * @param string $password
88
     *
89
     * @return $this
90
     */
91
    public function setPassword(string $password)
92
    {
93
        $this->password = $password;
94
95
        return $this;
96
    }
97
98
    /**
99
     * @param string $host
100
     *
101
     * @return $this
102
     */
103
    public function setHost(string $host)
104
    {
105
        $this->host = $host;
106
107
        return $this;
108
    }
109
110
    public function getHost(): string
111
    {
112
        return $this->host;
113
    }
114
115
    /**
116
     * @param int $port
117
     *
118
     * @return $this
119
     */
120
    public function setPort(int $port)
121
    {
122
        $this->port = $port;
123
124
        return $this;
125
    }
126
127
    /**
128
     * @param string $socket
129
     *
130
     * @return $this
131
     */
132
    public function setSocket(string $socket)
133
    {
134
        $this->socket = $socket;
135
136
        return $this;
137
    }
138
139
    /**
140
     * @param int $timeout
141
     *
142
     * @return $this
143
     */
144
    public function setTimeout(int $timeout)
145
    {
146
        $this->timeout = $timeout;
147
148
        return $this;
149
    }
150
151
    public function setDumpBinaryPath(string $dumpBinaryPath)
152
    {
153
        if ($dumpBinaryPath !== '' && substr($dumpBinaryPath, -1) !== '/') {
154
            $dumpBinaryPath .= '/';
155
        }
156
157
        $this->dumpBinaryPath = $dumpBinaryPath;
158
159
        return $this;
160
    }
161
162
    /**
163
     * @deprecated
164
     *
165
     * @return $this
166
     */
167
    public function enableCompression()
168
    {
169
        $this->compressor = new GzipCompressor();
170
171
        return $this;
172
    }
173
174
    public function getCompressorExtension(): string
175
    {
176
        return $this->compressor->useExtension();
177
    }
178
179
    public function useCompressor(Compressor $compressor)
180
    {
181
        $this->compressor = $compressor;
182
183
        return $this;
184
    }
185
186
    /**
187
     * @param string|array $includeTables
188
     *
189
     * @return $this
190
     *
191
     * @throws \Spatie\DbDumper\Exceptions\CannotSetParameter
192
     */
193 View Code Duplication
    public function includeTables($includeTables)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
194
    {
195
        if (! empty($this->excludeTables)) {
196
            throw CannotSetParameter::conflictingParameters('includeTables', 'excludeTables');
197
        }
198
199
        if (! is_array($includeTables)) {
200
            $includeTables = explode(', ', $includeTables);
201
        }
202
203
        $this->includeTables = $includeTables;
204
205
        return $this;
206
    }
207
208
    /**
209
     * @param string|array $excludeTables
210
     *
211
     * @return $this
212
     *
213
     * @throws \Spatie\DbDumper\Exceptions\CannotSetParameter
214
     */
215 View Code Duplication
    public function excludeTables($excludeTables)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
216
    {
217
        if (! empty($this->includeTables)) {
218
            throw CannotSetParameter::conflictingParameters('excludeTables', 'includeTables');
219
        }
220
221
        if (! is_array($excludeTables)) {
222
            $excludeTables = explode(', ', $excludeTables);
223
        }
224
225
        $this->excludeTables = $excludeTables;
226
227
        return $this;
228
    }
229
230
    /**
231
     * @param string $extraOption
232
     *
233
     * @return $this
234
     */
235
    public function addExtraOption(string $extraOption)
236
    {
237
        if (! empty($extraOption)) {
238
            $this->extraOptions[] = $extraOption;
239
        }
240
241
        return $this;
242
    }
243
244
    /**
245
     * @param string $extraOptionAtEnd
0 ignored issues
show
Bug introduced by
There is no parameter named $extraOptionAtEnd. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
246
     *
247
     * @return $this
248
     */
249
    public function addExtraOptionAfterDbName(string $extraOptionAfterDbName)
250
    {
251
        if (! empty($extraOptionAfterDbName)) {
252
            $this->extraOptionsAfterDbName[] = $extraOptionAfterDbName;
253
        }
254
255
        return $this;
256
    }
257
258
    abstract public function dumpToFile(string $dumpFile);
259
260
    protected function checkIfDumpWasSuccessFul(Process $process, string $outputFile)
261
    {
262
        if (! $process->isSuccessful()) {
263
            throw DumpFailed::processDidNotEndSuccessfully($process);
264
        }
265
266
        if (! file_exists($outputFile)) {
267
            throw DumpFailed::dumpfileWasNotCreated();
268
        }
269
270
        if (filesize($outputFile) === 0) {
271
            throw DumpFailed::dumpfileWasEmpty();
272
        }
273
    }
274
275
    protected function getCompressCommand(string $command, string $dumpFile): string
276
    {
277
        $compressCommand = $this->compressor->useCommand();
278
279
        if ($this->isWindows()) {
280
            return "{$command} | {$compressCommand} > {$dumpFile}";
281
        }
282
283
        return "(((({$command}; echo \$? >&3) | {$compressCommand} > {$dumpFile}) 3>&1) | (read x; exit \$x))";
284
    }
285
286
    protected function echoToFile(string $command, string $dumpFile): string
287
    {
288
        $dumpFile = '"'.addcslashes($dumpFile, '\\"').'"';
289
290
        if ($this->compressor) {
291
            return $this->getCompressCommand($command, $dumpFile);
292
        }
293
294
        return $command.' > '.$dumpFile;
295
    }
296
297
    protected function determineQuote(): string
298
    {
299
        return $this->isWindows() ? '"' : "'";
300
    }
301
302
    protected function isWindows(): bool
303
    {
304
        return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
305
    }
306
}
307