Completed
Pull Request — master (#52)
by Tom
04:13
created

ApplicationConfigServiceProvider::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 1
eloc 7
nc 1
nop 2
1
<?php
2
3
namespace TomPHP\ContainerConfigurator\League;
4
5
use League\Container\ServiceProvider\AbstractServiceProvider;
6
use TomPHP\ContainerConfigurator\ApplicationConfig;
7
8
final class ApplicationConfigServiceProvider extends AbstractServiceProvider
9
{
10
    /**
11
     * @var ApplicationConfig
12
     */
13
    private $config;
14
15
    /**
16
     * @var string
17
     */
18
    private $prefix;
19
20
    /**
21
     * @param ApplicationConfig $config
22
     * @param string            $prefix
23
     */
24
    public function __construct(ApplicationConfig $config, $prefix)
25
    {
26
        $this->prefix   = $prefix;
27
        $this->config   = $config;
28
        $this->provides = array_map(
29
            function ($key) {
30
                return $this->keyPrefix() . $key;
31
            },
32
            $config->getKeys()
33
        );
34
    }
35
36
    public function register()
37
    {
38
        $prefix = $this->keyPrefix();
39
40
        foreach ($this->config as $key => $value) {
41
            $this->container->share($prefix . $key, function () use ($value) {
42
                return $value;
43
            });
44
        }
45
    }
46
47
    /**
48
     * @return string
49
     */
50
    private function keyPrefix()
51
    {
52
        if (empty($this->prefix)) {
53
            return '';
54
        }
55
56
        return $this->prefix . $this->config->getSeparator();
57
    }
58
}
59