| Conditions | 13 |
| Paths | 168 |
| Total Lines | 122 |
| Code Lines | 76 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 205 | private function backupTables(FileHandler $fileHandler, $tables = '*') |
||
| 206 | { |
||
| 207 | $this->eventDispatcher->notifyEvent('run.backup.process', |
||
| 208 | new Event($this, |
||
| 209 | EventMessage::factory()->addDescription(__u('Copiando base de datos'))) |
||
| 210 | ); |
||
| 211 | |||
| 212 | $fileHandler->open('w'); |
||
| 213 | |||
| 214 | $db = $this->dic->get(Database::class); |
||
| 215 | |||
| 216 | $queryData = new QueryData(); |
||
| 217 | |||
| 218 | if ($tables === '*') { |
||
| 219 | $resTables = DatabaseUtil::$tables; |
||
| 220 | } else { |
||
| 221 | $resTables = is_array($tables) ? $tables : explode(',', $tables); |
||
| 222 | } |
||
| 223 | |||
| 224 | $lineSeparator = PHP_EOL . PHP_EOL; |
||
| 225 | |||
| 226 | $dbname = $db->getDbHandler()->getDatabaseName(); |
||
| 227 | |||
| 228 | $sqlOut = '-- ' . PHP_EOL; |
||
| 229 | $sqlOut .= '-- sysPass DB dump generated on ' . time() . ' (START)' . PHP_EOL; |
||
| 230 | $sqlOut .= '-- ' . PHP_EOL; |
||
| 231 | $sqlOut .= '-- Please, do not alter this file, it could break your DB' . PHP_EOL; |
||
| 232 | $sqlOut .= '-- ' . PHP_EOL; |
||
| 233 | $sqlOut .= 'SET AUTOCOMMIT = 0;' . PHP_EOL; |
||
| 234 | $sqlOut .= 'SET FOREIGN_KEY_CHECKS = 0;' . PHP_EOL; |
||
| 235 | $sqlOut .= 'SET UNIQUE_CHECKS = 0;' . PHP_EOL; |
||
| 236 | $sqlOut .= '-- ' . PHP_EOL; |
||
| 237 | $sqlOut .= 'CREATE DATABASE IF NOT EXISTS `' . $dbname . '`;' . PHP_EOL . PHP_EOL; |
||
| 238 | $sqlOut .= 'USE `' . $dbname . '`;' . PHP_EOL . PHP_EOL; |
||
| 239 | |||
| 240 | $fileHandler->write($sqlOut); |
||
| 241 | |||
| 242 | $sqlOutViews = ''; |
||
| 243 | // Recorrer las tablas y almacenar los datos |
||
| 244 | foreach ($resTables as $table) { |
||
| 245 | $tableName = is_object($table) ? $table->{'Tables_in_' . $dbname} : $table; |
||
| 246 | |||
| 247 | $queryData->setQuery('SHOW CREATE TABLE ' . $tableName); |
||
| 248 | |||
| 249 | // Consulta para crear la tabla |
||
| 250 | $txtCreate = $db->doQuery($queryData)->getData(); |
||
| 251 | |||
| 252 | if (isset($txtCreate->{'Create Table'})) { |
||
| 253 | $sqlOut = '-- ' . PHP_EOL; |
||
| 254 | $sqlOut .= '-- Table ' . strtoupper($tableName) . PHP_EOL; |
||
| 255 | $sqlOut .= '-- ' . PHP_EOL; |
||
| 256 | $sqlOut .= 'DROP TABLE IF EXISTS `' . $tableName . '`;' . PHP_EOL . PHP_EOL; |
||
| 257 | $sqlOut .= $txtCreate->{'Create Table'} . ';' . PHP_EOL . PHP_EOL; |
||
| 258 | |||
| 259 | $fileHandler->write($sqlOut); |
||
| 260 | } elseif (isset($txtCreate->{'Create View'})) { |
||
| 261 | $sqlOutViews .= '-- ' . PHP_EOL; |
||
| 262 | $sqlOutViews .= '-- View ' . strtoupper($tableName) . PHP_EOL; |
||
| 263 | $sqlOutViews .= '-- ' . PHP_EOL; |
||
| 264 | $sqlOutViews .= 'DROP TABLE IF EXISTS `' . $tableName . '`;' . PHP_EOL . PHP_EOL; |
||
| 265 | $sqlOutViews .= $txtCreate->{'Create View'} . ';' . PHP_EOL . PHP_EOL; |
||
| 266 | } |
||
| 267 | |||
| 268 | $fileHandler->write($lineSeparator); |
||
| 269 | } |
||
| 270 | |||
| 271 | // Guardar las vistas |
||
| 272 | $fileHandler->write($sqlOutViews); |
||
| 273 | |||
| 274 | // Guardar los datos |
||
| 275 | foreach ($resTables as $tableName) { |
||
| 276 | // No guardar las vistas! |
||
| 277 | if (strrpos($tableName, '_v') !== false) { |
||
| 278 | continue; |
||
| 279 | } |
||
| 280 | |||
| 281 | $queryData->setQuery('SELECT * FROM `' . $tableName . '`'); |
||
| 282 | |||
| 283 | // Consulta para obtener los registros de la tabla |
||
| 284 | $queryRes = $db->doQueryRaw($queryData); |
||
| 285 | |||
| 286 | $numColumns = $queryRes->columnCount(); |
||
| 287 | |||
| 288 | while ($row = $queryRes->fetch(\PDO::FETCH_NUM)) { |
||
| 289 | $fileHandler->write('INSERT INTO `' . $tableName . '` VALUES('); |
||
| 290 | |||
| 291 | $field = 1; |
||
| 292 | foreach ($row as $value) { |
||
| 293 | if (is_numeric($value)) { |
||
| 294 | $fileHandler->write($value); |
||
| 295 | } else { |
||
| 296 | $fileHandler->write(DatabaseUtil::escape($value, $db->getDbHandler())); |
||
| 297 | } |
||
| 298 | |||
| 299 | if ($field < $numColumns) { |
||
| 300 | $fileHandler->write(','); |
||
| 301 | } |
||
| 302 | |||
| 303 | $field++; |
||
| 304 | } |
||
| 305 | |||
| 306 | $fileHandler->write(');' . PHP_EOL); |
||
| 307 | } |
||
| 308 | } |
||
| 309 | |||
| 310 | $sqlOut = '-- ' . PHP_EOL; |
||
| 311 | $sqlOut .= 'SET AUTOCOMMIT = 1;' . PHP_EOL; |
||
| 312 | $sqlOut .= 'SET FOREIGN_KEY_CHECKS = 1;' . PHP_EOL; |
||
| 313 | $sqlOut .= 'SET UNIQUE_CHECKS = 1;' . PHP_EOL; |
||
| 314 | $sqlOut .= '-- ' . PHP_EOL; |
||
| 315 | $sqlOut .= '-- sysPass DB dump generated on ' . time() . ' (END)' . PHP_EOL; |
||
| 316 | $sqlOut .= '-- ' . PHP_EOL; |
||
| 317 | $sqlOut .= '-- Please, do not alter this file, it could break your DB' . PHP_EOL; |
||
| 318 | $sqlOut .= '-- ' . PHP_EOL . PHP_EOL; |
||
| 319 | |||
| 320 | $fileHandler->write($sqlOut); |
||
| 321 | $fileHandler->close(); |
||
| 322 | |||
| 323 | $archive = new ArchiveHandler($fileHandler->getFile(), $this->extensionChecker); |
||
| 324 | $archive->compressFile($fileHandler->getFile()); |
||
| 325 | |||
| 326 | $fileHandler->delete(); |
||
| 327 | } |
||
| 390 | } |