Completed
Push — master ( 7dd2dd...7e98b2 )
by Adam
03:24 queued 01:04
created

RolesProvider   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 88.24%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 61
ccs 15
cts 17
cp 0.8824
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B addRole() 0 24 5
A getRole() 0 8 2
A findAll() 0 4 1
1
<?php
2
/**
3
 * RolesProvider.php
4
 *
5
 * @copyright      More in license.md
6
 * @license        http://www.ipublikuj.eu
7
 * @author         Adam Kadlec http://www.ipublikuj.eu
8
 * @package        iPublikuj:Permissions!
9
 * @subpackage     Providers
10
 * @since          2.0.0
11
 *
12
 * @date           30.11.16
13
 */
14
15
declare(strict_types = 1);
16
17
namespace IPub\Permissions\Providers;
18
19
use Nette;
20
21
use IPub;
22
use IPub\Permissions\Entities;
23
use IPub\Permissions\Exceptions;
24
25
/**
26
 * Basic roles provider
27
 *
28
 * @package        iPublikuj:Permissions!
29
 * @subpackage     Providers
30
 *
31
 * @author         Adam Kadlec <[email protected]>
32
 */
33 1
class RolesProvider extends Nette\Object implements IRolesProvider
34
{
35
	/**
36
	 * @var Entities\IRole[]
37
	 */
38
	private $roles = [];
39
40
	/**
41
	 * @param string $id
42
	 * @param Entities\IRole|NULL $parent
43
	 * @param Entities\IPermission|Entities\IPermission[]|NULL $permissions
44
	 *
45
	 * @return Entities\IRole
46
	 */
47
	public function addRole(string $id, Entities\IRole $parent = NULL, $permissions = NULL) : Entities\IRole
48
	{
49 1
		if (array_key_exists($id, $this->roles)) {
50
			throw new Exceptions\InvalidStateException(sprintf('Role "%s" has been already added.', $id));
51
		}
52
53 1
		if ($permissions instanceof Entities\IPermission) {
54 1
			$permissions = [$permissions];
55
		}
56
57 1
		$role = new Entities\Role($id);
58
59 1
		if ($parent) {
60 1
			$role->setParent($parent);
61
		}
62
63 1
		if ($permissions) {
64 1
			$role->setPermissions($permissions);
65
		}
66
67 1
		$this->roles[$id] = $role;
68
69 1
		return $role;
70
	}
71
72
	/**
73
	 * @param string $roleName
74
	 *
75
	 * @return Entities\IRole
76
	 */
77
	public function getRole(string $roleName) : Entities\IRole
78
	{
79 1
		if (!array_key_exists($roleName, $this->roles)) {
80
			throw new Exceptions\InvalidArgumentException(sprintf('Role "%s" is not registered.', $roleName));
81
		}
82
83 1
		return $this->roles[$roleName];
84
	}
85
86
	/**
87
	 * @return Entities\IRole[]
88
	 */
89
	public function findAll() : array
90
	{
91 1
		return $this->roles;
92
	}
93
}
94