Passed
Push — master ( 8f5376...4142cd )
by Mihail
12:41
created

Main::actionIndex()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 27
rs 8.8571
cc 3
eloc 18
nc 4
nop 0
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';
0 ignored issues
show
Bug introduced by
The method getSize() does not seem to exist on object<Ffcms\Core\Helper\FileSystem\Directory>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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
}