Completed
Push — master ( 03d9da...2dac9a )
by Tom
13s
created

ContainerAdapterFactory::__construct()   A

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\UnknownContainerException;
6
7
final class ContainerAdapterFactory
8
{
9
    /**
10
     * @var array
11
     */
12
    private $config;
13
14
    public function __construct(array $config)
15
    {
16
        $this->config = $config;
17
    }
18
19
    /**
20
     * @param object $container
21
     *
22
     * @return void
0 ignored issues
show
Documentation introduced by
Should the return type not be object?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
23
     *
24
     * @throws UnknownContainerException
25
     */
26
    public function create($container)
27
    {
28
        $class = '';
29
30
        foreach ($this->config as $containerClass => $configuratorClass) {
31
            if (is_a($container, $containerClass)) {
32
                $class = $configuratorClass;
33
                break;
34
            }
35
        }
36
37
        if (!$class) {
38
            throw UnknownContainerException::fromContainerName(
39
                get_class($container),
40
                array_keys($this->config)
41
            );
42
        }
43
44
        $instance = new $class();
45
        $instance->setContainer($container);
46
47
        return $instance;
48
    }
49
}
50