Permission::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace WebComplete\rbac\entity;
4
5
use WebComplete\rbac\exception\RbacException;
6
use WebComplete\rbac\resource\ResourceInterface;
7
8
class Permission implements PermissionInterface
9
{
10
11
    /**
12
     * @var string
13
     */
14
    protected $name;
15
    /**
16
     * @var string
17
     */
18
    protected $description;
19
    /**
20
     * @var ResourceInterface
21
     */
22
    protected $resource;
23
    /**
24
     * @var array
25
     */
26
    protected $childrenNames = [];
27
    /**
28
     * @var string
29
     */
30
    protected $ruleClass;
31
32
    /**
33
     * @param string $name
34
     * @param string $description
35
     * @param ResourceInterface $resource
36
     */
37
    public function __construct(string $name, string $description, ResourceInterface $resource)
38
    {
39
        $this->name = $name;
40
        $this->description = $description;
41
        $this->resource = $resource;
42
    }
43
44
    /**
45
     * @return string
46
     */
47
    public function getName(): string
48
    {
49
        return $this->name;
50
    }
51
52
    /**
53
     * @return string
54
     */
55
    public function getDescription(): string
56
    {
57
        return $this->description;
58
    }
59
60
    /**
61
     * @param PermissionInterface $permission
62
     */
63
    public function addChild(PermissionInterface $permission)
64
    {
65
        $this->childrenNames[$permission->getName()] = true;
66
    }
67
68
    /**
69
     * @param string $permissionName
70
     */
71
    public function removeChild(string $permissionName)
72
    {
73
        unset($this->childrenNames[$permissionName]);
74
    }
75
76
    /**
77
     * @return PermissionInterface[]
78
     */
79
    public function getChildren(): array
80
    {
81
        $result = [];
82
        $permissionNames = \array_keys($this->childrenNames);
83
        foreach ($permissionNames as $name) {
84
            $result[$name] = $this->resource->getPermission($name);
85
        }
86
        return $result;
87
    }
88
89
    /**
90
     * @param string $ruleClass
91
     */
92
    public function setRuleClass(string $ruleClass)
93
    {
94
        $this->ruleClass = $ruleClass;
95
    }
96
97
    /**
98
     * @return string|null;
99
     */
100
    public function getRuleClass()
101
    {
102
        return $this->ruleClass;
103
    }
104
105
    /**
106
     * @param array|null $params
107
     *
108
     * @return bool
109
     * @throws RbacException
110
     */
111
    public function checkAccess($params = null): bool
112
    {
113
        $result = true;
114
        if ($ruleClass = $this->getRuleClass()) {
115
            try {
116
                $rule = new $ruleClass;
117
                if (!$rule instanceof RuleInterface) {
118
                    throw new RbacException('Rule class: ' . $ruleClass . ' is not an ' . RuleInterface::class);
119
                }
120
121
                $result = $rule->execute($params);
122
            } catch (\Throwable $e) {
123
                throw new RbacException('Cannot instantiate rule class: ' . $ruleClass, $e);
124
            }
125
        }
126
        return $result;
127
    }
128
}
129