Completed
Push — master ( 1b0b5c...c3ce6a )
by Sebastian
04:24
created

Mysqldump   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 227
Duplicated Lines 3.08 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 100%

Importance

Changes 13
Bugs 0 Features 4
Metric Value
wmc 7
c 13
b 0
f 4
lcom 1
cbo 10
dl 7
loc 227
ccs 44
cts 44
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setup() 0 16 1
A setupSourceData() 7 7 1
A getExecutable() 0 21 2
A backup() 0 14 2
A simulate() 0 5 1

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\Cli;
5
use phpbu\App\Backup\Source;
6
use phpbu\App\Backup\Target;
7
use phpbu\App\Cli\Executable;
8
use phpbu\App\Exception;
9
use phpbu\App\Result;
10
use phpbu\App\Util;
11
12
/**
13
 * Mysqldump source class.
14
 *
15
 * @package    phpbu
16
 * @subpackage Backup
17
 * @author     Sebastian Feldmann <[email protected]>
18
 * @copyright  Sebastian Feldmann <[email protected]>
19
 * @license    https://opensource.org/licenses/MIT The MIT License (MIT)
20
 * @link       http://phpbu.de/
21
 * @since      Class available since Release 1.0.0
22
 */
23
class Mysqldump extends Cli implements Source, Simulator
24
{
25
    /**
26
     * Path to executable.
27
     *
28
     * @var string
29
     */
30
    private $pathToMysqldump;
31
32
    /**
33
     * Show stdErr
34
     *
35
     * @var boolean
36
     */
37
    private $showStdErr;
38
39
    /**
40
     * Host to connect to
41
     * --host <hostname>
42
     *
43
     * @var string
44
     */
45
    private $host;
46
47
    /**
48
     * User to connect with
49
     * --user <username>
50
     *
51
     * @var string
52
     */
53
    private $user;
54
55
    /**
56
     * Password to authenticate with
57
     * --password <password>
58
     *
59
     * @var string
60
     */
61
    private $password;
62
63
    /**
64
     * List of tables to backup
65
     * --tables array of strings
66
     *
67
     * @var array
68
     */
69
    private $tables;
70
71
    /**
72
     * List of databases to backup
73
     * --databases array of strings
74
     *
75
     * @var array
76
     */
77
    private $databases;
78
79
    /**
80
     * List of tables to ignore
81
     *
82
     * @var array
83
     */
84
    private $ignoreTables;
85
86
    /**
87
     * List of tables where only the table structure is stored
88
     *
89
     * @var array
90
     */
91
    private $structureOnly;
92
93
    /**
94
     * Use mysqldump quick mode
95
     * -q
96
     *
97
     * @var boolean
98
     */
99
    private $quick;
100
101
    /**
102
     *
103
     * Lock tables option
104
     * --lock-tables
105
     *
106
     * @var bool
107
     */
108
    private $lockTables;
109
110
    /**
111
     * Use mysqldump with compression
112
     * -C
113
     *
114
     * @var boolean
115
     */
116
    private $compress;
117
118
    /**
119
     * Use mysqldump with extended insert
120
     * -e
121
     *
122
     * @var boolean
123
     */
124
    private $extendedInsert;
125
126
    /**
127
     * Dump blob fields as hex.
128
     * --hex-blob
129
     *
130
     * @var boolean
131
     */
132
    private $hexBlob;
133
134
    /**
135
     * Dump only table structures
136
     * --no-data
137
     *
138
     * @var boolean
139
     */
140
    private $noData;
141
142
    /**
143
     * Dump target location
144
     * @var string
145
     */
146 5
    private $dumpPathname;
147
148 5
    /**
149
     * Setup.
150 5
     *
151 5
     * @see    \phpbu\App\Backup\Source
152 5
     * @param  array $conf
153 5
     * @throws \phpbu\App\Exception
154 5
     */
155 5
    public function setup(array $conf = [])
156 5
    {
157 5
        $this->setupSourceData($conf);
158 5
159 5
        $this->pathToMysqldump = Util\Arr::getValue($conf, 'pathToMysqldump');
160 5
        $this->host            = Util\Arr::getValue($conf, 'host');
161
        $this->user            = Util\Arr::getValue($conf, 'user');
162
        $this->password        = Util\Arr::getValue($conf, 'password');
163
        $this->showStdErr      = Util\Str::toBoolean(Util\Arr::getValue($conf, 'showStdErr', ''), false);
164
        $this->hexBlob         = Util\Str::toBoolean(Util\Arr::getValue($conf, 'hexBlob', ''), false);
165
        $this->quick           = Util\Str::toBoolean(Util\Arr::getValue($conf, 'quick', ''), false);
166
        $this->lockTables      = Util\Str::toBoolean(Util\Arr::getValue($conf, 'lockTables', ''), false);
167 5
        $this->compress        = Util\Str::toBoolean(Util\Arr::getValue($conf, 'compress', ''), false);
168
        $this->extendedInsert  = Util\Str::toBoolean(Util\Arr::getValue($conf, 'extendedInsert', ''), false);
169 5
        $this->noData          = Util\Str::toBoolean(Util\Arr::getValue($conf, 'noData', ''), false);
170 5
    }
171 5
172 5
    /**
173 5
     * Get tables and databases to backup.
174
     *
175
     * @param array $conf
176
     */
177 View Code Duplication
    protected function setupSourceData(array $conf)
178
    {
179
        $this->tables        = Util\Str::toList(Util\Arr::getValue($conf, 'tables'));
180
        $this->databases     = Util\Str::toList(Util\Arr::getValue($conf, 'databases'));
181
        $this->ignoreTables  = Util\Str::toList(Util\Arr::getValue($conf, 'ignoreTables'));
182
        $this->structureOnly = Util\Str::toList(Util\Arr::getValue($conf, 'structureOnly'));
183
    }
184 2
185
    /**
186
     * (non-PHPDoc)
187 2
     *
188 2
     * @see    \phpbu\App\Backup\Source
189
     * @param  \phpbu\App\Backup\Target $target
190 2
     * @param  \phpbu\App\Result        $result
191
     * @return \phpbu\App\Backup\Source\Status
192 2
     * @throws \phpbu\App\Exception
193 1
     */
194
    public function backup(Target $target, Result $result)
195
    {
196 1
        // setup dump location and execute the dump
197
        $this->dumpPathname = $target->getPathnamePlain();
198
        $mysqldump          = $this->execute($target);
199
200
        $result->debug($mysqldump->getCmd());
201
202
        if (!$mysqldump->wasSuccessful()) {
203
            throw new Exception('mysqldump failed');
204
        }
205 5
206
        return Status::create()->uncompressed($this->dumpPathname);
207 5
    }
208 3
209 3
    /**
210 3
     * Create the Executable to run the mysqldump command.
211 3
     *
212 3
     * @param  \phpbu\App\Backup\Target $target
213 3
     * @return \phpbu\App\Cli\Executable
214 3
     */
215 3
    public function getExecutable(Target $target)
216 3
    {
217 3
        if (null == $this->executable) {
218 3
            $this->executable = new Executable\Mysqldump($this->pathToMysqldump);
219 3
            $this->executable->credentials($this->user, $this->password)
220 3
                             ->useHost($this->host)
221 3
                             ->useQuickMode($this->quick)
222 3
                             ->lockTables($this->lockTables)
223 5
                             ->dumpBlobsHexadecimal($this->hexBlob)
224
                             ->useCompression($this->compress)
225
                             ->useExtendedInsert($this->extendedInsert)
226
                             ->dumpTables($this->tables)
227
                             ->dumpDatabases($this->databases)
228
                             ->ignoreTables($this->ignoreTables)
229
                             ->dumpNoData($this->noData)
230
                             ->dumpStructureOnly($this->structureOnly)
231
                             ->dumpTo($this->dumpPathname)
232
                             ->showStdErr($this->showStdErr);
233
        }
234
        return $this->executable;
235
    }
236
237
    /**
238
     * Simulate the backup execution.
239
     *
240
     * @param  \phpbu\App\Backup\Target $target
241
     * @param  \phpbu\App\Result        $result
242
     * @return \phpbu\App\Backup\Source\Status
243
     */
244
    public function simulate(Target $target, Result $result)
245
    {
246
        $result->debug($this->getExecutable($target)->getCommandLine());
247
248
    }
249
}
250