ApplicationConfigServiceProvider::keyPrefix()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
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
/**
11
 * @internal
12
 */
13
final class ApplicationConfigServiceProvider extends AbstractServiceProvider
14
{
15
    /**
16
     * @var ApplicationConfig
17
     */
18
    private $config;
19
20
    /**
21
     * @var string
22
     */
23
    private $prefix;
24
25
    /**
26
     * @param ApplicationConfig $config
27
     * @param string            $prefix
28
     *
29
     * @throws InvalidArgumentException
30
     */
31
    public function __construct(ApplicationConfig $config, $prefix)
32
    {
33
        Assertion::string($prefix);
34
35
        $this->prefix   = $prefix;
36
        $this->config   = $config;
37
        $this->provides = array_map(
38
            function ($key) {
39
                return $this->keyPrefix() . $key;
40
            },
41
            $config->getKeys()
42
        );
43
    }
44
45
    public function register()
46
    {
47
        $prefix = $this->keyPrefix();
48
49
        foreach ($this->config as $key => $value) {
50
            $this->container->share($prefix . $key, function () use ($value) {
51
                return $value;
52
            });
53
        }
54
    }
55
56
    /**
57
     * @return string
58
     */
59
    private function keyPrefix()
60
    {
61
        if (empty($this->prefix)) {
62
            return '';
63
        }
64
65
        return $this->prefix . $this->config->getSeparator();
66
    }
67
}
68