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

PermissionsProvider   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 81.82%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 50
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        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
use Nette\Utils;
21
22
use IPub\Permissions\Entities;
23
use IPub\Permissions\Exceptions;
24
25
/**
26
 * Basic permissions provider
27
 *
28
 * @package        iPublikuj:Permissions!
29
 * @subpackage     Providers
30
 *
31
 * @author         Adam Kadlec <[email protected]>
32
 */
33 1
class PermissionsProvider extends Nette\Object implements IPermissionsProvider
34
{
35
	/**
36
	 * @var Entities\IPermission[]
37
	 */
38
	private $permissions = [];
39
40
	/**
41
	 * @param Entities\IResource|NULL $resource
42
	 * @param string|NULL $privilege
43
	 * @param array|NULL $details
44
	 * @param callable|NULL $assertion
45
	 *
46
	 * @return Entities\IPermission
47
	 */
48
	public function addPermission(Entities\IResource $resource = NULL, string $privilege = NULL, array $details = NULL, callable $assertion = NULL) : Entities\IPermission
49
	{
50 1
		$permission = new Entities\Permission($resource, $privilege, $details, $assertion);
0 ignored issues
show
Bug introduced by
It seems like $details defined by parameter $details on line 48 can also be of type null; however, IPub\Permissions\Entitie...rmission::__construct() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
51
52 1
		if (array_key_exists((string) $permission, $this->permissions)) {
53
			throw new Exceptions\InvalidStateException(sprintf('Permission "%s" is already registered.', (string) $permission));
54
		}
55
56 1
		$this->permissions[(string) $permission] = $permission;
57
58 1
		return $permission;
59
	}
60
61
	/**
62
	 * @param string $id
63
	 *
64
	 * @return Entities\IPermission
65
	 */
66
	public function getPermission(string $id) : Entities\IPermission
67
	{
68 1
		if (!array_key_exists($id, $this->permissions)) {
69
			throw new Exceptions\InvalidStateException(sprintf('Permission "%s" is not registered.', $id));
70
		}
71
72 1
		return $this->permissions[$id];
73
	}
74
75
	/**
76
	 * @return Entities\IPermission[]
77
	 */
78
	public function findAll() : array
79
	{
80 1
		return $this->permissions;
81
	}
82
}
83