ContainerAdapterFactory::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
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