Passed
Push — master ( d093e3...81fa39 )
by Dāvis
03:13 queued 25s
created

SludioHelperExtension::load()   C

Complexity

Conditions 14
Paths 72

Size

Total Lines 51
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 34
nc 72
nop 2
dl 0
loc 51
rs 5.6426
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Sludio\HelperBundle\DependencyInjection;
4
5
use Symfony\Component\Config\FileLocator;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
8
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
9
use Sludio\HelperBundle\DependencyInjection\Component\ConfigureInterface;
10
11
/**
12
 * This is the class that loads and manages your bundle configuration.
13
 *
14
 * @link http://symfony.com/doc/current/cookbook/bundles/extension.html
15
 */
16
class SludioHelperExtension extends Extension
17
{
18
19
    public function getAlias()
20
    {
21
        return 'sludio_helper';
22
    }
23
24
    private function checkComponent($key)
25
    {
26
        $className = 'Sludio\\HelperBundle\\DependencyInjection\\Component\\'.ucfirst($key);
27
        if (class_exists($className)) {
28
            $class = new $className();
29
            if ($class instanceof ConfigureInterface) {
30
                return $class;
31
            }
32
        }
33
34
        return null;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function load(array $configs, ContainerBuilder $container)
41
    {
42
        $configuration = new Configuration($this->getAlias());
43
        $config = $this->processConfiguration($configuration, $configs);
44
45
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
46
        $files = [
47
            'components.yml',
48
            'parameters.yml',
49
            'services.yml',
50
        ];
51
        foreach ($files as $file) {
52
            if (file_exists(__DIR__.'/../Resources/config/'.$file)) {
53
                $loader->load($file);
54
            }
55
        }
56
57
        foreach ($config['other'] as $key => $other) {
58
            if (is_array($other)) {
59
                foreach ($other as $variable => $value) {
60
                    $container->setParameter($this->getAlias().'.'.$key.'.'.$variable, $config['other'][$key][$variable]);
61
                }
62
            } else {
63
                $container->setParameter($this->getAlias().'.'.$key, $config['other'][$key]);
64
            }
65
        }
66
67
        foreach ($config['extensions'] as $key => $extension) {
68
            if (!isset($extension['enabled']) || $extension['enabled'] !== true) {
69
                continue;
70
            }
71
            $iterator = 0;
72
            foreach ($extension as $variable => $value) {
73
                $iterator++;
74
                if ($iterator === 1) {
75
                    $files = [
76
                        'components.yml',
77
                        'parameters.yml',
78
                        'services.yml',
79
                    ];
80
                    $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../'.ucfirst($key).'/Resources/config'));
81
                    foreach ($files as $file) {
82
                        if (file_exists(__DIR__.'/../'.ucfirst($key).'/Resources/config/'.$file)) {
83
                            $loader->load($file);
84
                        }
85
                    }
86
                }
87
                $container->setParameter($this->getAlias().'.'.$key.'.'.$variable, $config['extensions'][$key][$variable]);
88
            }
89
            if ($component = $this->checkComponent($key)) {
90
                $component->configure($container, $this->getAlias());
91
            }
92
        }
93
    }
94
}
95