Passed
Push — master ( 6ac393...b5eaf5 )
by Bo
02:15
created

ContainerFactory::addProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace NaiveContainer;
4
5
use Closure;
6
use NaiveContainer\Exceptions\DuplicateKeyException;
7
8
class ContainerFactory
9
{
10
11
    protected $factory_stack = [];
12
13
    public function register($id, Closure $closure)
14
    {
15
        if (isset($this->factory_stack[$id])) {
16
            throw new DuplicateKeyException();
17
        }
18
19
        $this->factory_stack[$id] = $closure;
20
    }
21
22
    public function set($id, $value)
23
    {
24
        if (isset($this->factory_stack[$id])) {
25
            throw new DuplicateKeyException();
26
        }
27
28
        $this->factory_stack[$id] = $value;
29
    }
30
31
    public function addProvider(FactoryProvider $provider)
32
    {
33
        $provider->register($this);
34
    }
35
36
    public function createContainer()
37
    {
38
        $container = new Container();
39
        $bootstrap = function($stack) {
40
            $this->container_stack = $stack;
41
        };
42
        $bootstrap->call($container, $this->factory_stack);
43
44
        return $container;
45
    }
46
}
47