DataProviderPass::process()   F
last analyzed

Complexity

Conditions 16
Paths 541

Size

Total Lines 69

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 69
rs 2.0375
c 0
b 0
f 0
cc 16
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 eXpansion\Framework\Core\Services\PluginManager;
7
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
8
use Symfony\Component\DependencyInjection\ContainerBuilder;
9
10
/**
11
 * DataProviderPass Register all data providers to the Manager.
12
 *
13
 * @package eXpansion\Framework\Core\DependencyInjection\Compiler
14
 */
15
class DataProviderPass implements CompilerPassInterface
16
{
17
    /**
18
     * Register all data providers to the Manager.
19
     *
20
     * @param ContainerBuilder $container
21
     */
22
    public function process(ContainerBuilder $container)
23
    {
24
        if (!$container->has(DataProviderManager::class)) {
25
            return;
26
        }
27
28
        // Get the data provider manager service definition to register plugins into.
29
        $dpmDefinition = $container->getDefinition(DataProviderManager::class);
30
        $pmDefinition = $container->getDefinition(PluginManager::class);
31
32
        $providerData = [];
33
34
        // Get provider service names.
35
        $dataProviders = $container->findTaggedServiceIds('expansion.dataprovider');
36
        foreach ($dataProviders as $id => $tags) {
37
            foreach ($tags as $attributes) {
38
                $providerData[$id]['provider'] = $attributes['provider'];
39
                $providerData[$id]['interface'] = $attributes['interface'];
40
            }
41
        }
42
43
        // Get compatibility information for each provider
44
        $dataProviders = $container->findTaggedServiceIds('expansion.dataprovider.compatibility');
45
        foreach ($dataProviders as $id => $tags) {
46
            foreach ($tags as $attributes) {
47
                $providerData[$id]['compatibility'][] = [
48
                    'title' => isset($attributes['title']) ? $attributes['title'] : DataProviderManager::COMPATIBLE_ALL,
49
                    'gamemode' => isset($attributes['gamemode']) ? $attributes['gamemode'] : DataProviderManager::COMPATIBLE_ALL,
50
                    'script' => isset($attributes['script']) ? strtolower($attributes['script']) : DataProviderManager::COMPATIBLE_ALL,
51
                ];
52
            }
53
        }
54
55
        // Get base events the data provider needs to listen to.
56
        $dataProviders = $container->findTaggedServiceIds('expansion.dataprovider.listener');
57
        foreach ($dataProviders as $id => $tags) {
58
            foreach ($tags as $attributes) {
59
                $providerData[$id]['listener'][$attributes['event_name']] = $attributes['method'];
60
            }
61
        }
62
63
        // Get parent plugins the data provider requires.
64
        $plugins = $container->findTaggedServiceIds('expansion.dataprovider.parent');
65
        foreach ($plugins as $id => $tags) {
66
            foreach ($tags as $attributes) {
67
                $providerData[$id]['parent'][] = $attributes['parent'];
68
            }
69
        }
70
71
        // Finally register collected data.
72
        foreach ($providerData as $id => $data) {
73
            $dpmDefinition->addMethodCall('registerDataProvider', [
74
                    $id,
75
                    $data['provider'],
76
                    $data['interface'],
77
                    $data['compatibility'],
78
                    !empty($data['listener']) ? $data['listener'] : [],
79
                ]
80
            );
81
82
            $pmDefinition->addMethodCall('registerPlugin', [
83
                    $id,
84
                    [],
85
                    isset($data['parent']) ? $data['parent'] : [],
86
                    $data['provider'],
87
                ]
88
            );
89
        }
90
    }
91
}
92