Completed
Pull Request — master (#296)
by
unknown
03:31
created

AuthItem::search()   C

Complexity

Conditions 11
Paths 6

Size

Total Lines 37
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 37
rs 5.2653
cc 11
eloc 25
nc 6
nop 1

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 mdm\admin\models\searchs;
4
5
use Yii;
6
use yii\base\Model;
7
use yii\data\ArrayDataProvider;
8
use mdm\admin\components\Configs;
9
use yii\rbac\Item;
10
11
/**
12
 * AuthItemSearch represents the model behind the search form about AuthItem.
13
 * 
14
 * @author Misbahul D Munir <[email protected]>
15
 * @since 1.0
16
 */
17
class AuthItem extends Model
18
{
19
    const TYPE_ROUTE = 101;
20
21
    public $name;
22
    public $type;
23
    public $description;
24
    public $ruleName;
25
    public $data;
26
27
    /**
28
     * @inheritdoc
29
     */
30
    public function rules()
31
    {
32
        return [
33
            [['name', 'ruleName', 'description'], 'safe'],
34
            [['type'], 'integer'],
35
        ];
36
    }
37
38
    /**
39
     * @inheritdoc
40
     */
41
    public function attributeLabels()
42
    {
43
        return [
44
            'name' => Yii::t('rbac-admin', 'Name'),
45
            'item_name' => Yii::t('rbac-admin', 'Name'),
46
            'type' => Yii::t('rbac-admin', 'Type'),
47
            'description' => Yii::t('rbac-admin', 'Description'),
48
            'ruleName' => Yii::t('rbac-admin', 'Rule Name'),
49
            'data' => Yii::t('rbac-admin', 'Data'),
50
        ];
51
    }
52
53
    /**
54
     * Search authitem
55
     * @param array $params
56
     * @return \yii\data\ActiveDataProvider|\yii\data\ArrayDataProvider
57
     */
58
    public function search($params)
59
    {
60
        /* @var \yii\rbac\Manager $authManager */
61
        $authManager = Configs::authManager();
62
        if ($this->type == Item::TYPE_ROLE) {
63
            $items = $authManager->getRoles();
64
        } else {
65
            $items = array_filter($authManager->getPermissions(), function($item) {
66
                return $this->type == Item::TYPE_PERMISSION xor strncmp($item->name, '/', 1) === 0;
67
            });
68
        }
69
        $this->load($params);
70
        if ($this->validate()) {
71
            
72
            $strtolower = "strtolower";
73
            $strpos = "strpos";
74
            if (extension_loaded('mbstring')) {
75
                $strtolower = "mb_strtolower";
76
                $strpos = "mb_strpos";
77
            }
78
            $search = $strtolower(trim($this->name));
79
            $desc = $strtolower(trim($this->description));
80
            $ruleName = $this->ruleName;
81
            foreach ($items as $name => $item) {
82
                $f = (empty($search) || $strpos($strtolower($item->name), $search) !== false) &&
83
                    (empty($desc) || $strpos($strtolower($item->description), $desc) !== false) &&
84
                    (empty($ruleName) || $item->ruleName == $ruleName);
85
                if (!$f) {
86
                    unset($items[$name]);
87
                }
88
            }
89
        }
90
91
        return new ArrayDataProvider([
92
            'allModels' => $items,
93
        ]);
94
    }
95
}
96