Completed
Push — master ( 35dcc1...739344 )
by Andrey
01:14
created

Permission::attributeLabels()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 7
Ratio 100 %

Importance

Changes 0
Metric Value
dl 7
loc 7
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, Permission as BasePermission, ManagerInterface};
7
use Itstructure\RbacModule\interfaces\ModelInterface;
8
use Itstructure\RbacModule\Module;
9
10
/**
11
 * Class Permission
12
 *
13
 * @property BasePermission $permission
14
 * @property array $permissions
15
 * @property ManagerInterface $authManager
16
 *
17
 * @package Itstructure\RbacModule\models
18
 */
19 View Code Duplication
class Permission 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
     * 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;
0 ignored issues
show
Documentation introduced by
The property permission 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...
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();
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...
158
        return $instance;
159
    }
160
}
161