Passed
Push — master ( 67ec34...2ff9ea )
by Mihail
05:25
created

Main::actionSettings()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 12

Duplication

Lines 22
Ratio 100 %

Importance

Changes 7
Bugs 1 Features 0
Metric Value
cc 4
eloc 12
c 7
b 1
f 0
nc 4
nop 0
dl 22
loc 22
rs 8.9197
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
     * @return string
30
     * @throws \Ffcms\Core\Exception\SyntaxException
31
     * @throws \Ffcms\Core\Exception\NativeException
32
     */
33
    public function actionIndex()
34
    {
35
        // cache some data
36
        $rootSize = App::$Cache->get('root.size');
37
        if ($rootSize === null) {
38
            $rootSize = round(Directory::getSize('/') / (1024*1000), 2) . ' mb';
39
            App::$Cache->set('root.size', $rootSize, 86400); // 24 hours caching = 60 * 60 * 24
40
        }
41
        $loadAvg = App::$Cache->get('load.average');
42
        if ($loadAvg === null) {
43
            $loadAvg = Environment::loadAverage();
44
            App::$Cache->set('load.average', $loadAvg, 60*5); // 5 min cache
45
        }
46
47
        // prepare system statistic
48
        $stats = [
49
            'ff_version' => App::$Properties->version['num'] . ' (' . App::$Properties->version['date'] . ')',
50
            'php_version' => Environment::phpVersion() . ' (' . Environment::phpSAPI() . ')',
51
            'os_name' => Environment::osName(),
52
            'database_name' => App::$Database->connection()->getDatabaseName() . ' (' . App::$Database->connection()->getDriverName() . ')',
53
            'file_size' => $rootSize,
54
            'load_avg' => $loadAvg
55
        ];
56
57
        // render view output
58
        return App::$View->render('index', [
59
            'stats' => $stats
60
        ]);
61
    }
62
63
    /**
64
     * Manage settings in web
65
     * @return string
66
     * @throws \Ffcms\Core\Exception\SyntaxException
67
     * @throws \Ffcms\Core\Exception\NativeException
68
     */
69 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...
70
    {
71
        // init settings model and process post send
72
        $model = new FormSettings();
73
        if ($model->send()) {
74
            if ($model->validate()) {
75
                if ($model->makeSave()) {
76
                    // show message about successful save and take system some time ;)
77
                    return App::$View->render('settings_save');
78
                } else {
79
                    App::$Session->getFlashBag()->add('error', __('Configuration file is not writable! Check /Private/Config/ dir and files'));
80
                }
81
            } else {
82
                App::$Session->getFlashBag()->add('error', __('Validation of form data is failed!'));
83
            }
84
        }
85
86
        // render output view
87
        return App::$View->render('settings', [
88
            'model' => $model->filter()
0 ignored issues
show
Bug introduced by
The method filter() does not seem to exist on object<Apps\Model\Admin\Main\FormSettings>.

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...
89
        ]);
90
    }
91
92
    /**
93
     * Manage files via elFinder
94
     * @return string
95
     * @throws \Ffcms\Core\Exception\SyntaxException
96
     * @throws \Ffcms\Core\Exception\NativeException
97
     */
98
    public function actionFiles()
99
    {
100
        return App::$View->render('files', [
101
            'connector' => App::$Alias->scriptUrl . '/api/main/files?lang=' . App::$Request->getLanguage()
102
        ]);
103
    }
104
105
    /**
106
     * Show antivirus view
107
     * @return string
108
     * @throws \Ffcms\Core\Exception\SyntaxException
109
     * @throws \Ffcms\Core\Exception\NativeException
110
     */
111
    public function actionAntivirus()
112
    {
113
        return App::$View->render('antivirus');
114
    }
115
116
    /**
117
     * Set debugging cookie to current user session
118
     */
119
    public function actionDebugcookie()
120
    {
121
        $cookieProperty = App::$Properties->get('debug');
122
        //App::$Request->cookies->add([$cookieProperty['cookie']['key'] => $cookieProperty['cookie']['value']]); todo: fix me
0 ignored issues
show
Unused Code Comprehensibility introduced by
69% 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...
123
        setcookie($cookieProperty['cookie']['key'], $cookieProperty['cookie']['value'], Integer::MAX, '/', null, null, true);
124
        App::$Response->redirect('/');
125
    }
126
127
    /**
128
     * List available routes
129
     * @return string
130
     * @throws \Ffcms\Core\Exception\SyntaxException
131
     * @throws \Ffcms\Core\Exception\NativeException
132
     */
133
    public function actionRouting()
134
    {
135
        $routingMap = App::$Properties->getAll('Routing');
136
137
        return App::$View->render('routing', [
138
            'routes' => $routingMap
139
        ]);
140
    }
141
142
    /**
143
     * Show add form for routing
144
     * @return string
145
     * @throws \Ffcms\Core\Exception\SyntaxException
146
     * @throws \Ffcms\Core\Exception\NativeException
147
     */
148
    public function actionAddroute()
149
    {
150
        $model = new FormAddRoute();
151
152
        if (!File::exist('/Private/Config/Routing.php') || !File::writable('/Private/Config/Routing.php')) {
153
            App::$Session->getFlashBag()->add('error', __('Routing configuration file is not allowed to write: /Private/Config/Routing.php'));
154
        } elseif ($model->send() && $model->validate()) {
155
            $model->save();
156
            return App::$View->render('add_route_save');
157
        }
158
159
        return App::$View->render('add_route', [
160
            'model' => $model->filter()
0 ignored issues
show
Bug introduced by
The method filter() does not seem to exist on object<Apps\Model\Admin\Main\FormAddRoute>.

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...
161
        ]);
162
    }
163
164
    /**
165
     * Delete scheme route
166
     * @throws SyntaxException
167
     * @return string
168
     * @throws \Ffcms\Core\Exception\NativeException
169
     */
170
    public function actionDeleteroute()
171
    {
172
        $type = (string)App::$Request->query->get('type');
173
        $loader = (string)App::$Request->query->get('loader');
174
        $source = Str::lowerCase((string)App::$Request->query->get('path'));
175
176
        $model = new EntityDeleteRoute($type, $loader, $source);
177
        if ($model->send()) {
178
            $model->make();
179
            return App::$View->render('delete_route_save');
180
        }
181
182
        return App::$View->render('delete_route', [
183
            'model' => $model->filter()
0 ignored issues
show
Bug introduced by
The method filter() does not seem to exist on object<Apps\Model\Admin\Main\EntityDeleteRoute>.

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...
184
        ]);
185
    }
186
}