| Conditions | 13 |
| Paths | 168 |
| Total Lines | 123 |
| Code Lines | 77 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 224 | private function backupTables(FileHandler $fileHandler, $tables = '*') |
||
| 225 | { |
||
| 226 | $this->eventDispatcher->notifyEvent('run.backup.process', |
||
| 227 | new Event($this, |
||
| 228 | EventMessage::factory()->addDescription(__u('Copying database'))) |
||
| 229 | ); |
||
| 230 | |||
| 231 | $fileHandler->open('w'); |
||
| 232 | |||
| 233 | $db = $this->dic->get(Database::class); |
||
| 234 | $databaseUtil = $this->dic->get(DatabaseUtil::class); |
||
| 235 | |||
| 236 | $queryData = new QueryData(); |
||
| 237 | |||
| 238 | if ($tables === '*') { |
||
| 239 | $resTables = DatabaseUtil::$tables; |
||
| 240 | } else { |
||
| 241 | $resTables = is_array($tables) ? $tables : explode(',', $tables); |
||
| 242 | } |
||
| 243 | |||
| 244 | $lineSeparator = PHP_EOL . PHP_EOL; |
||
| 245 | |||
| 246 | $dbname = $db->getDbHandler()->getDatabaseName(); |
||
| 247 | |||
| 248 | $sqlOut = '-- ' . PHP_EOL; |
||
| 249 | $sqlOut .= '-- sysPass DB dump generated on ' . time() . ' (START)' . PHP_EOL; |
||
| 250 | $sqlOut .= '-- ' . PHP_EOL; |
||
| 251 | $sqlOut .= '-- Please, do not alter this file, it could break your DB' . PHP_EOL; |
||
| 252 | $sqlOut .= '-- ' . PHP_EOL; |
||
| 253 | $sqlOut .= 'SET AUTOCOMMIT = 0;' . PHP_EOL; |
||
| 254 | $sqlOut .= 'SET FOREIGN_KEY_CHECKS = 0;' . PHP_EOL; |
||
| 255 | $sqlOut .= 'SET UNIQUE_CHECKS = 0;' . PHP_EOL; |
||
| 256 | $sqlOut .= '-- ' . PHP_EOL; |
||
| 257 | $sqlOut .= 'CREATE DATABASE IF NOT EXISTS `' . $dbname . '`;' . PHP_EOL . PHP_EOL; |
||
| 258 | $sqlOut .= 'USE `' . $dbname . '`;' . PHP_EOL . PHP_EOL; |
||
| 259 | |||
| 260 | $fileHandler->write($sqlOut); |
||
| 261 | |||
| 262 | $sqlOutViews = ''; |
||
| 263 | // Recorrer las tablas y almacenar los datos |
||
| 264 | foreach ($resTables as $table) { |
||
| 265 | $tableName = is_object($table) ? $table->{'Tables_in_' . $dbname} : $table; |
||
| 266 | |||
| 267 | $queryData->setQuery('SHOW CREATE TABLE ' . $tableName); |
||
| 268 | |||
| 269 | // Consulta para crear la tabla |
||
| 270 | $txtCreate = $db->doQuery($queryData)->getData(); |
||
| 271 | |||
| 272 | if (isset($txtCreate->{'Create Table'})) { |
||
| 273 | $sqlOut = '-- ' . PHP_EOL; |
||
| 274 | $sqlOut .= '-- Table ' . strtoupper($tableName) . PHP_EOL; |
||
| 275 | $sqlOut .= '-- ' . PHP_EOL; |
||
| 276 | $sqlOut .= 'DROP TABLE IF EXISTS `' . $tableName . '`;' . PHP_EOL . PHP_EOL; |
||
| 277 | $sqlOut .= $txtCreate->{'Create Table'} . ';' . PHP_EOL . PHP_EOL; |
||
| 278 | |||
| 279 | $fileHandler->write($sqlOut); |
||
| 280 | } elseif (isset($txtCreate->{'Create View'})) { |
||
| 281 | $sqlOutViews .= '-- ' . PHP_EOL; |
||
| 282 | $sqlOutViews .= '-- View ' . strtoupper($tableName) . PHP_EOL; |
||
| 283 | $sqlOutViews .= '-- ' . PHP_EOL; |
||
| 284 | $sqlOutViews .= 'DROP TABLE IF EXISTS `' . $tableName . '`;' . PHP_EOL . PHP_EOL; |
||
| 285 | $sqlOutViews .= $txtCreate->{'Create View'} . ';' . PHP_EOL . PHP_EOL; |
||
| 286 | } |
||
| 287 | |||
| 288 | $fileHandler->write($lineSeparator); |
||
| 289 | } |
||
| 290 | |||
| 291 | // Guardar las vistas |
||
| 292 | $fileHandler->write($sqlOutViews); |
||
| 293 | |||
| 294 | // Guardar los datos |
||
| 295 | foreach ($resTables as $tableName) { |
||
| 296 | // No guardar las vistas! |
||
| 297 | if (strrpos($tableName, '_v') !== false) { |
||
| 298 | continue; |
||
| 299 | } |
||
| 300 | |||
| 301 | $queryData->setQuery('SELECT * FROM `' . $tableName . '`'); |
||
| 302 | |||
| 303 | // Consulta para obtener los registros de la tabla |
||
| 304 | $queryRes = $db->doQueryRaw($queryData, [PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL], false); |
||
| 305 | |||
| 306 | $numColumns = $queryRes->columnCount(); |
||
| 307 | |||
| 308 | while ($row = $queryRes->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_NEXT)) { |
||
| 309 | $fileHandler->write('INSERT INTO `' . $tableName . '` VALUES('); |
||
| 310 | |||
| 311 | $field = 1; |
||
| 312 | foreach ($row as $value) { |
||
| 313 | if (is_numeric($value)) { |
||
| 314 | $fileHandler->write($value); |
||
| 315 | } else { |
||
| 316 | $fileHandler->write($databaseUtil->escape($value)); |
||
| 317 | } |
||
| 318 | |||
| 319 | if ($field < $numColumns) { |
||
| 320 | $fileHandler->write(','); |
||
| 321 | } |
||
| 322 | |||
| 323 | $field++; |
||
| 324 | } |
||
| 325 | |||
| 326 | $fileHandler->write(');' . PHP_EOL); |
||
| 327 | } |
||
| 328 | } |
||
| 329 | |||
| 330 | $sqlOut = '-- ' . PHP_EOL; |
||
| 331 | $sqlOut .= 'SET AUTOCOMMIT = 1;' . PHP_EOL; |
||
| 332 | $sqlOut .= 'SET FOREIGN_KEY_CHECKS = 1;' . PHP_EOL; |
||
| 333 | $sqlOut .= 'SET UNIQUE_CHECKS = 1;' . PHP_EOL; |
||
| 334 | $sqlOut .= '-- ' . PHP_EOL; |
||
| 335 | $sqlOut .= '-- sysPass DB dump generated on ' . time() . ' (END)' . PHP_EOL; |
||
| 336 | $sqlOut .= '-- ' . PHP_EOL; |
||
| 337 | $sqlOut .= '-- Please, do not alter this file, it could break your DB' . PHP_EOL; |
||
| 338 | $sqlOut .= '-- ' . PHP_EOL . PHP_EOL; |
||
| 339 | |||
| 340 | $fileHandler->write($sqlOut); |
||
| 341 | $fileHandler->close(); |
||
| 342 | |||
| 343 | $archive = new ArchiveHandler($fileHandler->getFile(), $this->extensionChecker); |
||
| 344 | $archive->compressFile($fileHandler->getFile()); |
||
| 345 | |||
| 346 | $fileHandler->delete(); |
||
| 347 | } |
||
| 411 | } |