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

ContainerFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 39
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 8 2
A set() 0 8 2
A addProvider() 0 4 1
A createContainer() 0 10 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