Completed
Push — master ( 425846...6cb170 )
by Adam
07:36
created

ResourcesProvider::getResource()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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