ApplicationConfigServiceProvider   A
last analyzed

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
/**
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