Completed
Push — master ( c7af1e...46d491 )
by Konstantinos
06:03 queued 02:12
created

ConfigExtension::getAlias()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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 1
    protected function loadInternal(array $config, ContainerBuilder $container)
28
    {
29 1
        $language = new ExpressionLanguage();
30
31
        // Evaluate match score modifiers -- this converts strings like "2/3" to
32
        // the corresponding number
33 1
        foreach ($config['league']['duration'] as &$modifier) {
34 1
            $modifier = $language->evaluate($modifier);
35
        }
36
37 1
        $this->store('bzion', $config);
38 1
        $container->getParameterBag()->add($this->conf);
39 1
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 1
    public function getAlias()
45
    {
46 1
        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 1
    private function store($name, $value)
57
    {
58 1
        if (is_array($value)) {
59 1
            foreach ($value as $key => $val) {
60 1
                if (is_int($key)) {
61
                    // Non-associative arrays are stored as arrays and don't get
62
                    // expanded further into parameters
63 1
                    $this->conf[$name][$key] = $val;
64
                } else {
65 1
                    $this->store("$name.$key", $val);
66
                }
67
            }
68
        } else {
69 1
            $this->conf[$name] = $value;
70
        }
71 1
    }
72
}
73