Completed
Push — wip-platform ( c9fbdf...0e8c27 )
by
unknown
26:56
created

BlastCoreExtension::loadBlasts()   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 2
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->loadParameters($container, $loader);
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->loadBlasts($container, $loader);
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 View Code Duplication
    public function loadServices(FileLoader $loader)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
    {
84
        if (file_exists($this->dir . $this->prefix . 'services' . $this->suffix)) {
85
            $loader->load('services' . $this->suffix);
86
        }
87
88
        return $this;
89
    }
90
91
    /**
92
     * This method is called after loading the services in the self::load() process, to load code generators.
93
     *
94
     * @param ContainerBuilder $container
95
     * @param array            $config
96
     *
97
     * @return self
98
     */
99
    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...
100
    {
101
        return $this;
102
    }
103
104
    /**
105
     * This method is called after loading the services in the self::load() process, to load data fixtures.
106
     *
107
     * @param ContainerBuilder $container
108
     * @param FileLoader       $loader
109
     *
110
     * @return self
111
     */
112
    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...
113
    {
114
        return $this;
115
    }
116
117
    /**
118
     * @param ContainerBuilder $container
119
     *
120
     * @return self
121
     */
122
    public function loadBlasts(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...
123
    {
124
        // the blast.yml
125
        if (file_exists($this->dir . $this->prefix . $this->file . $this->suffix)) {
126
            $this->mergeParameter('blast', $container, $this->dir . $this->prefix);
127
        }
128
129
        return $this;
130
    }
131
132
    /**
133
     * @param ContainerBuilder $container
134
     *
135
     * @return self
136
     */
137 View Code Duplication
    public function loadParameters(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...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
138
    {
139
        // parameters.yml
140
        if (file_exists($this->dir . $this->prefix . 'parameters' . $this->suffix)) {
141
            $loader->load('parameters' . $this->suffix);
142
        }
143
144
        return $this;
145
    }
146
147
    /**
148
     * This method is called at the end of the self::load() process, to add security related logic.
149
     *
150
     * @param ContainerBuilder $container
151
     *
152
     * @return self
153
     */
154
    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...
155
    {
156
        return $this;
157
    }
158
159
    /**
160
     * This method is called at the end of the self::load() process, to add the logic related to SonataAdmin.
161
     *
162
     * @param ContainerBuilder $container
163
     * @param FileLoader       $loader
164
     *
165
     * @return self
166
     */
167
    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...
168
    {
169
        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...
170
            $configSonataAdmin = Yaml::parse(
171
                file_get_contents($path)
172
            );
173
174
            DefaultParameters::getInstance($container)->defineDefaultConfiguration($configSonataAdmin['default']);
175
        }
176
177
        return $this;
178
    }
179
180
    /**
181
     * This method is called during the self::load() process, to add the logic related to SonataAdmin.
182
     *
183
     * @param ContainerBuilder $container
184
     * @param array            $config
185
     *
186
     * @return self
187
     */
188
    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...
189
    {
190
        return $this;
191
    }
192
193
    /**
194
     * This method is called at the end of the self::load() process, to add any logic needed.
195
     *
196
     * @param ContainerBuilder $container
197
     * @param FileLoader       $loader
198
     * @param array            $config
199
     *
200
     * @return self
201
     */
202
    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...
203
    {
204
        return $this;
205
    }
206
207
    /**
208
     * @param string           $var       the parameter name
209
     * @param ContainerBuilder $container
210
     * @param string           $dir
211
     * @param string           $file_name
212
     *
213
     * @return self
214
     */
215
    protected function mergeParameter($var, $container, $dir, $file_name = 'blast.yml')
216
    {
217
        $loader = new YamlFileLoader($newContainer = new ContainerBuilder(), new FileLocator($dir));
218
        $loader->load($file_name);
219
220
        if (!$container->hasParameter($var) || !is_array($container->getParameter($var))) {
221
            $container->setParameter($var, []);
222
        }
223
224
        if (!is_array($newContainer->getParameter($var))) {
225
            return $this;
226
        }
227
228
        $container->setParameter($var, array_merge(
229
            $container->getParameter($var),
230
            $newContainer->getParameter($var)
231
        ));
232
233
        return $this;
234
    }
235
}
236