Passed
Push — master ( 194b0c...eeb30b )
by Rick
01:38
created

ComponentContainer::get()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
crap 3
1
<?php
2
/**
3
 * Copyright 2017 NanoSector
4
 *
5
 * You should have received a copy of the MIT license with the project.
6
 * See the LICENSE file for more information.
7
 */
8
9
namespace Yoshi2889\Container;
10
11
use Psr\Container\ContainerInterface;
12
13
class ComponentContainer implements ContainerInterface
14
{
15
	/**
16
	 * @var object[]
17
	 */
18
	protected $storedComponents = [];
19
20
	/**
21
	 * @param ComponentInterface $object $object
22
	 *
23
	 * @throws ContainerException
24
	 */
25 2
	public function add(ComponentInterface $object)
26
	{
27 2
		$this->storedComponents[get_class($object)] = $object;
28 2
	}
29
30
	/**
31
	 * @inheritdoc
32
	 */
33 4
	public function get($id)
34
	{
35 4
		if (!is_string($id))
36 1
			throw new ContainerException('Given ID must be a string');
37
38 3
		if (!$this->has($id))
39 2
			throw new NotFoundException();
40
41 1
		return $this->storedComponents[$id];
42
	}
43
44
	/**
45
	 * @inheritdoc
46
	 */
47 4
	public function has($id)
48
	{
49 4
		if (!is_string($id))
50 1
			throw new ContainerException('Given ID must be a string');
51
52 4
		return array_key_exists($id, $this->storedComponents);
53
	}
54
}