Container::has()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 1
nc 1
nop 1
1
<?php
2
3
namespace anned20\Strix;
4
5
use Psr\Container\ContainerInterface;
6
7
use anned20\Strix\Exception\ContainerException;
8
use anned20\Strix\Exception\NotFoundException;
9
use anned20\Strix\Exception\AlreadyInContainerException;
10
11
/**
12
 * Class Container
13
 * @author Anne Douwe Bouma
14
 */
15
class Container implements ContainerInterface
16
{
17
	/**
18
	 * @var array
19
	 */
20
	private $entries = [];
21
22
	/**
23
	 * {@inheritdoc}
24
	 */
25
	public function get($id)
26
	{
27
		if (!$this->has($id)) {
28
			throw new NotFoundException(sprintf('"%s" is not in the container', $id));
29
		}
30
31
		return $this->entries[$id];
32
	}
33
34
	/**
35
	 * {@inheritdoc}
36
	 */
37
	public function has($id)
38
	{
39
		return array_key_exists($id, $this->entries);
40
	}
41
42
	/**
43
	 * Add entry to container
44
	 *
45
	 * @param string $id
46
	 * @param mixed  $entry
47
	 *
48
	 * @return Container
49
	 */
50
	public function add($id, $entry)
51
	{
52
		if ($this->has($id)) {
53
			throw new AlreadyInContainerException(sprintf('Container already has entry with id "%s"', $id));
54
		}
55
56
		$this->entries[$id] = $entry;
57
58
		return $this;
59
	}
60
61
	/**
62
	 * Delete entry in container
63
	 *
64
	 * @param string $id
65
	 *
66
	 * @return Container
67
	 */
68
	public function delete($id)
69
	{
70
		if (!$this->has($id)) {
71
			throw new NotFoundException(sprintf('"%s" is not in the container', $id));
72
		}
73
74
		unset($this->entries[$id]);
75
76
		return $this;
77
	}
78
}
79