Completed
Push — master ( 98a3b7...d114fb )
by Freek
02:07
created

DbDumper::enableCompression()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 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\Exceptions\CannotSetParameter;
8
9
abstract class DbDumper
10
{
11
    /** @var string */
12
    protected $dbName;
13
14
    /** @var string */
15
    protected $userName;
16
17
    /** @var string */
18
    protected $password;
19
20
    /** @var string */
21
    protected $host = 'localhost';
22
23
    /** @var int */
24
    protected $port = 5432;
25
26
    /** @var string */
27
    protected $socket = '';
28
29
    /** @var int */
30
    protected $timeout = 0;
31
32
    /** @var string */
33
    protected $dumpBinaryPath = '';
34
35
    /** @var array */
36
    protected $includeTables = [];
37
38
    /** @var array */
39
    protected $excludeTables = [];
40
41
    /** @var array */
42
    protected $extraOptions = [];
43
44
    /** @var bool */
45
    protected $enableCompression = false;
46
47
    public static function create()
48
    {
49
        return new static();
50
    }
51
52
    public function getDbName(): string
53
    {
54
        return $this->dbName;
55
    }
56
57
    /**
58
     * @param string $dbName
59
     *
60
     * @return $this
61
     */
62
    public function setDbName(string $dbName)
63
    {
64
        $this->dbName = $dbName;
65
66
        return $this;
67
    }
68
69
    /**
70
     * @param string $userName
71
     *
72
     * @return $this
73
     */
74
    public function setUserName(string $userName)
75
    {
76
        $this->userName = $userName;
77
78
        return $this;
79
    }
80
81
    /**
82
     * @param string $password
83
     *
84
     * @return $this
85
     */
86
    public function setPassword(string $password)
87
    {
88
        $this->password = $password;
89
90
        return $this;
91
    }
92
93
    /**
94
     * @param string $host
95
     *
96
     * @return $this
97
     */
98
    public function setHost(string $host)
99
    {
100
        $this->host = $host;
101
102
        return $this;
103
    }
104
105
    public function getHost(): string
106
    {
107
        return $this->host;
108
    }
109
110
    /**
111
     * @param int $port
112
     *
113
     * @return $this
114
     */
115
    public function setPort(int $port)
116
    {
117
        $this->port = $port;
118
119
        return $this;
120
    }
121
122
    /**
123
     * @param string $socket
124
     *
125
     * @return $this
126
     */
127
    public function setSocket(string $socket)
128
    {
129
        $this->socket = $socket;
130
131
        return $this;
132
    }
133
134
    /**
135
     * @param int $timeout
136
     *
137
     * @return $this
138
     */
139
    public function setTimeout(int $timeout)
140
    {
141
        $this->timeout = $timeout;
142
143
        return $this;
144
    }
145
146
    /**
147
     * @param string $dumpBinaryPath
148
     *
149
     * @return $this
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
     * @param string|array $includeTables
164
     *
165
     * @return $this
166
     *
167
     * @throws \Spatie\DbDumper\Exceptions\CannotSetParameter
168
     */
169 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...
170
    {
171
        if (! empty($this->excludeTables)) {
172
            throw CannotSetParameter::conflictingParameters('includeTables', 'excludeTables');
173
        }
174
175
        if (! is_array($includeTables)) {
176
            $includeTables = explode(', ', $includeTables);
177
        }
178
179
        $this->includeTables = $includeTables;
180
181
        return $this;
182
    }
183
184
    /**
185
     * @param string|array $excludeTables
186
     *
187
     * @return $this
188
     *
189
     * @throws \Spatie\DbDumper\Exceptions\CannotSetParameter
190
     */
191 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...
192
    {
193
        if (! empty($this->includeTables)) {
194
            throw CannotSetParameter::conflictingParameters('excludeTables', 'includeTables');
195
        }
196
197
        if (! is_array($excludeTables)) {
198
            $excludeTables = explode(', ', $excludeTables);
199
        }
200
201
        $this->excludeTables = $excludeTables;
202
203
        return $this;
204
    }
205
206
    /**
207
     * @param string $extraOption
208
     *
209
     * @return $this
210
     */
211
    public function addExtraOption(string $extraOption)
212
    {
213
        if (! empty($extraOption)) {
214
            $this->extraOptions[] = $extraOption;
215
        }
216
217
        return $this;
218
    }
219
220
    /**
221
     * @return $this
222
     */
223
    public function enableCompression()
224
    {
225
        $this->enableCompression = true;
226
227
        return $this;
228
    }
229
230
    abstract public function dumpToFile(string $dumpFile);
231
232
    protected function checkIfDumpWasSuccessFul(Process $process, string $outputFile)
233
    {
234
        if (! $process->isSuccessful()) {
235
            throw DumpFailed::processDidNotEndSuccessfully($process);
236
        }
237
238
        if (! file_exists($outputFile)) {
239
            throw DumpFailed::dumpfileWasNotCreated();
240
        }
241
242
        if (filesize($outputFile) === 0) {
243
            throw DumpFailed::dumpfileWasEmpty();
244
        }
245
    }
246
247
    /**
248
     * @param string $command
249
     *
250
     * @return string
251
     */
252
    protected function echoToFile(string $command, string $dumpFile)
253
    {
254
        $compression = $this->enableCompression ? ' | gzip' : '';
255
256
        return $command . $compression . ' > ' . $dumpFile;
257
    }
258
}
259