ContainerAdapterFactory   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B create() 0 28 5
1
<?php
2
3
namespace TomPHP\ContainerConfigurator;
4
5
use TomPHP\ContainerConfigurator\Exception\NotContainerAdapterException;
6
use TomPHP\ContainerConfigurator\Exception\UnknownContainerException;
7
8
/**
9
 * @internal
10
 */
11
final class ContainerAdapterFactory
12
{
13
    /**
14
     * @var array
15
     */
16
    private $config;
17
18
    public function __construct(array $config)
19
    {
20
        $this->config = $config;
21
    }
22
23
    /**
24
     * @param object $container
25
     *
26
     * @throws UnknownContainerException
27
     * @throws NotContainerAdapterException
28
     *
29
     * @return ContainerAdapter
30
     */
31
    public function create($container)
32
    {
33
        $class = '';
34
35
        foreach ($this->config as $containerClass => $configuratorClass) {
36
            if ($container instanceof $containerClass) {
37
                $class = $configuratorClass;
38
                break;
39
            }
40
        }
41
42
        if (!$class) {
43
            throw UnknownContainerException::fromContainerName(
44
                get_class($container),
45
                array_keys($this->config)
46
            );
47
        }
48
49
        $instance = new $class();
50
51
        if (!$instance instanceof ContainerAdapter) {
52
            throw NotContainerAdapterException::fromClassName($class);
53
        }
54
55
        $instance->setContainer($container);
56
57
        return $instance;
58
    }
59
}
60