Code Duplication    Length = 142-142 lines in 2 locations

src/models/Permission.php 1 location

@@ 19-160 (lines=142) @@
16
 *
17
 * @package Itstructure\RbacModule\models
18
 */
19
class Permission extends Rbac implements ModelInterface
20
{
21
    /**
22
     * Permission object.
23
     *
24
     * @var BasePermission
25
     */
26
    public $permission;
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
                    'safe',
50
                ],
51
            ]
52
        );
53
    }
54
55
    /**
56
     * @inheritdoc
57
     */
58
    public function attributeLabels()
59
    {
60
        return [
61
            'name' => Module::t('permissions', 'Name'),
62
            'description' => Module::t('permissions', 'Description'),
63
        ];
64
    }
65
66
    /**
67
     * Get current child permissions assigned to current permission.
68
     *
69
     * @return \yii\rbac\Permission[]
70
     */
71
    protected function getCurrentChildren(): array
72
    {
73
        $permissions = $this->authManager->getChildren($this->getOldName());
74
75
        return array_keys($permissions);
76
    }
77
78
    /**
79
     * Get new child items.
80
     *
81
     * @return array
82
     */
83
    protected function getNewChildren(): array
84
    {
85
        return empty($this->permissions) ? [] : $this->permissions;
86
    }
87
88
    /**
89
     * Create new Permission.
90
     *
91
     * @param string $name
92
     *
93
     * @return Item
94
     */
95
    protected function createItem(string $name): Item
96
    {
97
        return $this->authManager->createPermission($name);
98
    }
99
100
    /**
101
     * Create self child permission.
102
     *
103
     * @param string $name
104
     *
105
     * @return Item
106
     */
107
    protected function createChild(string $name): Item
108
    {
109
        return $this->authManager->createPermission($name);
110
    }
111
112
    /**
113
     * Returns old name in current object, which is set before new value.
114
     *
115
     * @return mixed
116
     */
117
    protected function getOldName()
118
    {
119
        return null === $this->permission ? null : $this->permission->name;
120
    }
121
122
    /**
123
     * Returns Permission object.
124
     *
125
     * @param string $key
126
     *
127
     * @return Item|null
128
     */
129
    protected function getItem(string $key)
130
    {
131
        return $this->authManager->getPermission($key);
132
    }
133
134
    /**
135
     * Set permission field for child instance of Rbac - Permission.
136
     *
137
     * @param Rbac $instance
138
     * @param Item      $item
139
     *
140
     * @return Rbac
141
     */
142
    protected function setItemForInstance(Rbac $instance, Item $item): Rbac
143
    {
144
        $instance->permission = $item;
145
        return $instance;
146
    }
147
148
    /**
149
     * Set children permissions for instance of Role.
150
     *
151
     * @param Rbac $instance
152
     *
153
     * @return Rbac
154
     */
155
    protected function setChildrenForInstance(Rbac $instance): Rbac
156
    {
157
        $instance->permissions = $instance->getCurrentChildren();
158
        return $instance;
159
    }
160
}
161

src/models/Role.php 1 location

@@ 19-160 (lines=142) @@
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
        ];
64
    }
65
66
    /**
67
     * Get current child permissions assigned to current role.
68
     *
69
     * @return \yii\rbac\Permission[]
70
     */
71
    protected function getCurrentChildren(): array
72
    {
73
        $permissions = $this->authManager->getPermissionsByRole($this->getOldName());
74
75
        return array_keys($permissions);
76
    }
77
78
    /**
79
     * Get new child permissions.
80
     *
81
     * @return array
82
     */
83
    protected function getNewChildren(): array
84
    {
85
        return empty($this->permissions) ? [] : $this->permissions;
86
    }
87
88
    /**
89
     * Create new role.
90
     *
91
     * @param string $name
92
     *
93
     * @return Item
94
     */
95
    protected function createItem(string $name): Item
96
    {
97
        return $this->authManager->createRole($name);
98
    }
99
100
    /**
101
     * Create child permission.
102
     *
103
     * @param string $name
104
     *
105
     * @return Item
106
     */
107
    protected function createChild(string $name): Item
108
    {
109
        return $this->authManager->createPermission($name);
110
    }
111
112
    /**
113
     * Returns old name in current object, which is set before new value.
114
     *
115
     * @return mixed
116
     */
117
    protected function getOldName()
118
    {
119
        return null === $this->role ? null : $this->role->name;
120
    }
121
122
    /**
123
     * Returns role object.
124
     *
125
     * @param string $key
126
     *
127
     * @return Item|null
128
     */
129
    protected function getItem(string $key)
130
    {
131
        return $this->authManager->getRole($key);
132
    }
133
134
    /**
135
     * Set role field for child instance of Rbac - Role.
136
     *
137
     * @param Rbac $instance
138
     * @param Item      $item
139
     *
140
     * @return Rbac
141
     */
142
    protected function setItemForInstance(Rbac $instance, Item $item): Rbac
143
    {
144
        $instance->role = $item;
145
        return $instance;
146
    }
147
148
    /**
149
     * Set children permissions for instance of Role.
150
     *
151
     * @param Rbac $instance
152
     *
153
     * @return Rbac
154
     */
155
    protected function setChildrenForInstance(Rbac $instance): Rbac
156
    {
157
        $instance->permissions = $instance->getCurrentChildren();
158
        return $instance;
159
    }
160
}
161