Passed
Push — master ( c1fc2f...23f085 )
by Mihail
04:43
created

AdminAppController::__construct()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 24
rs 8.5126
cc 5
eloc 12
nc 4
nop 1
1
<?php
2
3
namespace Extend\Core\Arch;
4
5
use Extend\Core\Arch\Controller as AbstractController;
6
use Ffcms\Core\App;
7
use Ffcms\Core\Exception\ForbiddenException;
8
use Ffcms\Core\Helper\Type\Obj;
9
use Ffcms\Core\Helper\Type\Str;
10
11
class AdminAppController extends AbstractController
12
{
13
    protected $applications;
14
    protected $application;
15
16
    /**
17
     * AdminAppController constructor.
18
     * @param bool $checkVersion
19
     * @throws ForbiddenException
20
     */
21
    public function __construct($checkVersion = true)
22
    {
23
        parent::__construct();
24
25
        // build app and check access
26
        $this->buildApps();
27
        $this->checkAccess();
28
29
        // if version is not necessary to check - continue
30
        if ($checkVersion === false) {
31
            return;
32
        } elseif ($this->application === null) {
33
            // check if appdata is loaded from db
34
            throw new ForbiddenException('This application is not installed!');
35
        }
36
37
        // check app version matching
38
        if (!method_exists($this->application, 'checkVersion') || $this->application->checkVersion() !== true) {
39
            App::$Session->getFlashBag()->add(
40
                'error',
41
                __('Attention! Version of this application scripts is no match to database version. Please, make update!')
42
            );
43
        }
44
    }
45
46
    /**
47
     * Check if current user can access to admin controllers
48
     */
49
    private function checkAccess()
50
    {
51
        $user = App::$User->identity();
52
        // user is not authed ?
53 View Code Duplication
        if ($user === null || !App::$User->isAuth()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
54
            $redirectUrl = App::$Alias->scriptUrl . '/user/login';
55
            App::$Response->redirect($redirectUrl, true);
56
            exit();
0 ignored issues
show
Coding Style Compatibility introduced by
The method checkAccess() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
57
        }
58
59
        $permission = env_name . '/' . App::$Request->getController() . '/' . App::$Request->getAction();
60
61
        // doesn't have permission? get the f*ck out
62 View Code Duplication
        if (!$user->getRole()->can($permission)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
63
            App::$Session->invalidate();
64
65
            $redirectUrl = App::$Alias->scriptUrl . '/user/login';
66
            App::$Response->redirect($redirectUrl, true);
67
            exit();
0 ignored issues
show
Coding Style Compatibility introduced by
The method checkAccess() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
68
        }
69
    }
70
71
    /**
72
     * Build application list to memory object
73
     * @throws \Ffcms\Core\Exception\SyntaxException
74
     */
75
    private function buildApps()
76
    {
77
        // each all applications
78
        foreach ($this->table as $app) {
79
            // check if type is mach for current controller abstraction
80
            if ($app->type === 'app') {
81
                // add to all type-based list
82
                $this->applications[] = $app;
83
                $currentAppName = Str::lastIn(get_class($this), '\\', true);
84
                // if this row is a current runned controller - set object for fast access
85
                if ($app->sys_name === $currentAppName) {
86
                    $this->application = $app;
87
                }
88
            }
89
        }
90
    }
91
92
    /**
93
     * Get current application data as stdClass object
94
     * @return object|null
95
     */
96
    public function getAppData()
97
    {
98
        return $this->application;
99
    }
100
101
    /**
102
     * Get all applications data as array of objects
103
     * @return array|null
104
     */
105
    public function getAllApps()
106
    {
107
        return $this->applications;
108
    }
109
110
    /**
111
     * Get current application configs as array
112
     * @return array
113
     */
114
    public function getConfigs()
115
    {
116
        return (array)unserialize($this->application->configs);
117
    }
118
119
    /**
120
     * Save application configs
121
     * @param array $configs
122
     * @return bool
123
     */
124
    public function setConfigs(array $configs = null)
125
    {
126
        if ($configs === null || !Obj::isArray($configs) || count($configs) < 1) {
127
            return false;
128
        }
129
130
        $serialized = serialize($configs);
131
132
        $obj = \Apps\ActiveRecord\App::find($this->application->id);
133
134
        if ($obj === null) {
135
            return false;
136
        }
137
138
        $obj->configs = $serialized;
139
        $obj->save();
140
        return true;
141
142
    }
143
}