Role::setParent()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * Role.php
4
 *
5
 * @copyright      More in license.md
6
 * @license        https://www.ipublikuj.eu
7
 * @author         Adam Kadlec <[email protected]>
8
 * @package        iPublikuj:Permissions!
9
 * @subpackage     Entities
10
 * @since          1.0.0
11
 *
12
 * @date           12.03.14
13
 */
14
15
declare(strict_types = 1);
16
17
namespace IPub\Permissions\Entities;
18
19
use Nette;
20
21
use IPub\Permissions\Exceptions;
22
23 1
class Role implements IRole
24
{
25
	/**
26
	 * Implement nette smart magic
27
	 */
28 1
	use Nette\SmartObject;
29
30
	/**
31
	 * @var string
32
	 */
33
	protected $id;
34
35
	/**
36
	 * @var IRole|NULL
37
	 */
38
	protected $parent;
39
40
	/**
41
	 * @var \SplObjectStorage
42
	 */
43
	protected $children;
44
45
	/**
46
	 * @var string|NULL
47
	 */
48
	protected $name;
49
50
	/**
51
	 * @var string|NULL
52
	 */
53
	protected $comment;
54
55
	/**
56
	 * @var \SplObjectStorage
57
	 */
58
	protected $permissions;
59
60
	/**
61
	 * @param string $id
62
	 * @param string|NULL $name
63
	 * @param string|NULL $comment
64
	 */
65
	public function __construct(string $id, ?string $name = NULL, ?string $comment = NULL)
66
	{
67
		// Role identifier
68 1
		$this->id = $id;
69
70 1
		$this->name = $name;
71 1
		$this->comment = $comment;
72
73
		// Storage initialization
74 1
		$this->permissions = new \SplObjectStorage();
75 1
		$this->children = new \SplObjectStorage();
76 1
	}
77
78
	/**
79
	 * {@inheritdoc}
80
	 */
81
	public function setParent(IRole $parent = NULL) : void
82
	{
83 1
		$parent->addChild($this);
0 ignored issues
show
Bug introduced by
It seems like $parent is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
84
85 1
		$this->parent = $parent;
86 1
	}
87
88
	/**
89
	 * {@inheritdoc}
90
	 */
91
	public function getParent() : ?IRole
92
	{
93 1
		return $this->parent;
94
	}
95
96
	/**
97
	 * {@inheritdoc}
98
	 */
99
	public function setChildren(array $roles) : void
100
	{
101
		foreach ($this->getChildren() as $child) {
102
			$child->setParent(NULL);
103
		}
104
105
		$this->children = new \SplObjectStorage();
106
107
		foreach ($roles as $child) {
108
			if ($child instanceof IRole) {
109
				$this->children->attach($child);
110
				$child->setParent($this);
111
			}
112
		}
113
	}
114
115
	/**
116
	 * {@inheritdoc}
117
	 */
118
	public function addChild(IRole $role) : void
119
	{
120 1
		if (!$this->children->contains($role)) {
121 1
			$this->children->attach($role);
122 1
			$role->setParent($this);
123
		}
124 1
	}
125
126
	/**
127
	 * {@inheritdoc}
128
	 */
129
	public function getChildren() : array
130
	{
131 1
		$children = [];
132
133 1
		$this->children->rewind();
134
135 1
		while ($this->children->valid())
136
		{
137 1
			$children[] = $this->children->current();
138 1
			$this->children->next();
139
		}
140
141 1
		return $children;
142
	}
143
144
	/**
145
	 * {@inheritdoc}
146
	 */
147
	public function getRoleId() : string
148
	{
149 1
		return $this->id;
150
	}
151
152
	/**
153
	 * {@inheritdoc}
154
	 */
155
	public function setName(string $name) : void
156
	{
157 1
		$this->name = $name;
158 1
	}
159
160
	/**
161
	 * {@inheritdoc}
162
	 */
163
	public function getName() : ?string
164
	{
165
		return $this->name ?: $this->id;
166
	}
167
168
	/**
169
	 * {@inheritdoc}
170
	 */
171
	public function setComment(?string $comment) : void
172
	{
173 1
		$this->comment = $comment;
174 1
	}
175
176
	/**
177
	 * {@inheritdoc}
178
	 */
179
	public function getComment() : ?string
180
	{
181
		return $this->comment;
182
	}
183
184
	/**
185
	 * {@inheritdoc}
186
	 */
187
	public function setPermissions(array $permissions) : void
188
	{
189 1
		$this->permissions = new \SplObjectStorage();
190
191 1
		foreach ($permissions as $permission) {
192 1
			if ($permission instanceof IPermission) {
193 1
				$this->permissions->attach($permission);
194
			}
195
		}
196 1
	}
197
198
	/**
199
	 * {@inheritdoc}
200
	 */
201
	public function addPermission(IPermission $permission) : void
202
	{
203
		if (!$this->permissions->contains($permission)) {
204
			$this->permissions->attach($permission);
205
		}
206
	}
207
208
	/**
209
	 * {@inheritdoc}
210
	 */
211
	public function getPermissions() : array
212
	{
213 1
		$permissions = [];
214
215 1
		$this->permissions->rewind();
216
217 1
		while ($this->permissions->valid())
218
		{
219 1
			$permissions[] = $this->permissions->current();
220 1
			$this->permissions->next();
221
		}
222
223 1
		return $permissions;
224
	}
225
226
	/**
227
	 * {@inheritdoc}
228
	 */
229
	public function hasPermission(IPermission $permission) : bool
230
	{
231
		return $this->permissions->contains($permission);
232
	}
233
234
	/**
235
	 * {@inheritdoc}
236
	 */
237
	public function removePermission(IPermission $permission) : void
238
	{
239
		if (!$this->permissions->contains($permission)) {
240
			throw new Exceptions\InvalidArgumentException(sprintf('Permission "%s" cannot be removed since it is not associated with the role %s', $permission, $this->getName()));
0 ignored issues
show
Bug introduced by
Consider using $this->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
241
		}
242
243
		$this->permissions->detach($permission);
244
	}
245
246
	/**
247
	 * {@inheritdoc}
248
	 */
249
	public function clearPermissions() : void
250
	{
251
		$this->permissions = new \SplObjectStorage();
252
	}
253
254
	/**
255
	 * {@inheritdoc}
256
	 */
257
	public function isLocked() : bool
258
	{
259
		return in_array($this->id, [IRole::ROLE_ANONYMOUS, IRole::ROLE_AUTHENTICATED, IRole::ROLE_ADMINISTRATOR], TRUE);
260
	}
261
262
	/**
263
	 * {@inheritdoc}
264
	 */
265
	public function isAnonymous() : bool
266
	{
267
		return $this->id === IRole::ROLE_ANONYMOUS;
268
	}
269
270
	/**
271
	 * {@inheritdoc}
272
	 */
273
	public function isAuthenticated() : bool
274
	{
275
		return $this->id === IRole::ROLE_AUTHENTICATED;
276
	}
277
278
	/**
279
	 * {@inheritdoc}
280
	 */
281
	public function isAdministrator() : bool
282
	{
283 1
		return $this->id === IRole::ROLE_ADMINISTRATOR;
284
	}
285
286
	/**
287
	 * Convert role object to string
288
	 *
289
	 * @return string
290
	 */
291
	public function __toString()
292
	{
293
		return $this->id;
294
	}
295
}
296