1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tleckie\Acl\Role; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class RoleRecorder |
9
|
|
|
* |
10
|
|
|
* @package Tleckie\Acl\Role |
11
|
|
|
* @author Teodoro Leckie Westberg <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class RoleRecorder implements RoleRecorderInterface |
14
|
|
|
{ |
15
|
|
|
/** @var array */ |
16
|
|
|
private array $roles = []; |
17
|
|
|
|
18
|
|
|
/** @var RoleFactoryInterface */ |
19
|
|
|
private RoleFactoryInterface $roleFactory; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* RoleRecorder constructor. |
23
|
|
|
* |
24
|
|
|
* @param RoleFactoryInterface $roleFactory |
25
|
|
|
*/ |
26
|
|
|
public function __construct(RoleFactoryInterface $roleFactory) |
27
|
|
|
{ |
28
|
|
|
$this->roleFactory = $roleFactory; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @inheritdoc |
33
|
|
|
*/ |
34
|
|
|
public function addRole(RoleInterface|string $role, array $parents = []): RoleRecorderInterface |
35
|
|
|
{ |
36
|
|
|
if ($this->hasRole($role)) { |
37
|
|
|
throw new InvalidArgumentException( |
38
|
|
|
sprintf('Role [%s] already exists', $role) |
39
|
|
|
); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$role = $this->roleFactory->create($role); |
43
|
|
|
|
44
|
|
|
$this->roles[(string)$role] = $role; |
45
|
|
|
|
46
|
|
|
foreach ($parents as $parent) { |
47
|
|
|
if (!$this->hasRole($parent)) { |
48
|
|
|
$this->addRole($parent); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$parent = $this->getRole($parent); |
52
|
|
|
|
53
|
|
|
$parent->addChildren($role); |
54
|
|
|
|
55
|
|
|
$role->addParent($parent); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @inheritdoc |
63
|
|
|
*/ |
64
|
|
|
public function hasRole(RoleInterface|string $role): bool |
65
|
|
|
{ |
66
|
|
|
return isset($this->roles[(string)$this->roleFactory->create($role)]); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @inheritdoc |
71
|
|
|
*/ |
72
|
|
|
public function getRole(RoleInterface|string $role): RoleInterface |
73
|
|
|
{ |
74
|
|
|
if (!$this->hasRole($role)) { |
75
|
|
|
throw new InvalidArgumentException( |
76
|
|
|
sprintf('Role [%s] not found', $role) |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $this->roles[(string)$role]; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @inheritdoc |
85
|
|
|
*/ |
86
|
|
|
public function removeRole(RoleInterface|string $role): RoleRecorderInterface |
87
|
|
|
{ |
88
|
|
|
try { |
89
|
|
|
$role = $this->getRole($role); |
90
|
|
|
} catch (InvalidArgumentException $exception) { |
91
|
|
|
throw new InvalidArgumentException( |
92
|
|
|
sprintf('Role [%s] not found, can not remove', $role), |
93
|
|
|
$exception->getCode(), |
94
|
|
|
$exception |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
foreach ($this->roles as $itemRole) { |
99
|
|
|
if ($role === $itemRole) { |
100
|
|
|
unset($this->roles[(string)$role]); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @inheritdoc |
109
|
|
|
*/ |
110
|
|
|
public function removeAllRole(): RoleRecorderInterface |
111
|
|
|
{ |
112
|
|
|
$this->roles = []; |
113
|
|
|
|
114
|
|
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @inheritdoc |
119
|
|
|
*/ |
120
|
|
|
public function roles(): array |
121
|
|
|
{ |
122
|
|
|
return $this->roles; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|