| Conditions | 9 |
| Paths | 16 |
| Total Lines | 100 |
| Code Lines | 60 |
| 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 |
||
| 43 | public function databaseBackupAction(Request $request, Application $app) |
||
| 44 | { |
||
| 45 | if ( |
||
| 46 | !$app['security']->isGranted('ROLE_TOOLS') && |
||
| 47 | !$app['security']->isGranted('ROLE_ADMIN') |
||
| 48 | ) { |
||
| 49 | $app->abort(403); |
||
| 50 | } |
||
| 51 | |||
| 52 | $data = array(); |
||
| 53 | |||
| 54 | $backups = array(); |
||
| 55 | $action = $request->query->get('action'); |
||
| 56 | $selectedBackup = $request->query->get('backup'); |
||
| 57 | $backupData = null; |
||
| 58 | $backupsDirectory = STORAGE_DIR.'/backups/database'; |
||
| 59 | |||
| 60 | if ($action == 'new') { |
||
| 61 | $username = $app['database_options']['default']['user']; |
||
| 62 | $password = $app['database_options']['default']['password']; |
||
| 63 | $database = $app['database_options']['default']['dbname']; |
||
| 64 | $file = $backupsDirectory.'/'.date('Ymd_His').'_new.sql'; |
||
| 65 | |||
| 66 | shell_exec('mysqldump -u '.$username.' -p'.$password.' '.$database.' > '.$file); |
||
| 67 | |||
| 68 | $app['flashbag']->add( |
||
| 69 | 'success', |
||
| 70 | 'A new backup was successfully created!' |
||
| 71 | ); |
||
| 72 | |||
| 73 | return $app->redirect( |
||
| 74 | $app['url_generator']->generate( |
||
| 75 | 'members-area.tools.database-backup' |
||
| 76 | ) |
||
| 77 | ); |
||
| 78 | } |
||
| 79 | |||
| 80 | if ($action == 'restore') { |
||
| 81 | $username = $app['database_options']['default']['user']; |
||
| 82 | $password = $app['database_options']['default']['password']; |
||
| 83 | $database = $app['database_options']['default']['dbname']; |
||
| 84 | $file = $backupsDirectory.'/'.$selectedBackup.'.sql'; |
||
| 85 | $fileOld = $backupsDirectory.'/'.date('Ymd_His').'_before_restore.sql'; |
||
| 86 | |||
| 87 | // First create a new backup! |
||
| 88 | shell_exec('mysqldump -u '.$username.' -p'.$password.' '.$database.' > '.$fileOld); |
||
| 89 | |||
| 90 | // Then restore it! |
||
| 91 | shell_exec('mysqldump -u '.$username.' -p'.$password.' '.$database.' < '.$file); |
||
| 92 | |||
| 93 | $app['flashbag']->add( |
||
| 94 | 'success', |
||
| 95 | 'The database has been successfully restored.' |
||
| 96 | ); |
||
| 97 | |||
| 98 | return $app->redirect( |
||
| 99 | $app['url_generator']->generate( |
||
| 100 | 'members-area.tools.database-backup' |
||
| 101 | ) |
||
| 102 | ); |
||
| 103 | } |
||
| 104 | |||
| 105 | $backupsArray = Helpers::rglob($backupsDirectory.'/*.sql'); |
||
| 106 | if (!empty($backupsArray)) { |
||
| 107 | foreach ($backupsArray as $backupFilePath) { |
||
| 108 | $backupFileName = str_replace( |
||
| 109 | $backupsDirectory.'/', |
||
| 110 | '', |
||
| 111 | $backupFilePath |
||
| 112 | ); |
||
| 113 | |||
| 114 | $backups[] = array( |
||
| 115 | 'name' => $backupFileName, |
||
| 116 | 'path' => $backupFilePath, |
||
| 117 | 'size' => filesize($backupFilePath), |
||
| 118 | ); |
||
| 119 | } |
||
| 120 | |||
| 121 | $backups = array_reverse($backups); |
||
| 122 | } |
||
| 123 | |||
| 124 | if ($selectedBackup) { |
||
| 125 | $backupData = file_get_contents($backupsDirectory.'/'.$selectedBackup); |
||
| 126 | |||
| 127 | if (strlen($backupData) > 5000) { |
||
| 128 | $backupData = substr($backupData, 0, 5000).' ...'; |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | $data['backups'] = $backups; |
||
| 133 | $data['selectedBackup'] = $selectedBackup; |
||
| 134 | $data['backupData'] = $backupData; |
||
| 135 | |||
| 136 | return new Response( |
||
| 137 | $app['twig']->render( |
||
| 138 | 'contents/members-area/tools/database-backup.html.twig', |
||
| 139 | $data |
||
| 140 | ) |
||
| 141 | ); |
||
| 142 | } |
||
| 143 | } |
||
| 144 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.