Completed
Push — master ( 19376a...bc803d )
by Sebastian
03:39
created

Mysqldump   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 268
Duplicated Lines 2.61 %

Coupling/Cohesion

Components 1
Dependencies 12

Test Coverage

Coverage 100%

Importance

Changes 11
Bugs 1 Features 3
Metric Value
wmc 19
c 11
b 1
f 3
lcom 1
cbo 12
dl 7
loc 268
ccs 44
cts 44
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A backup() 0 19 4
A setup() 0 21 3
A setupSourceData() 7 7 1
B getExecutable() 0 25 3
A createStatus() 0 16 3
A isHandlingCompression() 0 4 3
A getDumpTarget() 0 4 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
 * Mysqldump 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 1.0.0
20
 */
21
class Mysqldump extends SimulatorExecutable implements Simulator
22
{
23
    /**
24
     * Path to executable.
25
     *
26
     * @var string
27
     */
28
    private $pathToMysqldump;
29
30
    /**
31
     * Host to connect to
32
     * --host <hostname>
33
     *
34
     * @var string
35
     */
36
    private $host;
37
38
    /**
39
     * User to connect with
40
     * --user <username>
41
     *
42
     * @var string
43
     */
44
    private $user;
45
46
    /**
47
     * Password to authenticate with
48
     * --password <password>
49
     *
50
     * @var string
51
     */
52
    private $password;
53
54
    /**
55
     * List of tables to backup
56
     * --tables array of strings
57
     *
58
     * @var array
59
     */
60
    private $tables;
61
62
    /**
63
     * List of databases to backup
64
     * --databases array of strings
65
     *
66
     * @var array
67
     */
68
    private $databases;
69
70
    /**
71
     * List of tables to ignore
72
     *
73
     * @var array
74
     */
75
    private $ignoreTables;
76
77
    /**
78
     * List of tables where only the table structure is stored
79
     *
80
     * @var array
81
     */
82
    private $structureOnly;
83
84
    /**
85
     * Table separated data files
86
     * --tab
87
     *
88
     * @var boolean
89
     */
90
    private $filePerTable;
91
92
    /**
93
     * Use mysqldump quick mode
94
     * -q
95
     *
96
     * @var boolean
97
     */
98
    private $quick;
99
100
    /**
101
     *
102
     * Lock tables option
103
     * --lock-tables
104
     *
105
     * @var bool
106
     */
107
    private $lockTables;
108
109
    /**
110
     * Use mysqldump with compression
111
     * -C
112
     *
113
     * @var boolean
114
     */
115
    private $compress;
116
117
    /**
118
     * Use mysqldump with extended insert
119
     * -e
120
     *
121
     * @var boolean
122
     */
123
    private $extendedInsert;
124
125
    /**
126
     * Dump blob fields as hex.
127
     * --hex-blob
128
     *
129
     * @var boolean
130
     */
131
    private $hexBlob;
132
133
    /**
134
     * Dump only table structures
135
     * --no-data
136
     *
137
     * @var boolean
138
     */
139
    private $noData;
140
141
    /**
142
     * Setup.
143
     *
144
     * @see    \phpbu\App\Backup\Source
145
     * @param  array $conf
146 5
     * @throws \phpbu\App\Exception
147
     */
148 5
    public function setup(array $conf = [])
149
    {
150 5
        $this->setupSourceData($conf);
151 5
152 5
        $this->pathToMysqldump = Util\Arr::getValue($conf, 'pathToMysqldump');
153 5
        $this->host            = Util\Arr::getValue($conf, 'host');
154 5
        $this->user            = Util\Arr::getValue($conf, 'user');
155 5
        $this->password        = Util\Arr::getValue($conf, 'password');
156 5
        $this->hexBlob         = Util\Str::toBoolean(Util\Arr::getValue($conf, 'hexBlob', ''), false);
157 5
        $this->quick           = Util\Str::toBoolean(Util\Arr::getValue($conf, 'quick', ''), false);
158 5
        $this->lockTables      = Util\Str::toBoolean(Util\Arr::getValue($conf, 'lockTables', ''), false);
159 5
        $this->compress        = Util\Str::toBoolean(Util\Arr::getValue($conf, 'compress', ''), false);
160 5
        $this->extendedInsert  = Util\Str::toBoolean(Util\Arr::getValue($conf, 'extendedInsert', ''), false);
161
        $this->noData          = Util\Str::toBoolean(Util\Arr::getValue($conf, 'noData', ''), false);
162
        $this->filePerTable    = Util\Str::toBoolean(Util\Arr::getValue($conf, 'filePerTable', ''), false);
163
164
        // this doesn't fail, but it doesn't work, so throw an exception so the user understands
165
        if ($this->filePerTable && count($this->structureOnly)) {
166
            throw new Exception('\'structureOnly\' can not be used with the \'filePerTable\' option');
167 5
        }
168
    }
169 5
170 5
    /**
171 5
     * Get tables and databases to backup.
172 5
     *
173 5
     * @param array $conf
174
     */
175 View Code Duplication
    protected function setupSourceData(array $conf)
176
    {
177
        $this->tables        = Util\Str::toList(Util\Arr::getValue($conf, 'tables'));
178
        $this->databases     = Util\Str::toList(Util\Arr::getValue($conf, 'databases'));
179
        $this->ignoreTables  = Util\Str::toList(Util\Arr::getValue($conf, 'ignoreTables'));
180
        $this->structureOnly = Util\Str::toList(Util\Arr::getValue($conf, 'structureOnly'));
181
    }
182
183
    /**
184 2
     * Execute the backup.
185
     *
186
     * @see    \phpbu\App\Backup\Source
187 2
     * @param  \phpbu\App\Backup\Target $target
188 2
     * @param  \phpbu\App\Result        $result
189
     * @return \phpbu\App\Backup\Source\Status
190 2
     * @throws \phpbu\App\Exception
191
     */
192 2
    public function backup(Target $target, Result $result)
193 1
    {
194
        // create the writable dump directory for tables files
195
        if ($this->filePerTable && !is_dir($this->getDumpTarget($target))) {
196 1
            $old = umask(0);
197
            mkdir($this->getDumpTarget($target), 0777, true);
198
            umask($old);
199
        }
200
201
        $mysqldump = $this->execute($target);
202
203
        $result->debug($this->getExecutable($target)->getCommandLinePrintable());
204
205 5
        if (!$mysqldump->wasSuccessful()) {
206
            throw new Exception('mysqldump failed:' . $mysqldump->getStdErr());
207 5
        }
208 3
209 3
        return $this->createStatus($target);
210 3
    }
211 3
212 3
    /**
213 3
     * Create the Executable to run the mysqldump command.
214 3
     *
215 3
     * @param  \phpbu\App\Backup\Target $target
216 3
     * @return \phpbu\App\Cli\Executable
217 3
     */
218 3
    public function getExecutable(Target $target)
219 3
    {
220 3
        if (null == $this->executable) {
221 3
            $this->executable = new Executable\Mysqldump($this->pathToMysqldump);
222 3
            $this->executable->credentials($this->user, $this->password)
223 5
                             ->useHost($this->host)
224
                             ->useQuickMode($this->quick)
225
                             ->lockTables($this->lockTables)
226
                             ->dumpBlobsHexadecimal($this->hexBlob)
227
                             ->useCompression($this->compress)
228
                             ->useExtendedInsert($this->extendedInsert)
229
                             ->dumpTables($this->tables)
230
                             ->dumpDatabases($this->databases)
231
                             ->ignoreTables($this->ignoreTables)
232
                             ->produceFilePerTable($this->filePerTable)
233
                             ->dumpNoData($this->noData)
234
                             ->dumpStructureOnly($this->structureOnly)
235
                             ->dumpTo($this->getDumpTarget($target));
236
            // if compression is active and commands can be piped
237
            if ($this->isHandlingCompression($target)) {
238
                $this->executable->compressOutput($target->getCompression());
239
            }
240
        }
241
        return $this->executable;
242
    }
243
244
    /**
245
     * Create backup status.
246
     *
247
     * @param  \phpbu\App\Backup\Target
248
     * @return \phpbu\App\Backup\Source\Status
249
     */
250
    protected function createStatus(Target $target)
251
    {
252
        // file_per_table creates a directory with all the files
253
        if ($this->filePerTable) {
254
            return Status::create()->uncompressedDirectory($this->getDumpTarget($target));
255
        }
256
257
        // if compression is active and commands can be piped
258
        // compression is handled via pipe
259
        if ($this->isHandlingCompression($target)) {
260
            return Status::create();
261
        }
262
263
        // default create uncompressed dump file
264
        return Status::create()->uncompressedFile($this->getDumpTarget($target));
265
    }
266
267
    /**
268
     * Cann compression be handled via pipe operator.
269
     *
270
     * @param  \phpbu\App\Backup\Target $target
271
     * @return bool
272
     */
273
    private function isHandlingCompression(Target $target)
274
    {
275
        return $target->shouldBeCompressed() && Util\Cli::canPipe() && $target->getCompression()->isPipeable();
276
    }
277
278
    /**
279
     * Return dump target path.
280
     *
281
     * @param  \phpbu\App\Backup\Target $target
282
     * @return string
283
     */
284
    private function getDumpTarget(Target $target)
285
    {
286
        return $target->getPathnamePlain() . ($this->filePerTable ? '.dump' : '');
287
    }
288
}
289