Completed
Push — wip-platform ( 1b7118...2b724b )
by
unknown
03:32
created

DefaultParameters::defineDefaultConfiguration()   C

Complexity

Conditions 8
Paths 5

Size

Total Lines 25
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 11
nc 5
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Blast Project package.
5
 *
6
 * Copyright (C) 2015-2017 Libre Informatique
7
 *
8
 * This file is licenced under the GNU LGPL v3.
9
 * For the full copyright and license information, please view the LICENSE.md
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Blast\Bundle\CoreBundle\DependencyInjection;
14
15
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
16
use Symfony\Component\DependencyInjection\ContainerInterface;
17
18
/**
19
 * Class DefaultParameters.
20
 */
21
class DefaultParameters implements ContainerAwareInterface
22
{
23
    /**
24
     * @var
25
     */
26
    private static $instance;
27
28
    /**
29
     * @var ContainerInterface
30
     */
31
    private $container;
32
33
    /**
34
     * @param ContainerInterface $container
35
     */
36
    private function __construct(ContainerInterface $container)
37
    {
38
        $this->setContainer($container);
39
    }
40
41
    /**
42
     * defineDefaultConfiguration.
43
     *
44
     * @param array $parameters
45
     */
46
    public function defineDefaultConfiguration(array $parameters)
47
    {
48
        foreach ($parameters as $parameterKey => $change) {
49
            // retrieve current defined parameters
50
            $containerParameters = $this->container->getParameter($parameterKey);
51
52
            // replacing parameter only if defined in yml file
53
            if (array_key_exists('replace', $change)) {
54
                // default parameters in an array, so checking its content and replacing matching keys
55
                if (is_array($containerParameters) && is_array($change['default'])) {
56
                    foreach ($containerParameters as $defaultKey => $defaultValue) {
57
                        if (in_array($defaultValue, $change['default'])) {
58
                            $containerParameters[$defaultKey] = $change['replace'][$defaultKey];
59
                        }
60
                    }
61
                } elseif ($change['default'] == $containerParameters) {
62
                    // default parameters in a string, simply replacing it
63
                    $containerParameters = $change['replace'];
64
                }
65
66
                // overriding parameters with our default values
67
                $this->container->setParameter($parameterKey, $containerParameters);
68
            }
69
        }
70
    }
71
72
    /**
73
     * parameterExists.
74
     *
75
     * @param $name
76
     *
77
     * @return bool|mixed false if not defined, the parameter if exists
78
     */
79
    public function parameterExists($name)
80
    {
81
        if ($this->container->hasParameter($name)) {
82
            return $this->container->getParameter($name);
83
        } else {
84
            return false;
85
        }
86
    }
87
88
    /**
89
     * @return self
90
     */
91
    public static function getInstance(ContainerInterface $container)
92
    {
93
        if (self::$instance == null) {
94
            self::$instance = new self($container);
95
        }
96
97
        return self::$instance;
98
    }
99
100
    /**
101
     * Sets the Container.
102
     *
103
     * @param ContainerInterface|null $container A ContainerInterface instance or null
104
     *
105
     * @api
106
     */
107
    public function setContainer(ContainerInterface $container = null)
108
    {
109
        $this->container = $container;
110
    }
111
}
112