Passed
Push — master ( 4c7ac4...daab02 )
by Bo
02:14
created

Container::get()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
rs 8.8571
cc 5
eloc 12
nc 5
nop 1
1
<?php
2
3
namespace NaiveContainer;
4
5
use NaiveContainer\Exceptions\ContainerException;
6
use NaiveContainer\Exceptions\NotFoundException;
7
use Psr\Container\ContainerInterface;
8
9
class Container implements ContainerInterface
10
{
11
12
    protected $container_stack = [];
13
14
    public function get($id)
15
    {
16
        if (!array_key_exists($id, $this->container_stack)) {
17
            throw new NotFoundException();
18
        }
19
20
        if (is_callable($this->container_stack[$id])) {
21
            try {
22
                // entry: service or value
23
                $entry = call_user_func($this->container_stack[$id], $this);
24
            } catch (NotFoundException $e) {
25
                throw $e;
26
            } catch (\Exception $e) {
27
                throw new ContainerException($e->getMessage());
28
            }
29
            return $entry;
30
        }
31
32
        return $this->container_stack[$id];
33
    }
34
35
    public function has($id)
36
    {
37
        return array_key_exists($id, $this->container_stack);
38
    }
39
}
40