Module::getMenus()   C
last analyzed

Complexity

Conditions 14
Paths 9

Size

Total Lines 39
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 39
c 0
b 0
f 0
rs 6.2666
cc 14
nc 9
nop 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace toir427\admin;
4
5
use Yii;
6
use yii\helpers\Inflector;
7
8
/**
9
 * GUI manager for RBAC.
10
 *
11
 * Use [[\yii\base\Module::$controllerMap]] to change property of controller.
12
 * To change listed menu, use property [[$menus]].
13
 *
14
 * ```
15
 * 'layout' => 'left-menu', // default to null mean use application layout.
16
 * 'controllerMap' => [
17
 *     'assignment' => [
18
 *         'class' => 'toir427\admin\controllers\AssignmentController',
19
 *         'userClassName' => 'app\models\User',
20
 *         'idField' => 'id'
21
 *     ]
22
 * ],
23
 * 'menus' => [
24
 *     'assignment' => [
25
 *         'label' => 'Grand Access' // change label
26
 *     ],
27
 *     'route' => null, // disable menu
28
 * ],
29
 * ```
30
 *
31
 * @property string $mainLayout Main layout using for module. Default to layout of parent module.
32
 * Its used when `layout` set to 'left-menu', 'right-menu' or 'top-menu'.
33
 * @property array $menus List available menu of module.
34
 * It generated by module items .
35
 *
36
 * @author Misbahul D Munir <[email protected]>
37
 * @since 1.0
38
 */
39
class Module extends \yii\base\Module
40
{
41
    /**
42
     * @inheritdoc
43
     */
44
    public $defaultRoute = 'assignment';
45
    /**
46
     * @var array Nav bar items.
47
     */
48
    public $navbar;
49
    /**
50
     * @var string Main layout using for module. Default to layout of parent module.
51
     * Its used when `layout` set to 'left-menu', 'right-menu' or 'top-menu'.
52
     */
53
    public $mainLayout = '@toir427/admin/views/layouts/main.php';
54
    /**
55
     * @var array
56
     * @see [[menus]]
57
     */
58
    private $_menus = [];
59
    /**
60
     * @var array
61
     * @see [[menus]]
62
     */
63
    private $_coreItems = [
64
        'user' => 'Users',
65
        'assignment' => 'Assignments',
66
        'role' => 'Roles',
67
        'permission' => 'Permissions',
68
        'route' => 'Routes',
69
        'rule' => 'Rules',
70
        'menu' => 'Menus',
71
    ];
72
    /**
73
     * @var array
74
     * @see [[items]]
75
     */
76
    private $_normalizeMenus;
77
78
    /**
79
     * @var string Default url for breadcrumb
80
     */
81
    public $defaultUrl;
82
83
    /**
84
     * @var string Default url label for breadcrumb
85
     */
86
    public $defaultUrlLabel;
87
88
    /**
89
     * @inheritdoc
90
     */
91
    public function init()
92
    {
93
        parent::init();
94
        if (!isset(Yii::$app->i18n->translations['rbac-admin'])) {
95
            Yii::$app->i18n->translations['rbac-admin'] = [
96
                'class' => 'yii\i18n\PhpMessageSource',
97
                'sourceLanguage' => 'en',
98
                'basePath' => '@toir427/admin/messages',
99
            ];
100
        }
101
102
        //user did not define the Navbar?
103
        if ($this->navbar === null && Yii::$app instanceof \yii\web\Application) {
104
            $this->navbar = [
105
                ['label' => Yii::t('rbac-admin', 'Help'), 'url' => ['default/index']],
106
                ['label' => Yii::t('rbac-admin', 'Application'), 'url' => Yii::$app->homeUrl],
107
            ];
108
        }
109
        if (class_exists('yii\jui\JuiAsset')) {
110
            Yii::$container->set('toir427\admin\AutocompleteAsset', 'yii\jui\JuiAsset');
111
        }
112
    }
113
114
    /**
115
     * Get available menu.
116
     * @return array
117
     */
118
    public function getMenus()
119
    {
120
        if ($this->_normalizeMenus === null) {
121
            $mid = '/' . $this->getUniqueId() . '/';
122
            // resolve core menus
123
            $this->_normalizeMenus = [];
124
125
            $config = components\Configs::instance();
126
            $conditions = [
127
                'user' => $config->db && $config->db->schema->getTableSchema($config->userTable),
128
                'assignment' => ($userClass = Yii::$app->getUser()->identityClass) && is_subclass_of($userClass, 'yii\db\BaseActiveRecord'),
129
                'menu' => $config->db && $config->db->schema->getTableSchema($config->menuTable),
130
            ];
131
            foreach ($this->_coreItems as $id => $lable) {
132
                if (!isset($conditions[$id]) || $conditions[$id]) {
133
                    $this->_normalizeMenus[$id] = ['label' => Yii::t('rbac-admin', $lable), 'url' => [$mid . $id]];
134
                }
135
            }
136
            foreach (array_keys($this->controllerMap) as $id) {
137
                $this->_normalizeMenus[$id] = ['label' => Yii::t('rbac-admin', Inflector::humanize($id)), 'url' => [$mid . $id]];
138
            }
139
140
            // user configure menus
141
            foreach ($this->_menus as $id => $value) {
142
                if (empty($value)) {
143
                    unset($this->_normalizeMenus[$id]);
144
                    continue;
145
                }
146
                if (is_string($value)) {
147
                    $value = ['label' => $value];
148
                }
149
                $this->_normalizeMenus[$id] = isset($this->_normalizeMenus[$id]) ? array_merge($this->_normalizeMenus[$id], $value)
150
                : $value;
151
                if (!isset($this->_normalizeMenus[$id]['url'])) {
152
                    $this->_normalizeMenus[$id]['url'] = [$mid . $id];
153
                }
154
            }
155
        }
156
        return $this->_normalizeMenus;
157
    }
158
159
    /**
160
     * Set or add available menu.
161
     * @param array $menus
162
     */
163
    public function setMenus($menus)
164
    {
165
        $this->_menus = array_merge($this->_menus, $menus);
166
        $this->_normalizeMenus = null;
167
    }
168
169
    /**
170
     * @inheritdoc
171
     */
172
    public function beforeAction($action)
173
    {
174
        if (parent::beforeAction($action)) {
175
            /* @var $action \yii\base\Action */
176
            $view = $action->controller->getView();
177
178
            $view->params['breadcrumbs'][] = [
179
                'label' => ($this->defaultUrlLabel ?: Yii::t('rbac-admin', 'Admin')),
180
                'url' => ['/' . ($this->defaultUrl ?: $this->uniqueId)],
181
            ];
182
            return true;
183
        }
184
        return false;
185
    }
186
}
187