Completed
Pull Request — master (#81)
by
unknown
01:35
created

DbDumper::excludeTables()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 14
loc 14
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\DbDumper;
4
5
use Symfony\Component\Process\Process;
6
use Spatie\DbDumper\Exceptions\DumpFailed;
7
use Spatie\DbDumper\Compressors\GzipCompressor;
8
use Spatie\DbDumper\Compressors\Bzip2Compressor;
9
use Spatie\DbDumper\Exceptions\CannotSetParameter;
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 string */
47
    protected $compressor = null;
48
49
    public static function create()
50
    {
51
        return new static();
52
    }
53
54
    public function getDbName(): string
55
    {
56
        return $this->dbName;
57
    }
58
59
    /**
60
     * @param string $dbName
61
     *
62
     * @return $this
63
     */
64
    public function setDbName(string $dbName)
65
    {
66
        $this->dbName = $dbName;
67
68
        return $this;
69
    }
70
71
    /**
72
     * @param string $userName
73
     *
74
     * @return $this
75
     */
76
    public function setUserName(string $userName)
77
    {
78
        $this->userName = $userName;
79
80
        return $this;
81
    }
82
83
    /**
84
     * @param string $password
85
     *
86
     * @return $this
87
     */
88
    public function setPassword(string $password)
89
    {
90
        $this->password = $password;
91
92
        return $this;
93
    }
94
95
    /**
96
     * @param string $host
97
     *
98
     * @return $this
99
     */
100
    public function setHost(string $host)
101
    {
102
        $this->host = $host;
103
104
        return $this;
105
    }
106
107
    public function getHost(): string
108
    {
109
        return $this->host;
110
    }
111
112
    /**
113
     * @param int $port
114
     *
115
     * @return $this
116
     */
117
    public function setPort(int $port)
118
    {
119
        $this->port = $port;
120
121
        return $this;
122
    }
123
124
    /**
125
     * @param string $socket
126
     *
127
     * @return $this
128
     */
129
    public function setSocket(string $socket)
130
    {
131
        $this->socket = $socket;
132
133
        return $this;
134
    }
135
136
    /**
137
     * @param int $timeout
138
     *
139
     * @return $this
140
     */
141
    public function setTimeout(int $timeout)
142
    {
143
        $this->timeout = $timeout;
144
145
        return $this;
146
    }
147
148
    /**
149
     * @param string $dumpBinaryPath
150
     *
151
     * @return $this
152
     */
153
    public function setDumpBinaryPath(string $dumpBinaryPath)
154
    {
155
        if ($dumpBinaryPath !== '' && substr($dumpBinaryPath, -1) !== '/') {
156
            $dumpBinaryPath .= '/';
157
        }
158
159
        $this->dumpBinaryPath = $dumpBinaryPath;
160
161
        return $this;
162
    }
163
164
    public function enableCompression()
165
    {
166
        return $this->GzipCompression();
167
    }
168
169
    /**
170
     * @param string $level
171
     *
172
     * @return $this
173
     *
174
     */
175
    public function GzipCompression(string $level = '9')
176
    {
177
        $this->compressor = new GzipCompressor;
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Spatie\DbDumper\Compressors\GzipCompressor() of type object<Spatie\DbDumper\C...ressors\GzipCompressor> is incompatible with the declared type string of property $compressor.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
178
179
        $this->compressor->setLevel($level);
180
181
        return $this;
182
    }
183
184
    /**
185
     * @param string $level
186
     *
187
     * @return $this
188
     *
189
     */
190
    public function Bzip2Compression(string $level = '9')
191
    {
192
        $this->compressor = new Bzip2Compressor;
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Spatie\DbDumper\Compressors\Bzip2Compressor() of type object<Spatie\DbDumper\C...essors\Bzip2Compressor> is incompatible with the declared type string of property $compressor.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
193
194
        $this->compressor->setLevel($level);
195
196
        return $this;
197
    }
198
199
    public function getCompressorExtension()
200
    {
201
        return $this->compressor->getExtension();
0 ignored issues
show
Bug introduced by
The method getExtension cannot be called on $this->compressor (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
202
    }
203
204
    /**
205
     * @param string|array $includeTables
206
     *
207
     * @return $this
208
     *
209
     * @throws \Spatie\DbDumper\Exceptions\CannotSetParameter
210
     */
211 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...
212
    {
213
        if (! empty($this->excludeTables)) {
214
            throw CannotSetParameter::conflictingParameters('includeTables', 'excludeTables');
215
        }
216
217
        if (! is_array($includeTables)) {
218
            $includeTables = explode(', ', $includeTables);
219
        }
220
221
        $this->includeTables = $includeTables;
222
223
        return $this;
224
    }
225
226
    /**
227
     * @param string|array $excludeTables
228
     *
229
     * @return $this
230
     *
231
     * @throws \Spatie\DbDumper\Exceptions\CannotSetParameter
232
     */
233 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...
234
    {
235
        if (! empty($this->includeTables)) {
236
            throw CannotSetParameter::conflictingParameters('excludeTables', 'includeTables');
237
        }
238
239
        if (! is_array($excludeTables)) {
240
            $excludeTables = explode(', ', $excludeTables);
241
        }
242
243
        $this->excludeTables = $excludeTables;
244
245
        return $this;
246
    }
247
248
    /**
249
     * @param string $extraOption
250
     *
251
     * @return $this
252
     */
253
    public function addExtraOption(string $extraOption)
254
    {
255
        if (! empty($extraOption)) {
256
            $this->extraOptions[] = $extraOption;
257
        }
258
259
        return $this;
260
    }
261
262
    abstract public function dumpToFile(string $dumpFile);
263
264
    protected function checkIfDumpWasSuccessFul(Process $process, string $outputFile)
265
    {
266
        if (! $process->isSuccessful()) {
267
            throw DumpFailed::processDidNotEndSuccessfully($process);
268
        }
269
270
        if (! file_exists($outputFile)) {
271
            throw DumpFailed::dumpfileWasNotCreated();
272
        }
273
274
        if (filesize($outputFile) === 0) {
275
            throw DumpFailed::dumpfileWasEmpty();
276
        }
277
    }
278
279
    protected function echoToFile(string $command, string $dumpFile): string
280
    {
281
        $compression = $this->compressor ? ' | ' . $this->compressor->getCompressorCommand() : '';
0 ignored issues
show
Bug introduced by
The method getCompressorCommand cannot be called on $this->compressor (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
282
        $dumpFile = '"'.addcslashes($dumpFile, '\\"').'"';
283
284
        return $command.$compression.' > '.$dumpFile;
285
    }
286
}
287