Role   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 22
c 2
b 0
f 0
dl 0
loc 141
rs 10
wmc 12

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getOldName() 0 3 2
A getItem() 0 3 1
A getCurrentChildren() 0 5 1
A getNewChildren() 0 3 2
A setChildrenForInstance() 0 4 1
A createChild() 0 3 1
A rules() 0 10 1
A attributeLabels() 0 6 1
A setItemForInstance() 0 4 1
A createItem() 0 3 1
1
<?php
2
3
namespace Itstructure\RbacModule\models;
4
5
use yii\helpers\ArrayHelper;
6
use yii\rbac\{Item, Role as BaseRole, ManagerInterface};
7
use Itstructure\RbacModule\interfaces\ModelInterface;
8
use Itstructure\RbacModule\Module;
9
10
/**
11
 * Class Role
12
 *
13
 * @property BaseRole $role
14
 * @property array $permissions
15
 * @property ManagerInterface $authManager
16
 *
17
 * @package Itstructure\RbacModule\models
18
 */
19
class Role extends Rbac implements ModelInterface
20
{
21
    /**
22
     * Role object.
23
     *
24
     * @var BaseRole
25
     */
26
    public $role;
27
28
    /**
29
     * Child permissions.
30
     *
31
     * @var array
32
     */
33
    public $permissions = [];
34
35
    /**
36
     * Validate rules.
37
     *
38
     * @return array
39
     */
40
    public function rules()
41
    {
42
        return ArrayHelper::merge(
43
            parent::rules(),
44
            [
45
                [
46
                    [
47
                        'permissions',
48
                    ],
49
                    'required',
50
                ],
51
            ]
52
        );
53
    }
54
55
    /**
56
     * @inheritdoc
57
     */
58
    public function attributeLabels()
59
    {
60
        return [
61
            'name' => Module::t('roles', 'Name'),
62
            'description' => Module::t('roles', 'Description'),
63
            'permissions' => Module::t('roles', 'Permissions'),
64
        ];
65
    }
66
67
    /**
68
     * Get current child permissions assigned to current role.
69
     *
70
     * @return \yii\rbac\Permission[]
71
     */
72
    protected function getCurrentChildren(): array
73
    {
74
        $permissions = $this->authManager->getPermissionsByRole($this->getOldName());
75
76
        return array_keys($permissions);
77
    }
78
79
    /**
80
     * Get new child permissions.
81
     *
82
     * @return array
83
     */
84
    protected function getNewChildren(): array
85
    {
86
        return empty($this->permissions) ? [] : $this->permissions;
87
    }
88
89
    /**
90
     * Create new role.
91
     *
92
     * @param string $name
93
     *
94
     * @return Item
95
     */
96
    protected function createItem(string $name): Item
97
    {
98
        return $this->authManager->createRole($name);
99
    }
100
101
    /**
102
     * Create child permission.
103
     *
104
     * @param string $name
105
     *
106
     * @return Item
107
     */
108
    protected function createChild(string $name): Item
109
    {
110
        return $this->authManager->createPermission($name);
111
    }
112
113
    /**
114
     * Returns old name in current object, which is set before new value.
115
     *
116
     * @return mixed
117
     */
118
    protected function getOldName()
119
    {
120
        return null === $this->role ? null : $this->role->name;
121
    }
122
123
    /**
124
     * Returns role object.
125
     *
126
     * @param string $key
127
     *
128
     * @return Item|null
129
     */
130
    protected function getItem(string $key)
131
    {
132
        return $this->authManager->getRole($key);
133
    }
134
135
    /**
136
     * Set role field for child instance of Rbac - Role.
137
     *
138
     * @param Rbac $instance
139
     * @param Item      $item
140
     *
141
     * @return Rbac
142
     */
143
    protected function setItemForInstance(Rbac $instance, Item $item): Rbac
144
    {
145
        $instance->role = $item;
0 ignored issues
show
Bug Best Practice introduced by
The property role does not exist on Itstructure\RbacModule\models\Rbac. Since you implemented __set, consider adding a @property annotation.
Loading history...
146
        return $instance;
147
    }
148
149
    /**
150
     * Set children permissions for instance of Role.
151
     *
152
     * @param Rbac $instance
153
     *
154
     * @return Rbac
155
     */
156
    protected function setChildrenForInstance(Rbac $instance): Rbac
157
    {
158
        $instance->permissions = $instance->getCurrentChildren();
0 ignored issues
show
Bug Best Practice introduced by
The property permissions does not exist on Itstructure\RbacModule\models\Rbac. Since you implemented __set, consider adding a @property annotation.
Loading history...
159
        return $instance;
160
    }
161
}
162