RedirectPanel   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 42
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A beforeAction() 0 8 3
A getModule() 0 8 2
A matchAction() 0 8 2
1
<?php
2
/**
3
 * HiPanel core package
4
 *
5
 * @link      https://hipanel.com/
6
 * @package   hipanel-core
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2014-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\filters;
12
13
use Yii;
14
use yii\base\Action;
15
use yii\base\ActionFilter;
16
use yii\helpers\StringHelper;
17
18
/**
19
 * EasyAccessControl provides easy access control based on a list of actions and permissions.
20
 * Like this:
21
 * ```
22
 *   'class' => RedirectPanel::class,
23
 *   'actions' => 'index,view',
24
 * ```.
25
 *
26
 * @author Andrii Vasyliev <[email protected]>
27
 */
28
class RedirectPanel extends ActionFilter
29
{
30
    /**
31
     * @var string a list of actions requiring redirect to panel
32
     */
33
    public $actions = '';
34
35
    /**
36
     * This method is invoked right before an action is to be executed (after all possible filters.)
37
     * You may override this method to do last-minute preparation for the action.
38
     * @param Action $action the action to be executed
39
     * @return bool whether the action execution should be continued
40
     */
41
    public function beforeAction($action)
42
    {
43
        if ($this->getModule()->isPanel()) {
44
            return true;
45
        }
46
47
        return $this->matchAction($action, $this->actions) ? $this->getModule()->redirectPanel() : true;
48
    }
49
50
    protected function matchAction($action, $names)
51
    {
52
        if ($names === '*') {
53
            return true;
54
        }
55
56
        return in_array($action->id, StringHelper::explode($names, ',', true, true), true);
57
    }
58
59
    protected $_module;
60
61
    public function getModule()
62
    {
63
        if ($this->_module === null) {
64
            $this->_module = Yii::$app->getModule('hipanel');
65
        }
66
67
        return $this->_module;
68
    }
69
}
70