PermissionsProvider   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 81.82%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 55
ccs 9
cts 11
cp 0.8182
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A addPermission() 0 12 2
A getPermission() 0 8 2
A findAll() 0 4 1
1
<?php
2
/**
3
 * PermissionsProvider.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     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\Permissions\Entities;
22
use IPub\Permissions\Exceptions;
23
24
/**
25
 * Basic permissions provider
26
 *
27
 * @package        iPublikuj:Permissions!
28
 * @subpackage     Providers
29
 *
30
 * @author         Adam Kadlec <[email protected]>
31
 */
32 1
class PermissionsProvider implements IPermissionsProvider
33
{
34
	/**
35
	 * Implement nette smart magic
36
	 */
37 1
	use Nette\SmartObject;
38
39
	/**
40
	 * @var Entities\IPermission[]
41
	 */
42
	private $permissions = [];
43
44
	/**
45
	 * @param Entities\IResource|NULL $resource
46
	 * @param string|NULL $privilege
47
	 * @param array|NULL $details
48
	 * @param callable|NULL $assertion
49
	 *
50
	 * @return Entities\IPermission
51
	 */
52
	public function addPermission(?Entities\IResource $resource = NULL, ?string $privilege = NULL, ?array $details = NULL, ?callable $assertion = NULL) : Entities\IPermission
53
	{
54 1
		$permission = new Entities\Permission($resource, $privilege, $details, $assertion);
55
56 1
		if (array_key_exists((string) $permission, $this->permissions)) {
57
			throw new Exceptions\InvalidStateException(sprintf('Permission "%s" is already registered.', (string) $permission));
58
		}
59
60 1
		$this->permissions[(string) $permission] = $permission;
61
62 1
		return $permission;
63
	}
64
65
	/**
66
	 * @param string $id
67
	 *
68
	 * @return Entities\IPermission
69
	 */
70
	public function getPermission(string $id) : Entities\IPermission
71
	{
72 1
		if (!array_key_exists($id, $this->permissions)) {
73
			throw new Exceptions\InvalidStateException(sprintf('Permission "%s" is not registered.', $id));
74
		}
75
76 1
		return $this->permissions[$id];
77
	}
78
79
	/**
80
	 * @return Entities\IPermission[]
81
	 */
82
	public function findAll() : array
83
	{
84 1
		return $this->permissions;
85
	}
86
}
87