Completed
Push — master ( c26f0f...c7af1e )
by Konstantinos
18:52
created

ConfigExtension   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 1
cbo 4
dl 0
loc 55
ccs 0
cts 21
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A loadInternal() 0 13 2
A getAlias() 0 4 1
A store() 0 16 4
1
<?php
2
/**
3
 * This file makes sure that we can read the configuration values in the code
4
 *
5
 * @license    https://github.com/allejo/bzion/blob/master/LICENSE.md GNU General Public License Version 3
6
 */
7
8
namespace BZIon\Config;
9
10
use Symfony\Component\DependencyInjection\ContainerBuilder;
11
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
12
use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension;
13
14
/**
15
 * A configuration extension for bzion which makes sure that configuration
16
 * parameters are accessible in the code
17
 */
18
class ConfigExtension extends ConfigurableExtension
19
{
20
    private $conf = array();
21
22
    /**
23
     * {@inheritdoc}
24
     *
25
     * Loads the configuration from the yml file into container parameters
26
     */
27
    protected function loadInternal(array $config, ContainerBuilder $container)
28
    {
29
        $language = new ExpressionLanguage();
30
31
        // Evaluate match score modifiers -- this converts strings like "2/3" to
32
        // the corresponding number
33
        foreach ($config['league']['duration'] as &$modifier) {
34
            $modifier = $language->evaluate($modifier);
35
        }
36
37
        $this->store('bzion', $config);
38
        $container->getParameterBag()->add($this->conf);
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function getAlias()
45
    {
46
        return 'bzion';
47
    }
48
49
    /**
50
     * Convert the yaml array into proper symfony container parameters
51
     *
52
     * @param  string $name  The name of the root parameter
53
     * @param  mixed  $value The value to store in that parameter
54
     * @return void
55
     */
56
    private function store($name, $value)
57
    {
58
        if (is_array($value)) {
59
            foreach ($value as $key => $val) {
60
                if (is_int($key)) {
61
                    // Non-associative arrays are stored as arrays and don't get
62
                    // expanded further into parameters
63
                    $this->conf[$name][$key] = $val;
64
                } else {
65
                    $this->store("$name.$key", $val);
66
                }
67
            }
68
        } else {
69
            $this->conf[$name] = $value;
70
        }
71
    }
72
}
73