Role::getPermissions()   B
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 11
nc 4
nop 1
dl 0
loc 20
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace WebComplete\rbac\entity;
4
5
use WebComplete\rbac\resource\ResourceInterface;
6
7
class Role implements RoleInterface
8
{
9
10
    /**
11
     * @var string
12
     */
13
    protected $name;
14
    /**
15
     * @var string
16
     */
17
    protected $description;
18
    /**
19
     * @var ResourceInterface
20
     */
21
    protected $resource;
22
    /**
23
     * @var array
24
     */
25
    protected $childrenNames = [];
26
    /**
27
     * @var array
28
     */
29
    protected $permissionNames = [];
30
31
    /**
32
     * @param string $name
33
     * @param string $description
34
     * @param ResourceInterface $resource
35
     */
36
    public function __construct(string $name, string $description, ResourceInterface $resource)
37
    {
38
        $this->name = $name;
39
        $this->description = $description;
40
        $this->resource = $resource;
41
    }
42
43
    /**
44
     * @return string
45
     */
46
    public function getName(): string
47
    {
48
        return $this->name;
49
    }
50
51
    /**
52
     * @return string
53
     */
54
    public function getDescription(): string
55
    {
56
        return $this->description;
57
    }
58
59
    /**
60
     * @param RoleInterface $role
61
     */
62
    public function addChild(RoleInterface $role)
63
    {
64
        $this->childrenNames[$role->getName()] = true;
65
    }
66
67
    /**
68
     * @param string $roleName
69
     */
70
    public function removeChild(string $roleName)
71
    {
72
        unset($this->childrenNames[$roleName]);
73
    }
74
75
    /**
76
     * @return RoleInterface[]
77
     */
78
    public function getChildren(): array
79
    {
80
        $result = [];
81
        $roleNames = \array_keys($this->childrenNames);
82
        foreach ($roleNames as $name) {
83
            $result[$name] = $this->resource->getRole($name);
84
        }
85
        return $result;
86
    }
87
88
    /**
89
     * @param PermissionInterface $permission
90
     */
91
    public function addPermission(PermissionInterface $permission)
92
    {
93
        $this->permissionNames[$permission->getName()] = true;
94
    }
95
96
    /**
97
     * @param string $permissionName
98
     */
99
    public function removePermission(string $permissionName)
100
    {
101
        unset($this->permissionNames[$permissionName]);
102
    }
103
104
    /**
105
     * @param bool $withChildren
106
     *
107
     * @return PermissionInterface[]
108
     */
109
    public function getPermissions(bool $withChildren = false): array
110
    {
111
        $result = [];
112
        $permissionNames = \array_keys($this->permissionNames);
113
114
        foreach ($permissionNames as $name) {
115
            $permission = $this->resource->getPermission($name);
116
            $result[$name] = $permission;
117
        }
118
119
        if ($withChildren) {
120
            foreach ($result as $permission) {
121
                $this->collectChildrenPermissions($permission, $result);
122
            }
123
            foreach ($this->getChildren() as $child) {
124
                $result = \array_merge($result, $child->getPermissions(true));
125
            }
126
        }
127
128
        return $result;
129
    }
130
131
    /**
132
     * @param string $permissionName
133
     * @param array|null $params
134
     *
135
     * @return bool
136
     */
137
    public function checkAccess($permissionName, $params = null): bool
138
    {
139
        $permissions = $this->getPermissions(true);
140
        if (isset($permissions[$permissionName])) {
141
            if ($permissions[$permissionName]->checkAccess($params)) {
142
                return true;
143
            }
144
        }
145
        return false;
146
    }
147
148
    /**
149
     * @param PermissionInterface $permission
150
     * @param $result
151
     */
152
    protected function collectChildrenPermissions(PermissionInterface $permission, &$result)
153
    {
154
        foreach ($permission->getChildren() as $childPermission) {
155
            $childPermissionName = $childPermission->getName();
156
            if (!isset($result[$childPermissionName])) {
157
                $result[$childPermissionName] = $childPermission;
158
                $this->collectChildrenPermissions($childPermission, $result);
159
            }
160
        }
161
    }
162
}
163