Pgdump::usePort()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
namespace phpbu\App\Cli\Executable;
3
4
use phpbu\App\Cli\Executable;
5
use phpbu\App\Exception;
6
use SebastianFeldmann\Cli\CommandLine;
7
use SebastianFeldmann\Cli\Command\Executable as Cmd;
8
9
/**
10
 * Pgdump Executable class.
11
 *
12
 * @package    phpbu
13
 * @subpackage Backup
14
 * @author     Sebastian Feldmann <[email protected]>
15
 * @copyright  Sebastian Feldmann <[email protected]>
16
 * @license    https://opensource.org/licenses/MIT The MIT License (MIT)
17
 * @link       http://phpbu.de/
18
 * @since      Class available since Release 1.0.0
19
 */
20
class Pgdump extends Abstraction implements Executable
21
{
22
    use OptionMasker;
23
24
    /**
25
     * Host to connect to
26
     * --host=<hostname>
27
     *
28
     * @var string
29
     */
30
    private $host;
31
32
    /**
33
     * Port to connect to
34
     * --port=<portnumber>
35
     *
36
     * @var int
37
     */
38
    private $port;
39
40
    /**
41
     * User to connect with
42
     * --user=<username>
43
     *
44
     * @var string
45
     */
46
    private $user;
47
48
    /**
49
     * Password to authenticate with
50
     * --password=<password>
51
     *
52
     * @var string
53
     */
54
    private $password;
55
56
    /**
57
     * Database to dump
58
     * db-name
59
     *
60
     * @var string
61
     */
62
    private $databaseToDump;
63
64
    /**
65
     * List of schmeas to dump
66
     * --schema=<schema>
67
     *
68
     * @var array
69
     */
70
    private $schemasToDump = [];
71
72
    /**
73
     * Exclude Schemas
74
     * --exclude-schema=<schema>
75
     *
76
     * @var array
77
     */
78
    private $schemasToExclude = [];
79
80
    /**
81
     * Tables to dump.
82
     * --table=<table>
83
     *
84
     * @var array
85
     */
86
    private $tablesToDump = [];
87
88
    /**
89
     * List of tables to exclude
90
     * --exclude-table=<table>
91
     *
92
     * @var array
93
     */
94
    private $tablesToExclude = [];
95
96
    /**
97
     * Don't dump the structure
98
     * --data-only
99
     *
100
     * @var boolean
101
     */
102
    private $dataOnly;
103
104
    /**
105
     * Dump only schema definitions.
106
     * --schema-only
107
     *
108
     * @var boolean
109
     */
110
    private $schemaOnly;
111
112
    /**
113
     * Do not dump data for any tables matching the table pattern.
114
     * --exclude-table-data
115
     *
116
     * @var array
117
     */
118
    private $excludeTableData = [];
119
120
    /**
121
     * Add drop statements to the dump.
122
     * --clean
123
     *
124
     * @var boolean
125
     */
126
    private $clean = false;
127
128
    /**
129
     * Encoding of the dump file
130
     * --encoding
131
     *
132
     * @var string
133
     */
134
    private $encoding;
135
136
    /**
137
     * postgreSQL dump format definition
138
     * --format [plain|custom|directory|tar]
139
     *
140
     * @var string
141
     */
142
    private $format;
143
144
    /**
145
     * Allow any user to restore the dump
146
     * --no-owner
147
     *
148
     * @var bool
149
     */
150
    private $noOwner = false;
151
152
    /**
153
     * Prevent dumping of access privileges.
154
     * --no-acl
155
     *
156
     * @var boolean
157
     */
158
    private $noPrivileges;
159
160
    /**
161
     * Do not output commands to select tablespaces.
162
     * --no-tablespaces
163
     *
164
     * @var boolean
165
     */
166
    private $noTablespaces;
167
168
    /**
169
     * File to dump to
170
     * --file
171
     *
172
     * @var string
173
     */
174
    private $file;
175
176
    /**
177
     * List of available output formats
178
     *
179
     * @var array
180
     */
181
    private $availableFormats = [
182
        'p'         => true,
183
        'plain'     => true,
184
        'c'         => true,
185
        'custom'    => true,
186
        'd'         => true,
187
        'directory' => true,
188
        't'         => true,
189
        'tar'       => true,
190
    ];
191
192
    /**
193
     * Constructor.
194
     *
195
     * @param string $path
196
     */
197 24
    public function __construct(string $path = '')
198
    {
199 24
        $this->setup('pg_dump', $path);
200 24
        $this->setMaskCandidates(['password']);
201 24
    }
202
203
    /**
204
     * Set the postgreSQL credentials.
205
     *
206
     * @param  string $user
207
     * @param  string $password
208
     * @return \phpbu\App\Cli\Executable\Pgdump
209
     */
210 5
    public function credentials(string $user = '', string $password = '') : Pgdump
211
    {
212 5
        $this->user     = $user;
213 5
        $this->password = $password;
214 5
        return $this;
215
    }
216
217
    /**
218
     * Set the postgreSQL hostname.
219
     *
220
     * @param  string $host
221
     * @return \phpbu\App\Cli\Executable\Pgdump
222
     */
223 5
    public function useHost(string $host) : Pgdump
224
    {
225 5
        $this->host = $host;
226 5
        return $this;
227
    }
228
229
    /**
230
     * Set the postgreSQL port.
231
     *
232
     * @param  int $port
233
     * @return \phpbu\App\Cli\Executable\Pgdump
234
     */
235 5
    public function usePort(int $port) : Pgdump
236
    {
237 5
        $this->port = $port;
238 5
        return $this;
239
    }
240
241
    /**
242
     * Set database to dump.
243
     *
244
     * @param  string $database
245
     * @return \phpbu\App\Cli\Executable\Pgdump
246
     */
247 21
    public function dumpDatabase(string $database) : Pgdump
248
    {
249 21
        $this->databaseToDump = $database;
250 21
        return $this;
251
    }
252
253
    /**
254
     * Add drop statements to the dump file.
255
     * Works only on format=plain-text.
256
     *
257
     * @param  boolean $bool
258
     * @return \phpbu\App\Cli\Executable\Pgdump
259
     */
260 1
    public function addDropStatements(bool $bool) : Pgdump
261
    {
262 1
        $this->clean = $bool;
263 1
        return $this;
264
    }
265
266
    /**
267
     * Add the --no-owner option so no ownership setting commands will be added.
268
     *
269
     * @param  boolean $bool
270
     * @return \phpbu\App\Cli\Executable\Pgdump
271
     */
272 1
    public function skipOwnerCommands(bool $bool) : Pgdump
273
    {
274 1
        $this->noOwner = $bool;
275 1
        return $this;
276
    }
277
278
    /**
279
     * Set schemas to dump.
280
     *
281
     * @param  array $schemas
282
     * @return \phpbu\App\Cli\Executable\Pgdump
283
     */
284 5
    public function dumpSchemas(array $schemas) : Pgdump
285
    {
286 5
        $this->schemasToDump = $schemas;
287 5
        return $this;
288
    }
289
290
    /**
291
     * Set schemas to exclude.
292
     *
293
     * @param  array $schemas
294
     * @return \phpbu\App\Cli\Executable\Pgdump
295
     */
296 5
    public function excludeSchemas(array $schemas) : Pgdump
297
    {
298 5
        $this->schemasToExclude = $schemas;
299 5
        return $this;
300
    }
301
302
    /**
303
     * Set tables to dump.
304
     *
305
     * @param  array $tables
306
     * @return \phpbu\App\Cli\Executable\Pgdump
307
     */
308 5
    public function dumpTables(array $tables) : Pgdump
309
    {
310 5
        $this->tablesToDump = $tables;
311 5
        return $this;
312
    }
313
314
    /**
315
     * Set tables to ignore.
316
     *
317
     * @param  array $tables
318
     * @return \phpbu\App\Cli\Executable\Pgdump
319
     */
320 5
    public function excludeTables(array $tables) : Pgdump
321
    {
322 5
        $this->tablesToExclude = $tables;
323 5
        return $this;
324
    }
325
326
    /**
327
     * Set tables where no data is dumped.
328
     *
329
     * @param  array $tables
330
     * @return \phpbu\App\Cli\Executable\Pgdump
331
     */
332 5
    public function excludeTableData(array $tables) : Pgdump
333
    {
334 5
        $this->excludeTableData = $tables;
335 5
        return $this;
336
    }
337
338
    /**
339
     * Dump only the schema information.
340
     *
341
     * @param  boolean $bool
342
     * @return \phpbu\App\Cli\Executable\Pgdump
343
     * @throws \phpbu\App\Exception
344
     */
345 7
    public function dumpSchemaOnly(bool $bool) : Pgdump
346
    {
347 7
        if ($this->dataOnly) {
348 1
            throw new Exception('can\'t use schema-only when data-only is used already');
349
        }
350 6
        $this->schemaOnly = $bool;
351 6
        return $this;
352
    }
353
354
    /**
355
     * Dump no schema information.
356
     *
357
     * @param  boolean $bool
358
     * @return \phpbu\App\Cli\Executable\Pgdump
359
     * @throws \phpbu\App\Exception
360
     */
361 7
    public function dumpDataOnly(bool $bool) : Pgdump
362
    {
363 7
        if ($this->schemaOnly) {
364 1
            throw new Exception('can\'t use data-only when schema-only is used already');
365
        }
366 6
        $this->dataOnly = $bool;
367 6
        return $this;
368
    }
369
370
    /**
371
     * Set the dump target path.
372
     *
373
     * @param  string $path
374
     * @return \phpbu\App\Cli\Executable\Pgdump
375
     */
376 21
    public function dumpTo(string $path) : Pgdump
377
    {
378 21
        $this->file = $path;
379 21
        return $this;
380
    }
381
382
    /**
383
     * Set the dump format.
384
     *
385
     * @param  string $format
386
     * @return \phpbu\App\Cli\Executable\Pgdump
387
     * @throws \phpbu\App\Exception
388
     */
389 6
    public function dumpFormat(string $format) : Pgdump
390
    {
391 6
        if (!isset($this->availableFormats[$format])) {
392 1
            throw new Exception('invalid format');
393
        }
394 5
        $this->format = $format;
395 5
        return $this;
396
    }
397
398
    /**
399
     * Do not dump commands setting ownership.
400
     * --no-owner
401
     *
402
     * @param  bool $bool
403
     * @return \phpbu\App\Cli\Executable\Pgdump
404
     */
405 4
    public function dumpNoOwner(bool $bool) : Pgdump
406
    {
407 4
        $this->noOwner = $bool;
408 4
        return $this;
409
    }
410
411
    /**
412
     * Do not output commands to select tablespaces.
413
     * --no-tablespaces
414
     *
415
     * @param  bool $bool
416
     * @return \phpbu\App\Cli\Executable\Pgdump
417
     */
418 1
    public function dumpNoTablespaces(bool $bool) : Pgdump
419
    {
420 1
        $this->noTablespaces = $bool;
421 1
        return $this;
422
    }
423
424
    /**
425
     * Prevent dumping of access privileges.
426
     * --no-acl
427
     *
428
     * @param  boolean $bool
429
     * @return \phpbu\App\Cli\Executable\Pgdump
430
     */
431 5
    public function dumpNoPrivileges(bool $bool) : Pgdump
432
    {
433 5
        $this->noPrivileges = $bool;
434 5
        return $this;
435
    }
436
    /**
437
     * Set the output file encoding.
438
     *
439
     * @param  string $encoding
440
     * @return \phpbu\App\Cli\Executable\Pgdump
441
     */
442 1
    public function encode(string $encoding) : Pgdump
443
    {
444 1
        $this->encoding = $encoding;
445 1
        return $this;
446
    }
447
448
    /**
449
     * Pgdump CommandLine generator.
450
     *
451
     * @return \SebastianFeldmann\Cli\CommandLine
452
     */
453 21
    protected function createCommandLine() : CommandLine
454
    {
455 21
        $process  = new CommandLine();
456 21
        $password = $this->password ? 'PGPASSWORD=' . escapeshellarg($this->password) . ' ' : '';
457 21
        $cmd      = new Cmd($password . $this->binary);
458 21
        $process->addCommand($cmd);
459
460
        // always disable password prompt
461 21
        $cmd->addOption('-w');
462
463 21
        $cmd->addOptionIfNotEmpty('--username', $this->user);
464 21
        $cmd->addOptionIfNotEmpty('--host', $this->host);
465 21
        $cmd->addOptionIfNotEmpty('--port', $this->port);
466 21
        $cmd->addOptionIfNotEmpty('--dbname', $this->databaseToDump);
467 21
        $cmd->addOptionIfNotEmpty('--schema-only', $this->schemaOnly, false);
468 21
        $cmd->addOptionIfNotEmpty('--data-only', $this->dataOnly, false);
469 21
        $cmd->addOptionIfNotEmpty('--clean', $this->clean, false);
470 21
        $cmd->addOptionIfNotEmpty('--no-owner', $this->noOwner, false);
471 21
        $cmd->addOptionIfNotEmpty('--encoding', $this->encoding);
472 21
        $cmd->addOptionIfNotEmpty('--no-tablespaces', $this->noTablespaces, false);
473 21
        $cmd->addOptionIfNotEmpty('--no-acl', $this->noPrivileges, false);
474
475 21
        $this->handleSchemas($cmd);
476 21
        $this->handleTables($cmd);
477
478 21
        $cmd->addOptionIfNotEmpty('--file', $this->file);
479 21
        $cmd->addOptionIfNotEmpty('--format', $this->format);
480 21
        return $process;
481
    }
482
483
    /**
484
     * Handle command schema settings.
485
     *
486
     * @param \SebastianFeldmann\Cli\Command\Executable $cmd
487
     */
488 21
    protected function handleSchemas(Cmd $cmd)
489
    {
490 21
        foreach ($this->schemasToDump as $schema) {
491 1
            $cmd->addOption('--schema', $schema);
492
        }
493
494 21
        foreach ($this->schemasToExclude as $table) {
495 1
            $cmd->addOption('--exclude-schema', $table);
496
        }
497 21
    }
498
499
    /**
500
     * Handle command table settings.
501
     *
502
     * @param \SebastianFeldmann\Cli\Command\Executable $cmd
503
     */
504 21
    protected function handleTables(Cmd $cmd)
505
    {
506 21
        foreach ($this->tablesToDump as $table) {
507 1
            $cmd->addOption('--table', $table);
508
        }
509
510 21
        foreach ($this->tablesToExclude as $table) {
511 1
            $cmd->addOption('--exclude-table', $table);
512
        }
513
514 21
        foreach ($this->excludeTableData as $table) {
515 1
            $cmd->addOption('--exclude-table-data', $table);
516
        }
517 21
    }
518
}
519