Completed
Pull Request — master (#31)
by Tom
03:36
created

Configurator   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 8
c 1
b 0
f 1
lcom 0
cbo 1
dl 0
loc 40
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A addApplicationConfig() 0 10 3
B addServiceConfig() 0 22 4
A addInflectorConfig() 0 3 1
1
<?php
2
3
namespace TomPHP\ConfigServiceProvider\Pimple;
4
5
use Pimple\Container;
6
use ReflectionClass;
7
use TomPHP\ConfigServiceProvider\ApplicationConfig;
8
use TomPHP\ConfigServiceProvider\ContainerConfigurator;
9
use TomPHP\ConfigServiceProvider\InflectorConfig;
10
use TomPHP\ConfigServiceProvider\ServiceConfig;
11
12
final class Configurator implements ContainerConfigurator
13
{
14
    public function addApplicationConfig($container, ApplicationConfig $config, $prefix = 'config')
15
    {
16
        if (!empty($prefix)) {
17
            $prefix .= $config->getSeparator();
18
        }
19
20
        foreach ($config as $key => $value) {
21
            $container[$prefix . $key] = $value;
22
        }
23
    }
24
25
    public function addServiceConfig($container, ServiceConfig $config)
26
    {
27
        foreach ($config as $definition) {
28
            $factory = function () use ($definition) {
29
                $reflection = new ReflectionClass($definition->getClass());
30
31
                $instance = $reflection->newInstanceArgs($definition->getArguments());
32
33
                foreach ($definition->getMethods() as $name => $args) {
34
                    call_user_func_array([$instance, $name], $args);
35
                }
36
37
                return $instance;
38
            };
39
40
            if (!$definition->isSingleton()) {
41
                $factory = $container->factory($factory);
42
            }
43
44
            $container[$definition->getName()] = $factory;
45
        }
46
    }
47
48
    public function addInflectorConfig($container, InflectorConfig $config)
49
    {
50
    }
51
}
52