Completed
Push — master ( 7c0fe2...a2c4bb )
by Sebastian
09:03
created

Pgdump::setupConnection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 7
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 7
loc 7
rs 9.4285
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
namespace phpbu\App\Backup\Source;
3
4
use phpbu\App\Backup\Target;
5
use phpbu\App\Cli\Executable;
6
use phpbu\App\Exception;
7
use phpbu\App\Result;
8
use phpbu\App\Util;
9
10
/**
11
 * Pgdump source class.
12
 *
13
 * @package    phpbu
14
 * @subpackage Backup
15
 * @author     Sebastian Feldmann <[email protected]>
16
 * @copyright  Sebastian Feldmann <[email protected]>
17
 * @license    https://opensource.org/licenses/MIT The MIT License (MIT)
18
 * @link       http://phpbu.de/
19
 * @since      Class available since Release 3.0.0
20
 */
21
class Pgdump extends SimulatorExecutable implements Simulator
22
{
23
    /**
24
     * pg_dump default format.
25
     *
26
     * @var string
27
     */
28
    const DEFAULT_FORMAT = 'p';
29
30
    /**
31
     * Path to executable.
32
     *
33
     * @var string
34
     */
35
    private $pathToPgdump;
36
37
    /**
38
     * Host to connect to
39
     * --host=<hostname>
40
     *
41
     * @var string
42
     */
43
    private $host;
44
45
    /**
46
     * Port to connect to
47
     * --port=<portnumber>
48
     *
49
     * @var string
50
     */
51
    private $port;
52
53
    /**
54
     * User to connect with
55
     * --user=<username>
56
     *
57
     * @var string
58
     */
59
    private $user;
60
61
    /**
62
     * Password to authenticate with
63
     * PGPASSWORD=<password>
64
     *
65
     * @var string
66
     */
67
    private $password;
68
69
    /**
70
     * List of databases to backup
71
     * --dbname string
72
     *
73
     * @var string
74
     */
75
    private $database;
76
77
    /**
78
     * List of schemas to backup
79
     * --schema=<schema> array of strings
80
     *
81
     * @var array
82
     */
83
    private $schemas;
84
85
    /**
86
     * List of schemas to ignore
87
     * --exclude-schema=<schema>
88
     *
89
     * @var array
90
     */
91
    private $excludeSchemas;
92
93
    /**
94
     * List of tables to backup
95
     * --table=<table> array of strings
96
     *
97
     * @var array
98
     */
99
    private $tables;
100
101
    /**
102
     * List of tables to ignore
103
     * --exclude-table=<table>
104
     *
105
     * @var array
106
     */
107
    private $excludeTables;
108
109
    /**
110
     * List of tables where only the table structure is stored
111
     *
112
     * @var array
113
     */
114
    private $excludeTableData;
115
116
    /**
117
     * Dump encoding
118
     * --encoding
119
     *
120
     * @var string
121
     */
122
    private $encoding;
123
124
    /**
125
     * Add drop statements.
126
     * --clean
127
     *
128
     * @var boolean
129
     */
130
    private $clean;
131
132
    /**
133
     * Dump no owner statement
134
     * --no-owner
135
     *
136
     * @var bool
137
     */
138
    private $noOwner;
139
140
    /**
141
     * Dump format
142
     * --format=<format>
143
     *
144
     * @var bool
145
     */
146
    private $format;
147
148
    /**
149
     * Dump only table structures
150
     * --schema-only
151
     *
152
     * @var bool
153
     */
154
    private $schemaOnly;
155
156
    /**
157
     * Dump only table data
158
     * --data-only
159
     *
160
     * @var bool
161
     */
162
    private $dataOnly;
163
164
    /**
165
     * Dump no privilege data
166
     * --no-acl
167
     *
168
     * @var bool
169
     */
170
    private $noPrivileges;
171
172
    /**
173
     * Setup.
174
     *
175
     * @see    \phpbu\App\Backup\Source
176
     * @param  array $conf
177
     * @throws \phpbu\App\Exception
178
     */
179
    public function setup(array $conf = [])
180
    {
181
        $this->pathToPgdump = Util\Arr::getValue($conf, 'pathToPgdump');
182
183
        $this->setupSourceData($conf);
184
        $this->setupConnection($conf);
185
        $this->setupDumpOptions($conf);
186
    }
187
188
    /**
189
     * Setup connection settings.
190
     *
191
     * @param array $conf
192
     */
193 View Code Duplication
    private function setupConnection(array $conf)
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
        $this->host     = Util\Arr::getValue($conf, 'host');
196
        $this->port     = Util\Arr::getValue($conf, 'port');
197
        $this->user     = Util\Arr::getValue($conf, 'user');
198
        $this->password = Util\Arr::getValue($conf, 'password');
199
    }
200
201
    /**
202
     * Get tables and databases to backup.
203
     *
204
     * @param array $conf
205
     */
206
    private function setupSourceData(array $conf)
207
    {
208
        $this->database         = Util\Arr::getValue($conf, 'database');
209
        $this->tables           = Util\Str::toList(Util\Arr::getValue($conf, 'tables'));
210
        $this->excludeTables    = Util\Str::toList(Util\Arr::getValue($conf, 'ignoreTables'));
211
        $this->schemas          = Util\Str::toList(Util\Arr::getValue($conf, 'schemas'));
212
        $this->excludeSchemas   = Util\Str::toList(Util\Arr::getValue($conf, 'ignoreTables'));
213
        $this->excludeTableData = Util\Str::toList(Util\Arr::getValue($conf, 'excludeTableData'));
214
    }
215
216
    /**
217
     * Setup some dump options.
218
     *
219
     * @param array $conf
220
     */
221
    private function setupDumpOptions(array $conf)
222
    {
223
        $this->clean        = Util\Str::toBoolean(Util\Arr::getValue($conf, 'clean', ''), false);
224
        $this->noPrivileges = Util\Str::toBoolean(Util\Arr::getValue($conf, 'noPrivileges', ''), false);
225
        $this->schemaOnly   = Util\Str::toBoolean(Util\Arr::getValue($conf, 'schemaOnly', ''), false);
226
        $this->dataOnly     = Util\Str::toBoolean(Util\Arr::getValue($conf, 'dataOnly', ''), false);
227
        $this->noOwner      = Util\Str::toBoolean(Util\Arr::getValue($conf, 'noOwner', ''), false);
228
        $this->encoding     = Util\Arr::getValue($conf, 'encoding');
229
        $this->format       = Util\Arr::getValue($conf, 'format', self::DEFAULT_FORMAT);
230
    }
231
232
233
    /**
234
     * Execute the backup.
235
     *
236
     * @see    \phpbu\App\Backup\Source
237
     * @param  \phpbu\App\Backup\Target $target
238
     * @param  \phpbu\App\Result        $result
239
     * @return \phpbu\App\Backup\Source\Status
240
     * @throws \phpbu\App\Exception
241
     */
242
    public function backup(Target $target, Result $result)
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...
243
    {
244
245
        $pgDump = $this->execute($target);
246
        $result->debug($pgDump->getCmd());
247
248
        if (!$pgDump->wasSuccessful()) {
249
            throw new Exception('mysqldump failed:' . $pgDump->getStdErr());
250
        }
251
252
        return $this->createStatus($target);
253
    }
254
255
    /**
256
     * Create the Executable to run the mysqldump command.
257
     *
258
     * @param  \phpbu\App\Backup\Target $target
259
     * @return \phpbu\App\Cli\Executable
260
     */
261
    public function getExecutable(Target $target)
262
    {
263
        if (null == $this->executable) {
264
            $this->executable = new Executable\Pgdump($this->pathToPgdump);
265
            $this->executable->credentials($this->user, $this->password)
266
                             ->useHost($this->host)
267
                             ->usePort($this->port)
268
                             ->dumpDatabase($this->database)
269
                             ->dumpSchemas($this->schemas)
270
                             ->excludeSchemas($this->excludeSchemas)
271
                             ->dumpTables($this->tables)
272
                             ->excludeTables($this->excludeTables)
273
                             ->excludeTableData($this->excludeTableData)
274
                             ->dumpSchemaOnly($this->schemaOnly)
275
                             ->dumpDataOnly($this->dataOnly)
276
                             ->dumpNoPrivileges($this->noPrivileges)
277
                             ->dumpNoOwner($this->noOwner)
278
                             ->dumpFormat($this->format)
0 ignored issues
show
Documentation introduced by
$this->format is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
279
                             ->dumpTo($target->getPathnamePlain());
280
        }
281
        return $this->executable;
282
    }
283
284
    /**
285
     * Create backup status.
286
     *
287
     * @param  \phpbu\App\Backup\Target
288
     * @return \phpbu\App\Backup\Source\Status
289
     */
290
    protected function createStatus(Target $target)
291
    {
292
        return Status::create()->uncompressed($target->getPathnamePlain());
293
    }
294
}
295