PluginPass::process()   B
last analyzed

Complexity

Conditions 10
Paths 25

Size

Total Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 40
rs 7.6666
c 0
b 0
f 0
cc 10
nc 25
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 eXpansion\Framework\Core\DependencyInjection\Compiler;
4
5
use eXpansion\Framework\Core\Services\PluginManager;
6
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
7
use Symfony\Component\DependencyInjection\ContainerBuilder;
8
9
/**
10
 * PluginPass to register all plugins to the plugin manager.
11
 *
12
 * @package eXpansion\Framework\Core\DependencyInjection\Compiler
13
 */
14
class PluginPass implements CompilerPassInterface
15
{
16
    /**
17
     * Register all data providers to the Manager.
18
     *
19
     * @param ContainerBuilder $container
20
     */
21
    public function process(ContainerBuilder $container)
22
    {
23
        if (!$container->has(PluginManager::class)) {
24
            return;
25
        }
26
27
        $pluginsData = [];
28
29
        // Find all Plugin services.
30
        $plugins = $container->findTaggedServiceIds('expansion.plugin');
31
        foreach ($plugins as $id => $tags) {
32
            $pluginsData[$id]['dataProviders'] = [];
33
            foreach ($tags as $attributes) {
34
                if (isset($attributes['data_provider'])) {
35
                    $pluginsData[$id]['dataProviders'][] = $attributes['data_provider'];
36
                }
37
            }
38
        }
39
40
        // Find the parent services.
41
        $plugins = $container->findTaggedServiceIds('expansion.plugin.parent');
42
        foreach ($plugins as $id => $tags) {
43
            foreach ($tags as $attributes) {
44
                $pluginsData[$id]['parent'][] = $attributes['parent'];
45
            }
46
        }
47
48
        // Get the data provider manager service definition to register data providers into.
49
        $definition = $container->getDefinition(PluginManager::class);
50
51
        foreach ($pluginsData as $pluginId => $data)
52
        {
53
            $definition->addMethodCall('registerPlugin', [
54
                    $pluginId,
55
                    empty($data['dataProviders']) ? [] : $data['dataProviders'],
56
                    empty($data['parent']) ? [] : $data['parent'],
57
                ]
58
            );
59
        }
60
    }
61
}
62