Completed
Push — wip-platform ( 03cba6...2999ab )
by
unknown
05:53 queued 02:49
created

BlastCoreExtension::loadParameters()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
/*
4
 *
5
 * Copyright (C) 2015-2017 Libre Informatique
6
 *
7
 * This file is licenced under the GNU LGPL v3.
8
 * For the full copyright and license information, please view the LICENSE.md
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Blast\Bundle\CoreBundle\DependencyInjection;
13
14
use Symfony\Component\DependencyInjection\ContainerBuilder;
15
use Symfony\Component\Config\FileLocator;
16
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
17
use Symfony\Component\DependencyInjection\Loader\FileLoader;
18
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
19
use Symfony\Component\Yaml\Yaml;
20
21
/**
22
 * This is the class that loads and manages your bundle configuration.
23
 *
24
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
25
 */
26
class BlastCoreExtension extends Extension
27
{
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function load(array $configs, ContainerBuilder $container)
32
    {
33
        $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...
34
        $this->initialize();
35
        $loader = $this->buildLoader($container);
36
37
        $this->loadServices($loader);
38
        $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...
39
        $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...
40
        $this->loadParameters($container);
41
        $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...
42
        $this->loadSonataAdmin($container, $loader);
43
        $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...
44
        $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...
45
    }
46
47
    /**
48
     * @return self
49
     */
50
    public function initialize()
51
    {
52
        $rc = new \ReflectionClass($this);
53
        $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...
54
        $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...
55
        $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...
56
        $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...
57
        $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...
58
59
        return $this;
60
    }
61
62
    /**
63
     * the buildLoader returns the required FileLoader.
64
     *
65
     * @param ContainerBuilder $container
66
     *
67
     * @return FileLoader
68
     */
69
    public function buildLoader(ContainerBuilder $container)
70
    {
71
        return new YamlFileLoader($container, new FileLocator($this->dir . $this->prefix));
72
    }
73
74
    /**
75
     * This method is called during the self::load() process, to add the logic related to SonataAdmin.
76
     *
77
     * @param FileLoader $loader
78
     *
79
     * @return self
80
     */
81
    public function loadServices(FileLoader $loader)
82
    {
83
        // services, admin & config files
84
        foreach (['services', 'admin'] as $fileName) {
85
            if (file_exists($this->dir . $this->prefix . $fileName . $this->suffix)) {
86
                $loader->load($fileName . $this->suffix);
87
            }
88
        }
89
90
        return $this;
91
    }
92
93
    /**
94
     * This method is called after loading the services in the self::load() process, to load code generators.
95
     *
96
     * @param ContainerBuilder $container
97
     * @param array            $config
98
     *
99
     * @return self
100
     */
101
    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...
102
    {
103
        return $this;
104
    }
105
106
    /**
107
     * This method is called after loading the services in the self::load() process, to load data fixtures.
108
     *
109
     * @param ContainerBuilder $container
110
     * @param FileLoader       $loader
111
     *
112
     * @return self
113
     */
114
    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...
115
    {
116
        return $this;
117
    }
118
119
    /**
120
     * This method is called after loading the services in the self::load() process, to load data fixtures.
121
     *
122
     * @param ContainerBuilder $container
123
     *
124
     * @return self
125
     */
126
    public function loadParameters(ContainerBuilder $container)
127
    {
128
        // the blast.yml
129
        if (file_exists($this->dir . $this->prefix . $this->file . $this->suffix)) {
130
            $this->mergeParameter('blast', $container, $this->dir . $this->prefix);
131
        }
132
133
        return $this;
134
    }
135
136
    /**
137
     * This method is called at the end of the self::load() process, to add security related logic.
138
     *
139
     * @param ContainerBuilder $container
140
     *
141
     * @return self
142
     */
143
    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...
144
    {
145
        return $this;
146
    }
147
148
    /**
149
     * This method is called at the end of the self::load() process, to add the logic related to SonataAdmin.
150
     *
151
     * @param ContainerBuilder $container
152
     * @param FileLoader       $loader
153
     *
154
     * @return self
155
     */
156
    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...
157
    {
158
        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...
159
            $configSonataAdmin = Yaml::parse(
160
                file_get_contents($path)
161
            );
162
163
            DefaultParameters::getInstance($container)->defineDefaultConfiguration($configSonataAdmin['default']);
164
        }
165
166
        return $this;
167
    }
168
169
    /**
170
     * This method is called during the self::load() process, to add the logic related to SonataAdmin.
171
     *
172
     * @param ContainerBuilder $container
173
     * @param array            $config
174
     *
175
     * @return self
176
     */
177
    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...
178
    {
179
        return $this;
180
    }
181
182
    /**
183
     * This method is called at the end of the self::load() process, to add any logic needed.
184
     *
185
     * @param ContainerBuilder $container
186
     * @param FileLoader       $loader
187
     * @param array            $config
188
     *
189
     * @return self
190
     */
191
    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...
192
    {
193
        return $this;
194
    }
195
196
    /**
197
     * @param string           $var       the parameter name
198
     * @param ContainerBuilder $container
199
     * @param string           $dir
200
     * @param string           $file_name
201
     *
202
     * @return self
203
     */
204
    protected function mergeParameter($var, $container, $dir, $file_name = 'blast.yml')
205
    {
206
        $loader = new YamlFileLoader($newContainer = new ContainerBuilder(), new FileLocator($dir));
207
        $loader->load($file_name);
208
209
        if (!$container->hasParameter($var) || !is_array($container->getParameter($var))) {
210
            $container->setParameter($var, []);
211
        }
212
213
        if (!is_array($newContainer->getParameter($var))) {
214
            return $this;
215
        }
216
217
        $container->setParameter($var, array_merge(
218
            $container->getParameter($var),
219
            $newContainer->getParameter($var)
220
        ));
221
222
        return $this;
223
    }
224
}
225