Passed
Push — master ( 19b012...ae2464 )
by Rick
02:24
created

ComponentContainer   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 93.75%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 48
ccs 15
cts 16
cp 0.9375
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 9 2
A get() 0 10 3
A has() 0 7 2
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
	    $id = get_class($object);
28
29 2
	    if ($this->has($id))
30
	        throw new ContainerException('A class with ID ' . $id . ' already exists in this container.');
31
32 2
		$this->storedComponents[$id] = $object;
33 2
	}
34
35
	/**
36
	 * @inheritdoc
37
	 */
38 4
	public function get($id)
39
	{
40 4
		if (!is_string($id))
41 1
			throw new ContainerException('Given ID must be a string');
42
43 3
		if (!$this->has($id))
44 2
			throw new NotFoundException();
45
46 1
		return $this->storedComponents[$id];
47
	}
48
49
	/**
50
	 * @inheritdoc
51
     * @throws ContainerException
52
	 */
53 4
	public function has($id)
54
	{
55 4
		if (!is_string($id))
56 1
			throw new ContainerException('Given ID must be a string');
57
58 4
		return array_key_exists($id, $this->storedComponents);
59
	}
60
}