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

BlastCoreExtension::mergeParameter()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.2
c 0
b 0
f 0
cc 4
eloc 11
nc 4
nop 4
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\ContainerBuilder;
16
use Symfony\Component\Config\FileLocator;
17
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
18
use Symfony\Component\DependencyInjection\Loader\FileLoader;
19
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
20
use Symfony\Component\Yaml\Yaml;
21
22
/**
23
 * This is the class that loads and manages your bundle configuration.
24
 *
25
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
26
 */
27
class BlastCoreExtension extends Extension
28
{
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function load(array $configs, ContainerBuilder $container)
33
    {
34
        $config = $this->processConfiguration($this->getConfiguration([], $container), $configs);
0 ignored issues
show
Documentation introduced by
$this->getConfiguration(array(), $container) is of type object|null, but the function expects a object<Symfony\Component...ConfigurationInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
35
        $this->initialize();
36
        $loader = $this->buildLoader($container);
37
38
        $this->loadServices($loader);
39
        $this->loadCodeGenerators($container, $config);
0 ignored issues
show
Unused Code introduced by
The call to the method Blast\Bundle\CoreBundle\...n::loadCodeGenerators() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
40
        $this->loadDataFixtures($container, $loader);
0 ignored issues
show
Unused Code introduced by
The call to the method Blast\Bundle\CoreBundle\...ion::loadDataFixtures() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
41
        $this->loadParameters($container);
42
        $this->loadSecurity($container);
0 ignored issues
show
Unused Code introduced by
The call to the method Blast\Bundle\CoreBundle\...tension::loadSecurity() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
43
        $this->loadSonataAdmin($container, $loader);
44
        $this->loadListeners($container, $config);
0 ignored issues
show
Unused Code introduced by
The call to the method Blast\Bundle\CoreBundle\...ension::loadListeners() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
45
        $this->doLoad($container, $loader, $config);
0 ignored issues
show
Unused Code introduced by
The call to the method Blast\Bundle\CoreBundle\...CoreExtension::doLoad() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
46
    }
47
48
    /**
49
     * @return self
50
     */
51
    public function initialize()
52
    {
53
        $rc = new \ReflectionClass($this);
54
        $this->dir = dirname($rc->getFileName());
0 ignored issues
show
Bug introduced by
The property dir does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
55
        $this->prefix = '/../Resources/config/';
0 ignored issues
show
Bug introduced by
The property prefix does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
56
        $this->bundlesPrefix = $this->prefix . 'bundles/';
0 ignored issues
show
Bug introduced by
The property bundlesPrefix does not seem to exist. Did you mean prefix?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
57
        $this->suffix = '.yml';
0 ignored issues
show
Bug introduced by
The property suffix does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
58
        $this->file = 'blast';
0 ignored issues
show
Bug introduced by
The property file does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
59
60
        return $this;
61
    }
62
63
    /**
64
     * the buildLoader returns the required FileLoader.
65
     *
66
     * @param ContainerBuilder $container
67
     *
68
     * @return FileLoader
69
     */
70
    public function buildLoader(ContainerBuilder $container)
71
    {
72
        return new YamlFileLoader($container, new FileLocator($this->dir . $this->prefix));
73
    }
74
75
    /**
76
     * This method is called during the self::load() process, to add the logic related to SonataAdmin.
77
     *
78
     * @param FileLoader $loader
79
     *
80
     * @return self
81
     */
82
    public function loadServices(FileLoader $loader)
83
    {
84
        // services, admin & config files
85
        foreach (['services', 'admin'] as $fileName) {
86
            if (file_exists($this->dir . $this->prefix . $fileName . $this->suffix)) {
87
                $loader->load($fileName . $this->suffix);
88
            }
89
        }
90
91
        return $this;
92
    }
93
94
    /**
95
     * This method is called after loading the services in the self::load() process, to load code generators.
96
     *
97
     * @param ContainerBuilder $container
98
     * @param array            $config
99
     *
100
     * @return self
101
     */
102
    public function loadCodeGenerators(ContainerBuilder $container, array $config)
0 ignored issues
show
Unused Code introduced by
The parameter $container is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $config is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
103
    {
104
        return $this;
105
    }
106
107
    /**
108
     * This method is called after loading the services in the self::load() process, to load data fixtures.
109
     *
110
     * @param ContainerBuilder $container
111
     * @param FileLoader       $loader
112
     *
113
     * @return self
114
     */
115
    public function loadDataFixtures(ContainerBuilder $container, FileLoader $loader)
0 ignored issues
show
Unused Code introduced by
The parameter $container is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $loader is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
116
    {
117
        return $this;
118
    }
119
120
    /**
121
     * This method is called after loading the services in the self::load() process, to load data fixtures.
122
     *
123
     * @param ContainerBuilder $container
124
     *
125
     * @return self
126
     */
127
    public function loadParameters(ContainerBuilder $container)
128
    {
129
        // the blast.yml
130
        if (file_exists($this->dir . $this->prefix . $this->file . $this->suffix)) {
131
            $this->mergeParameter('blast', $container, $this->dir . $this->prefix);
132
        }
133
134
        return $this;
135
    }
136
137
    /**
138
     * This method is called at the end of the self::load() process, to add security related logic.
139
     *
140
     * @param ContainerBuilder $container
141
     *
142
     * @return self
143
     */
144
    public function loadSecurity(ContainerBuilder $container)
0 ignored issues
show
Unused Code introduced by
The parameter $container is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
145
    {
146
        return $this;
147
    }
148
149
    /**
150
     * This method is called at the end of the self::load() process, to add the logic related to SonataAdmin.
151
     *
152
     * @param ContainerBuilder $container
153
     * @param FileLoader       $loader
154
     *
155
     * @return self
156
     */
157
    public function loadSonataAdmin(ContainerBuilder $container, FileLoader $loader)
0 ignored issues
show
Unused Code introduced by
The parameter $loader is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
158
    {
159
        if (file_exists($path = $this->dir . $this->bundlesPrefix . 'sonata_admin' . $this->suffix)) {
0 ignored issues
show
Bug introduced by
The property bundlesPrefix does not seem to exist. Did you mean prefix?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
160
            $configSonataAdmin = Yaml::parse(
161
                file_get_contents($path)
162
            );
163
164
            DefaultParameters::getInstance($container)->defineDefaultConfiguration($configSonataAdmin['default']);
165
        }
166
167
        return $this;
168
    }
169
170
    /**
171
     * This method is called during the self::load() process, to add the logic related to SonataAdmin.
172
     *
173
     * @param ContainerBuilder $container
174
     * @param array            $config
175
     *
176
     * @return self
177
     */
178
    public function loadListeners(ContainerBuilder $container, array $config)
0 ignored issues
show
Unused Code introduced by
The parameter $container is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $config is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
179
    {
180
        return $this;
181
    }
182
183
    /**
184
     * This method is called at the end of the self::load() process, to add any logic needed.
185
     *
186
     * @param ContainerBuilder $container
187
     * @param FileLoader       $loader
188
     * @param array            $config
189
     *
190
     * @return self
191
     */
192
    public function doLoad(ContainerBuilder $container, FileLoader $loader, array $config)
0 ignored issues
show
Unused Code introduced by
The parameter $container is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $loader is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $config is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
193
    {
194
        return $this;
195
    }
196
197
    /**
198
     * @param string           $var       the parameter name
199
     * @param ContainerBuilder $container
200
     * @param string           $dir
201
     * @param string           $file_name
202
     *
203
     * @return self
204
     */
205
    protected function mergeParameter($var, $container, $dir, $file_name = 'blast.yml')
206
    {
207
        $loader = new YamlFileLoader($newContainer = new ContainerBuilder(), new FileLocator($dir));
208
        $loader->load($file_name);
209
210
        if (!$container->hasParameter($var) || !is_array($container->getParameter($var))) {
211
            $container->setParameter($var, []);
212
        }
213
214
        if (!is_array($newContainer->getParameter($var))) {
215
            return $this;
216
        }
217
218
        $container->setParameter($var, array_merge(
219
            $container->getParameter($var),
220
            $newContainer->getParameter($var)
221
        ));
222
223
        return $this;
224
    }
225
}
226