Passed
Push — master ( 939347...72ceee )
by Mihail
07:40
created

Main   C

Complexity

Total Complexity 19

Size/Duplication

Total Lines 145
Duplicated Lines 14.48 %

Coupling/Cohesion

Components 0
Dependencies 20

Importance

Changes 17
Bugs 3 Features 0
Metric Value
wmc 19
c 17
b 3
f 0
lcom 0
cbo 20
dl 21
loc 145
rs 6.4705

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B actionIndex() 0 27 3
A actionSettings() 21 21 4
A actionFiles() 0 6 1
A actionAntivirus() 0 4 1
A actionDebugcookie() 0 7 1
A actionRouting() 0 8 1
B actionAddroute() 0 15 5
A actionDeleteroute() 0 16 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Apps\Controller\Admin;
4
5
use Apps\Model\Admin\Main\EntityDeleteRoute;
6
use Apps\Model\Admin\Main\FormAddRoute;
7
use Apps\Model\Admin\Main\FormSettings;
8
use Extend\Core\Arch\AdminController;
9
use Ffcms\Core\App;
10
use Ffcms\Core\Exception\SyntaxException;
11
use Ffcms\Core\Helper\Environment;
12
use Ffcms\Core\Helper\FileSystem\Directory;
13
use Ffcms\Core\Helper\FileSystem\File;
14
use Ffcms\Core\Helper\Type\Arr;
15
use Ffcms\Core\Helper\Type\Integer;
16
use Ffcms\Core\Helper\Type\Str;
17
18
class Main extends AdminController
19
{
20
    public $type = 'app';
21
22
    public function __construct()
23
    {
24
        parent::__construct(false);
25
    }
26
27
    /**
28
     * Index page of admin dashboard
29
     */
30
    public function actionIndex()
31
    {
32
        // cache some data
33
        $rootSize = App::$Cache->get('root.size');
34
        if ($rootSize === null) {
35
            $rootSize = round(Directory::getSize('/') / (1024*1000), 2) . ' mb';
36
            App::$Cache->set('root.size', $rootSize, 60 * 60 * 24); // 24 hours caching
37
        }
38
        $loadAvg = App::$Cache->get('load.average');
39
        if ($loadAvg === null) {
40
            $loadAvg = Environment::loadAverage();
41
            App::$Cache->set('load.average', $loadAvg, 60*2); // 2 min cache
42
        }
43
44
        $stats = [
45
            'ff_version' => App::$Properties->version['num'] . ' (' . App::$Properties->version['date'] . ')',
46
            'php_version' => Environment::phpVersion() . ' (' . Environment::phpSAPI() . ')',
47
            'os_name' => Environment::osName(),
48
            'database_name' => App::$Database->connection()->getDatabaseName() . ' (' . App::$Database->connection()->getDriverName() . ')',
49
            'file_size' => $rootSize,
50
            'load_avg' => $loadAvg
51
        ];
52
53
        return App::$View->render('index', [
54
            'stats' => $stats
55
        ]);
56
    }
57
58
    /**
59
     * Manage settings in web
60
     */
61 View Code Duplication
    public function actionSettings()
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...
62
    {
63
        $model = new FormSettings();
64
65
        if ($model->send()) {
66
            if ($model->validate()) {
67
                if ($model->makeSave()) {
68
                    // show message about successful save and take system some time ;)
69
                    return App::$View->render('settings_save');
70
                } else {
71
                    App::$Session->getFlashBag()->add('error', __('Configuration file is not writable! Check /Private/Config/ dir and files'));
72
                }
73
            } else {
74
                App::$Session->getFlashBag()->add('error', __('Validation of form data is failed!'));
75
            }
76
        }
77
78
        return App::$View->render('settings', [
79
            'model' => $model // no $model->export() there
0 ignored issues
show
Unused Code Comprehensibility introduced by
40% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
80
        ]);
81
    }
82
83
    /**
84
     * Manage files via elFinder
85
     */
86
    public function actionFiles()
87
    {
88
        return App::$View->render('files', [
89
            'connector' => App::$Alias->scriptUrl . '/api/main/files?lang=' . App::$Request->getLanguage()
90
        ]);
91
    }
92
93
    /**
94
     * Show antivirus
95
     */
96
    public function actionAntivirus()
97
    {
98
        return App::$View->render('antivirus');
99
    }
100
101
    public function actionDebugcookie()
102
    {
103
        $cookieProperty = App::$Properties->get('debug');
104
        //App::$Request->cookies->add([$cookieProperty['cookie']['key'] => $cookieProperty['cookie']['value']]); todo: fix me
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
105
        setcookie($cookieProperty['cookie']['key'], $cookieProperty['cookie']['value'], Integer::MAX, '/', null, null, true);
106
        App::$Response->redirect('/');
107
    }
108
109
    /**
110
     * List available routes
111
     * @throws \Ffcms\Core\Exception\SyntaxException
112
     */
113
    public function actionRouting()
114
    {
115
        $routingMap = App::$Properties->getAll('Routing');
116
117
        return App::$View->render('routing', [
118
            'routes' => $routingMap
119
        ]);
120
    }
121
122
    /**
123
     * Show add form for routing
124
     * @throws \Ffcms\Core\Exception\SyntaxException
125
     */
126
    public function actionAddroute()
127
    {
128
        $model = new FormAddRoute();
129
130
        if (!File::exist('/Private/Config/Routing.php') || !File::writable('/Private/Config/Routing.php')) {
131
            App::$Session->getFlashBag()->add('error', __('Routing configuration file is not allowed to write: /Private/Config/Routing.php'));
132
        } elseif ($model->send() && $model->validate()) {
133
            $model->save();
134
            App::$Response->redirect('main/routing');
135
        }
136
137
        return App::$View->render('add_route', [
138
            'model' => $model
139
        ]);
140
    }
141
142
    /**
143
     * Delete scheme route
144
     * @throws SyntaxException
145
     */
146
    public function actionDeleteroute()
147
    {
148
        $type = (string)App::$Request->query->get('type');
149
        $loader = (string)App::$Request->query->get('loader');
150
        $source = Str::lowerCase((string)App::$Request->query->get('path'));
151
152
        $model = new EntityDeleteRoute($type, $loader, $source);
153
        if ($model->send()) {
154
            $model->make();
155
            App::$Response->redirect('main/routing');
156
        }
157
158
        return App::$View->render('delete_route', [
159
            'model' => $model
160
        ]);
161
    }
162
}