Completed
Push — master ( b8b393...c56780 )
by GBProd
02:34
created

DataProviderCompilerPass   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 6
Bugs 3 Features 0
Metric Value
wmc 15
c 6
b 3
f 0
lcom 1
cbo 3
dl 0
loc 87
ccs 53
cts 53
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A validateTag() 0 11 2
A isTagIncorrect() 0 8 4
A processProvider() 0 9 2
A validateIsAProvider() 0 11 2
A isNotAProvider() 0 6 1
A registerProvider() 0 12 1
A process() 0 18 3
1
<?php
2
3
namespace GBProd\ElasticsearchDataProviderBundle\DependencyInjection\Compiler;
4
5
use GBProd\ElasticsearchDataProviderBundle\DataProvider\DataProviderInterface;
6
use GBProd\ElasticsearchDataProviderBundle\DataProvider\RegistryEntry;
7
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
8
use Symfony\Component\DependencyInjection\ContainerBuilder;
9
use Symfony\Component\DependencyInjection\Definition;
10
use Symfony\Component\DependencyInjection\Reference;
11
12
/**
13
 * Compiler path to register DataProviders
14
 *
15
 * @author gbprod <[email protected]>
16
 */
17
class DataProviderCompilerPass implements CompilerPassInterface
18
{
19
    /**
20
     * {@inheritdoc}
21
     */
22 4
    public function process(ContainerBuilder $container)
23
    {
24 4
        if (!$container->hasDefinition('gbprod.elasticsearch_dataprovider.registry')) {
25 1
            return;
26
        }
27
28 3
        $registry = $container->getDefinition(
29
            'gbprod.elasticsearch_dataprovider.registry'
30 3
        );
31
32 3
        $providers = $container->findTaggedServiceIds(
33
            'elasticsearch.dataprovider'
34 3
        );
35
36 3
        foreach ($providers as $providerId => $tags) {
37 3
            $this->processProvider($container, $registry, $providerId, $tags);
38 1
        }
39 1
    }
40
41 3
    private function processProvider(ContainerBuilder $container, $registry, $providerId, $tags)
42
    {
43 3
        $this->validateIsAProvider($container, $providerId);
44
45 2
        foreach ($tags as $tag) {
46 2
            $this->validateTag($tag, $providerId);
47 1
            $this->registerProvider($registry, $providerId, $tag);
48 1
        }
49 1
    }
50
51 3
    private function validateIsAProvider(ContainerBuilder $container, $providerId)
52
    {
53 3
        if ($this->isNotAProvider($container->getDefinition($providerId))) {
54 1
            throw new \InvalidArgumentException(
55 1
                sprintf(
56 1
                    'DataProvider "%s" must implements DataProviderInterface.',
57
                    $providerId
58 1
                )
59 1
            );
60
        }
61 2
    }
62
63 3
    private function isNotAProvider(Definition $definition)
64
    {
65 3
        $reflection = new \ReflectionClass($definition->getClass());
66
67 3
        return !$reflection->implementsInterface(DataProviderInterface::class);
68
    }
69
70 2
    private function validateTag($tag, $providerId)
71
    {
72 2
        if ($this->isTagIncorrect($tag)) {
73 1
            throw new \InvalidArgumentException(
74 1
                sprintf('DataProvider "%s" must specify the "index"'.
75 1
                    ' and "type" attribute.',
76
                    $providerId
77 1
                )
78 1
            );
79
        }
80 1
    }
81
82 2
    private function isTagIncorrect($tag)
83
    {
84 2
        return !isset($tag['type'])
85 2
            || !isset($tag['index'])
86 2
            || empty($tag['index'])
87 1
            || empty($tag['type'])
88 2
        ;
89
    }
90
91 1
    private function registerProvider($registry, $providerId, $tag)
92
    {
93 1
        $entryDefinition = new Definition(
94 1
            RegistryEntry::class,
95
            [
96 1
                new Reference($providerId),
97 1
                $tag['index'],
98 1
                $tag['type']
99 1
            ]
100 1
        );
101 1
        $registry->addMethodCall('add', [$entryDefinition]);
102
    }
103
}