PimcoreRadBrickExtension::load()   B
last analyzed

Complexity

Conditions 8
Paths 36

Size

Total Lines 61
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 39
CRAP Score 8

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 38
c 1
b 0
f 1
dl 0
loc 61
ccs 39
cts 39
cp 1
rs 8.0675
cc 8
nc 36
nop 2
crap 8

How to fix   Long Method   

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 Khusseini\PimcoreRadBrickBundle\DependencyInjection;
4
5
use Khusseini\PimcoreRadBrickBundle\AreabrickConfigurator;
6
use Khusseini\PimcoreRadBrickBundle\AreabrickRenderer;
7
use Khusseini\PimcoreRadBrickBundle\Areabricks\SimpleBrick;
8
use Symfony\Component\Config\FileLocator;
9
use Symfony\Component\DependencyInjection\ChildDefinition;
10
use Symfony\Component\DependencyInjection\ContainerBuilder;
11
use Symfony\Component\DependencyInjection\Definition;
12
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
13
use Symfony\Component\DependencyInjection\Reference;
14
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
15
16
class PimcoreRadBrickExtension extends Extension
17
{
18
    /**
19
     * @param array<array> $configs
20
     *
21
     * @return void
22
     */
23 3
    public function load(array $configs, ContainerBuilder $container)
24
    {
25 3
        $loader = new YamlFileLoader($container, new FileLocator(\dirname(__DIR__).'/Resources/config'));
26 3
        $loader->load('services.yml');
27
28 3
        $configuration = new Configuration();
29 3
        $config = $this->processConfiguration($configuration, $configs);
30
31 3
        $configurators = [];
32 3
        $ids = $container->findTaggedServiceIds('radbrick.configurator');
33 3
        foreach ($ids as $id => $tags) {
34 3
            $configurators[] = new Reference($id);
35
        }
36
37 3
        $datasources = $config['datasources'];
38 3
        foreach ($datasources as $key => $datasource) {
39 1
            $datasources[$key]['service_id'] = new Reference($datasource['service_id']);
40
        }
41 3
        $config['datasources'] = $datasources;
42
43 3
        $configurator = new Definition(
44 3
            AreabrickConfigurator::class,
45
            [
46 3
            $config,
47 3
            $configurators,
48
            ]
49
        );
50 3
        $container->setDefinition(AreabrickConfigurator::class, $configurator);
51
52 3
        $rendererDefinition = new Definition(AreabrickRenderer::class, [
53 3
            new Reference(AreabrickConfigurator::class),
54 3
            new Reference('pimcore.templating.tag_renderer'),
55
        ]);
56
57 3
        $container->setDefinition(AreabrickRenderer::class, $rendererDefinition);
58
59 3
        $areabricks = $config['areabricks'];
60 3
        foreach ($areabricks as $id => $areabrickConfig) {
61 3
            $definitionId = 'radbrick.'.$id;
62 3
            $parent = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $parent is dead and can be removed.
Loading history...
63 3
            $options = $areabrickConfig['options'] ?: [];
64
65 3
            $target = null;
66 3
            if ($class = @$areabrickConfig['class']) {
67 1
                $target = new ChildDefinition($class);
68 1
                $target->setClass($class);
69
            }
70
71 3
            if (!$target) {
72 2
                $target = new Definition(SimpleBrick::class);
73
            }
74
75 3
            $target->setArgument('$name', $id);
76 3
            $target->setArgument('$areabrickRenderer', new Reference(AreabrickRenderer::class));
77 3
            $target->addMethodCall('configure', [$options]);
78
79 3
            if (!$target->hasTag('pimcore.area.brick')) {
80 3
                $target->addTag('pimcore.area.brick', ['id' => $id]);
81
            }
82
83 3
            $container->setDefinition($definitionId, $target);
84
        }
85 3
    }
86
}
87