Completed
Push — master ( fff0f0...89663a )
by De Cramer
02:26
created

DataProviderPass::process()   F

Complexity

Conditions 16
Paths 541

Size

Total Lines 69
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 69
rs 3.326
c 0
b 0
f 0
cc 16
eloc 37
nc 541
nop 1

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 eXpansion\Framework\Core\DependencyInjection\Compiler;
4
5
use eXpansion\Framework\Core\Services\DataProviderManager;
6
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
7
use Symfony\Component\DependencyInjection\ContainerBuilder;
8
9
/**
10
 * DataProviderPass Register all data providers to the Manager.
11
 *
12
 * @package eXpansion\Framework\Core\DependencyInjection\Compiler
13
 */
14
class DataProviderPass 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('expansion.framework.core.services.data_provider_manager')) {
24
            return;
25
        }
26
27
        // Get the data provider manager service definition to register plugins into.
28
        $dpmDefinition = $container->getDefinition('expansion.framework.core.services.data_provider_manager');
29
        $pmDefinition = $container->getDefinition('expansion.framework.core.services.plugin_manager');
30
31
        $providerData = [];
32
33
        // Get provider service names.
34
        $dataProviders = $container->findTaggedServiceIds('expansion.data_provider');
35
        foreach ($dataProviders as $id => $tags) {
36
            foreach ($tags as $attributes) {
37
                $providerData[$id]['provider'] = $attributes['provider'];
38
                $providerData[$id]['interface'] = $attributes['interface'];
39
            }
40
        }
41
42
        // Get compatibility information for each provider
43
        $dataProviders = $container->findTaggedServiceIds('expansion.data_provider.compatibility');
44
        foreach ($dataProviders as $id => $tags) {
45
            foreach ($tags as $attributes) {
46
                $providerData[$id]['compatibility'][] = [
47
                    'title' => isset($attributes['title']) ? $attributes['title'] : DataProviderManager::COMPATIBLE_ALL,
48
                    'mode' => isset($attributes['mode']) ? $attributes['mode'] : DataProviderManager::COMPATIBLE_ALL,
49
                    'script' => isset($attributes['script']) ? $attributes['script'] : DataProviderManager::COMPATIBLE_ALL,
50
                ];
51
            }
52
        }
53
54
        // Get base events the data provider needs to listen to.
55
        $dataProviders = $container->findTaggedServiceIds('expansion.data_provider.listener');
56
        foreach ($dataProviders as $id => $tags) {
57
            foreach ($tags as $attributes) {
58
                $providerData[$id]['listener'][$attributes['event_name']] = $attributes['method'];
59
            }
60
        }
61
62
        // Get parent plugins the data provider requires.
63
        $plugins = $container->findTaggedServiceIds('expansion.data_provider.parent');
64
        foreach ($plugins as $id => $tags) {
65
            foreach ($tags as $attributes) {
66
                $providerData[$id]['parent'][] = $attributes['parent'];
67
            }
68
        }
69
70
        // Finally register collected data.
71
        foreach ($providerData as $id => $data) {
72
            $dpmDefinition->addMethodCall('registerDataProvider', [
73
                    $id,
74
                    $data['provider'],
75
                    $data['interface'],
76
                    $data['compatibility'],
77
                    !empty($data['listener']) ? $data['listener'] : [],
78
                ]
79
            );
80
81
            $pmDefinition->addMethodCall('registerPlugin', [
82
                    $id,
83
                    [],
84
                    isset($data['parent']) ? $data['parent'] : [],
85
                    $data['provider'],
86
                ]
87
            );
88
        }
89
    }
90
}