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

ConfiguratorFactory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 5
c 2
b 0
f 2
lcom 1
cbo 1
dl 0
loc 36
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B create() 0 23 4
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