| Total Complexity | 7 |
| Total Lines | 46 |
| Duplicated Lines | 0 % |
| Coverage | 93.75% |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 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 | 4 | public function add(ComponentInterface $object) |
|
| 26 | { |
||
| 27 | 4 | $id = get_class($object); |
|
| 28 | |||
| 29 | 4 | if ($this->has($id)) |
|
| 30 | throw new ContainerException('A class with ID ' . $id . ' already exists in this container.'); |
||
| 31 | |||
| 32 | 4 | $this->storedComponents[$id] = $object; |
|
| 33 | 4 | } |
|
| 34 | |||
| 35 | /** |
||
| 36 | * @inheritdoc |
||
| 37 | */ |
||
| 38 | 8 | public function get($id) |
|
| 39 | { |
||
| 40 | 8 | if (!is_string($id)) |
|
| 41 | 2 | throw new ContainerException('Given ID must be a string'); |
|
| 42 | |||
| 43 | 6 | if (!$this->has($id)) |
|
| 44 | 4 | throw new NotFoundException(); |
|
| 45 | |||
| 46 | 2 | return $this->storedComponents[$id]; |
|
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @inheritdoc |
||
| 51 | * @throws ContainerException |
||
| 52 | */ |
||
| 53 | 8 | public function has($id) |
|
| 59 | } |
||
| 60 | } |