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

ApplicationConfigServiceProvider   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 0
loc 55
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A register() 0 10 2
A keyPrefix() 0 8 2
1
<?php
2
3
namespace TomPHP\ContainerConfigurator\League;
4
5
use Assert\Assertion;
6
use InvalidArgumentException;
7
use League\Container\ServiceProvider\AbstractServiceProvider;
8
use TomPHP\ContainerConfigurator\ApplicationConfig;
9
10
final class ApplicationConfigServiceProvider extends AbstractServiceProvider
11
{
12
    /**
13
     * @var ApplicationConfig
14
     */
15
    private $config;
16
17
    /**
18
     * @var string
19
     */
20
    private $prefix;
21
22
    /**
23
     * @param ApplicationConfig $config
24
     * @param string            $prefix
25
     *
26
     * @throws InvalidArgumentException
27
     */
28
    public function __construct(ApplicationConfig $config, $prefix)
29
    {
30
        Assertion::string($prefix);
31
32
        $this->prefix   = $prefix;
33
        $this->config   = $config;
34
        $this->provides = array_map(
35
            function ($key) {
36
                return $this->keyPrefix() . $key;
37
            },
38
            $config->getKeys()
39
        );
40
    }
41
42
    public function register()
43
    {
44
        $prefix = $this->keyPrefix();
45
46
        foreach ($this->config as $key => $value) {
47
            $this->container->share($prefix . $key, function () use ($value) {
48
                return $value;
49
            });
50
        }
51
    }
52
53
    /**
54
     * @return string
55
     */
56
    private function keyPrefix()
57
    {
58
        if (empty($this->prefix)) {
59
            return '';
60
        }
61
62
        return $this->prefix . $this->config->getSeparator();
63
    }
64
}
65