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