Completed
Pull Request — master (#33)
by Tom
02:24
created

ConfiguratorFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace TomPHP\ConfigServiceProvider;
4
5
use TomPHP\ConfigServiceProvider\Exception\UnknownContainerException;
6
7
final class ConfiguratorFactory
8
{
9
    /**
10
     * @var array
11
     */
12
    private $config;
13
14
    public function __construct(array $config)
15
    {
16
        $this->config = $config;
17
    }
18
19
    public function create($container)
20
    {
21
        $class = '';
22
23
        foreach ($this->config as $containerClass => $configuratorClass) {
24
            if (is_a($container, $containerClass)) {
25
                $class = $configuratorClass;
26
                break;
27
            }
28
        }
29
30
        if (!$class) {
31
            throw UnknownContainerException::fromContainerName(
32
                get_class($container),
33
                array_keys($this->config)
34
            );
35
        }
36
37
        $instance = new $class();
38
        $instance->setContainer($container);
39
40
        return $instance;
41
    }
42
}
43