MigrationExtensionPass::loadExtensions()   D
last analyzed

Complexity

Conditions 10
Paths 43

Size

Total Lines 37
Code Lines 24

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 37
rs 4.8196
cc 10
eloc 24
nc 43
nop 1

How to fix   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 RDV\Bundle\MigrationBundle\DependencyInjection\Compiler;
4
5
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
6
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
7
use Symfony\Component\DependencyInjection\ContainerBuilder;
8
use Symfony\Component\DependencyInjection\Reference;
9
10
class MigrationExtensionPass implements CompilerPassInterface
11
{
12
    const MANAGER_SERVICE_KEY = 'rdv_migration.migrations.extension_manager';
13
    const TAG                 = 'rdv_migration.extension';
14
15
    /**
16
     * {@inheritdoc}
17
     */
18
    public function process(ContainerBuilder $container)
19
    {
20
        if (!$container->hasDefinition(self::MANAGER_SERVICE_KEY)) {
21
            return;
22
        }
23
        $storageDefinition = $container->getDefinition(self::MANAGER_SERVICE_KEY);
24
25
        $extensions = $this->loadExtensions($container);
26
        foreach ($extensions as $extensionName => $extensionServiceId) {
27
            $storageDefinition->addMethodCall(
28
                'addExtension',
29
                [$extensionName, new Reference($extensionServiceId)]
30
            );
31
        }
32
    }
33
34
    /**
35
     * Load migration extensions services
36
     *
37
     * @param ContainerBuilder $container
38
     * @return array
39
     * @throws InvalidConfigurationException
40
     */
41
    protected function loadExtensions(ContainerBuilder $container)
42
    {
43
        $taggedServices = $container->findTaggedServiceIds(self::TAG);
44
        $extensions     = [];
45
        foreach ($taggedServices as $id => $tagAttributes) {
46
            if ($container->hasDefinition($id)) {
47
                $container->getDefinition($id)->setPublic(false);
48
            }
49
            $priority = 0;
50
            $extensionName = null;
51
            foreach ($tagAttributes as $attributes) {
52
                if (!empty($attributes['priority'])) {
53
                    $priority = (int)$attributes['priority'];
54
                }
55
                if (!isset($attributes['extension_name']) || empty($attributes['extension_name'])) {
56
                    throw new InvalidConfigurationException(
57
                        sprintf('Tag attribute "extension_name" is required for "%s" service', $id)
58
                    );
59
                }
60
                $extensionName = $attributes['extension_name'];
61
            }
62
            if (!isset($extensions[$extensionName])) {
63
                $extensions[$extensionName] = [];
64
            }
65
            $extensions[$extensionName][$priority] = $id;
66
        }
67
68
        $result = [];
69
        foreach ($extensions as $name => $extension) {
70
            if (count($extension) > 1) {
71
                krsort($extension);
72
            }
73
            $result[$name] = array_pop($extension);
74
        }
75
76
        return $result;
77
    }
78
}
79