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.
Completed
Push — develop ( 9e9d51...fc7292 )
by Borut
03:13
created

ToolsController::databaseBackupAction()   D

Complexity

Conditions 9
Paths 16

Size

Total Lines 100
Code Lines 60

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 100
rs 4.8196
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 Request     $request
17
     * @param Application $app
18
     *
19
     * @return Response
20
     */
21
    public function indexAction(Request $request, Application $app)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
22
    {
23
        if (
24
            !$app['security']->isGranted('ROLE_TOOLS') &&
25
            !$app['security']->isGranted('ROLE_ADMIN')
26
        ) {
27
            $app->abort(403);
28
        }
29
30
        return new Response(
31
            $app['twig']->render(
32
                'contents/members-area/tools/index.html.twig'
33
            )
34
        );
35
    }
36
37
    /**
38
     * @param Request     $request
39
     * @param Application $app
40
     *
41
     * @return Response
42
     */
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