Completed
Push — master ( 3f24f8...42984c )
by Andrey
01:17
created

Role::attributeLabels()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
dl 8
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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 View Code Duplication
class Role extends Rbac implements ModelInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
Documentation introduced by
The property role does not exist on object<Itstructure\RbacModule\models\Rbac>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

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
Documentation introduced by
The property permissions does not exist on object<Itstructure\RbacModule\models\Rbac>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
159
        return $instance;
160
    }
161
}
162