GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

ToolsController::databaseBackupAction()   D
last analyzed

Complexity

Conditions 9
Paths 16

Size

Total Lines 100
Code Lines 60

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 100
rs 4.8196
c 0
b 0
f 0
cc 9
eloc 60
nc 16
nop 2

How to fix   Long Method   

Long Method

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:

1
<?php
2
3
namespace Application\Controller\MembersArea;
4
5
use Application\Tool\Helpers;
6
use Silex\Application;
7
use Symfony\Component\HttpFoundation\Request;
8
use Symfony\Component\HttpFoundation\Response;
9
10
/**
11
 * @author Borut Balažek <[email protected]>
12
 */
13
class ToolsController
14
{
15
    /**
16
     * @param Application $app
17
     *
18
     * @return Response
19
     */
20 View Code Duplication
    public function indexAction(Application $app)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
21
    {
22
        if (
23
            !$app['security']->isGranted('ROLE_TOOLS') &&
24
            !$app['security']->isGranted('ROLE_ADMIN')
25
        ) {
26
            $app->abort(403);
27
        }
28
29
        return new Response(
30
            $app['twig']->render(
31
                'contents/members-area/tools/index.html.twig'
32
            )
33
        );
34
    }
35
36
    /**
37
     * @param Request     $request
38
     * @param Application $app
39
     *
40
     * @return Response
41
     */
42
    public function databaseBackupAction(Request $request, Application $app)
43
    {
44
        if (
45
            !$app['security']->isGranted('ROLE_TOOLS') &&
46
            !$app['security']->isGranted('ROLE_ADMIN')
47
        ) {
48
            $app->abort(403);
49
        }
50
51
        $data = array();
52
53
        $backups = array();
54
        $action = $request->query->get('action');
55
        $selectedBackup = $request->query->get('backup');
56
        $backupData = null;
57
        $backupsDirectory = STORAGE_DIR.'/backups/database';
58
59
        if ($action == 'new') {
60
            $username = $app['database_options']['default']['user'];
61
            $password = $app['database_options']['default']['password'];
62
            $database = $app['database_options']['default']['dbname'];
63
            $file = $backupsDirectory.'/'.date('Ymd_His').'_new.sql';
64
65
            shell_exec('mysqldump -u '.$username.' -p'.$password.' '.$database.' > '.$file);
66
67
            $app['flashbag']->add(
68
                'success',
69
                'A new backup was successfully created!'
70
            );
71
72
            return $app->redirect(
73
                $app['url_generator']->generate(
74
                    'members-area.tools.database-backup'
75
                )
76
            );
77
        }
78
79
        if ($action == 'restore') {
80
            $username = $app['database_options']['default']['user'];
81
            $password = $app['database_options']['default']['password'];
82
            $database = $app['database_options']['default']['dbname'];
83
            $file = $backupsDirectory.'/'.$selectedBackup.'.sql';
84
            $fileOld = $backupsDirectory.'/'.date('Ymd_His').'_before_restore.sql';
85
86
            // First create a new backup!
87
            shell_exec('mysqldump -u '.$username.' -p'.$password.' '.$database.' > '.$fileOld);
88
89
            // Then restore it!
90
            shell_exec('mysqldump -u '.$username.' -p'.$password.' '.$database.' < '.$file);
91
92
            $app['flashbag']->add(
93
                'success',
94
                'The database has been successfully restored.'
95
            );
96
97
            return $app->redirect(
98
                $app['url_generator']->generate(
99
                    'members-area.tools.database-backup'
100
                )
101
            );
102
        }
103
104
        $backupsArray = Helpers::rglob($backupsDirectory.'/*.sql');
105
        if (!empty($backupsArray)) {
106
            foreach ($backupsArray as $backupFilePath) {
107
                $backupFileName = str_replace(
108
                    $backupsDirectory.'/',
109
                    '',
110
                    $backupFilePath
111
                );
112
113
                $backups[] = array(
114
                    'name' => $backupFileName,
115
                    'path' => $backupFilePath,
116
                    'size' => filesize($backupFilePath),
117
                );
118
            }
119
120
            $backups = array_reverse($backups);
121
        }
122
123
        if ($selectedBackup) {
124
            $backupData = file_get_contents($backupsDirectory.'/'.$selectedBackup);
125
126
            if (strlen($backupData) > 5000) {
127
                $backupData = substr($backupData, 0, 5000).' ...';
128
            }
129
        }
130
131
        $data['backups'] = $backups;
132
        $data['selectedBackup'] = $selectedBackup;
133
        $data['backupData'] = $backupData;
134
135
        return new Response(
136
            $app['twig']->render(
137
                'contents/members-area/tools/database-backup.html.twig',
138
                $data
139
            )
140
        );
141
    }
142
}
143