Completed
Pull Request — master (#28)
by Tom
27:09
created

ConfigServiceProvider::getKeys()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace TomPHP\ConfigServiceProvider;
4
5
use League\Container\ServiceProvider\AbstractServiceProvider;
6
use League\Container\ServiceProvider\BootableServiceProviderInterface;
7
use League\Container\ServiceProvider\ServiceProviderInterface;
8
use TomPHP\ConfigServiceProvider\Exception\EntryDoesNotExistException;
9
use TomPHP\ConfigServiceProvider\League\AggregateServiceProvider;
10
use League\Container\ContainerInterface;
11
12
final class ConfigServiceProvider extends AbstractServiceProvider implements
13
    BootableServiceProviderInterface
14
{
15
    const DEFAULT_PREFIX         = 'config';
16
    const DEFAULT_SEPARATOR      = '.';
17
    const DEFAULT_INFLECTORS_KEY = 'inflectors';
18
    const DEFAULT_DI_KEY         = 'di';
19
20
    const SETTING_PREFIX    = 'prefix';
21
    const SETTING_SEPARATOR = 'separator';
22
23
    /**
24
     * @var array
25
     */
26
    private $config;
27
28
    /**
29
     * @var AggregateServiceProvider
30
     */
31
    private $subProviders;
32
33
    /**
34
     * @api
35
     *
36
     * @param array|ApplicationConfig $config
37
     * @param array                   $settings
38
     *
39
     * @return ConfigServiceProvider
40
     */
41
    public static function fromConfig($config, array $settings = [])
42
    {
43
        return new self(
44
            $config,
45
            self::getSettingOrDefault(self::SETTING_PREFIX, $settings, self::DEFAULT_PREFIX),
46
            self::getSettingOrDefault(self::SETTING_SEPARATOR, $settings, self::DEFAULT_SEPARATOR)
47
        );
48
    }
49
50
    /**
51
     * @api
52
     *
53
     * @param string[] $patterns
54
     * @param array    $settings
55
     *
56
     * @return ConfigServiceProvider
57
     */
58
    public static function fromFiles(array $patterns, array $settings = [])
59
    {
60
        $separator = self::getSettingOrDefault(self::SETTING_SEPARATOR, $settings, self::DEFAULT_SEPARATOR);
61
62
        return self::fromConfig(ApplicationConfig::fromFiles($patterns, $separator), $settings);
63
    }
64
65
    /**
66
     * @api
67
     *
68
     * @param array|ApplicationConfig    $config
69
     * @param string                     $prefix
70
     * @param string                     $separator
71
     * @param ServiceProviderInterface[] $subProviders
0 ignored issues
show
Bug introduced by
There is no parameter named $subProviders. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
72
     */
73
    public function __construct(
74
        $config,
75
        $prefix = self::DEFAULT_PREFIX,
76
        $separator = self::DEFAULT_SEPARATOR
77
    ) {
78
        $this->config = [];
79
80
        $config = ($config instanceof ApplicationConfig) ? $config : new ApplicationConfig($config, $separator);
81
82
        $configurator = new League\Configurator();
83
84
        $configurator->addApplicationConfig($config, $prefix);
85
86
        if (isset($config[self::DEFAULT_DI_KEY])) {
87
            $configurator->addServiceConfig(new ServiceConfig($config[self::DEFAULT_DI_KEY]));
88
        }
89
90
91
        if (isset($config[self::DEFAULT_INFLECTORS_KEY])) {
92
            $configurator->addInflectorConfig(new InflectorConfig($config[self::DEFAULT_INFLECTORS_KEY]));
93
        }
94
95
        $this->subProviders = $configurator->getServiceProvider();
96
    }
97
98
    public function provides($service = null)
99
    {
100
        return $this->subProviders->provides($service);
101
    }
102
103
    public function register()
104
    {
105
        $this->subProviders->register();
106
    }
107
108
    public function boot()
109
    {
110
        $this->subProviders->boot();
111
    }
112
113
    public function setContainer(ContainerInterface $container)
114
    {
115
        $this->subProviders->setContainer($container);
116
    }
117
118
    public function getContainer()
119
    {
120
        return $this->subProviders->getContainer();
121
    }
122
123
    /**
124
     * @param string $name
125
     * @param array  $settings
126
     * @param mixed  $default
127
     *
128
     * @return mixed
129
     */
130
    private static function getSettingOrDefault($name, array $settings, $default)
131
    {
132
        return isset($settings[$name]) ? $settings[$name] : $default;
133
    }
134
}
135