ResourcesProvider   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 76.92%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 57
ccs 10
cts 13
cp 0.7692
rs 10
c 0
b 0
f 0

3 Methods

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