Completed
Push — master ( fbc1fa...9bba21 )
by
unknown
14s
created

RegisterPagerProvidersPass::process()   C

Complexity

Conditions 8
Paths 10

Size

Total Lines 42
Code Lines 24

Duplication

Lines 42
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 42
loc 42
ccs 0
cts 33
cp 0
rs 5.3846
cc 8
eloc 24
nc 10
nop 1
crap 72
1
<?php
2
3
/*
4
 * This file is part of the FOSElasticaBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\ElasticaBundle\DependencyInjection\Compiler;
13
14
use FOS\ElasticaBundle\Provider\PagerProviderInterface;
15
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17
18 View Code Duplication
final class RegisterPagerProvidersPass implements CompilerPassInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23
    public function process(ContainerBuilder $container)
24
    {
25
        if (!$container->hasDefinition('fos_elastica.pager_provider_registry')) {
26
            return;
27
        }
28
29
        $defaultIndex = $container->getParameter('fos_elastica.default_index');
30
        $registry = $container->getDefinition('fos_elastica.pager_provider_registry');
31
32
        $registeredProviders = [];
33
        foreach ($container->findTaggedServiceIds('fos_elastica.pager_provider') as $id => $attributes) {
34
            foreach ($attributes as $attribute) {
35
                if (!isset($attribute['type'])) {
36
                    throw new \InvalidArgumentException(sprintf('Elastica provider "%s" must specify the "type" attribute.', $id));
37
                }
38
39
                $index = isset($attribute['index']) ? $attribute['index'] : $defaultIndex;
40
                $type = $attribute['type'];
41
42
                if (isset($registeredProviders[$index][$type])) {
43
                    throw new \InvalidArgumentException(sprintf(
44
                        'Cannot register provider "%s". The provider "%s" has been registered for same index "%s" and type "%s"',
45
                        $id,
46
                        $registeredProviders[$index][$type],
47
                        $index,
48
                        $type
49
                    ));
50
                }
51
52
                $providerDef = $container->getDefinition($id);
53
                if (!$providerDef->getFactory()) {
54
                    // You are on your own if you use a factory to create a provider.
55
                    // It would fail in runtime if the factory does not return a proper provider.
56
                    $this->assertClassImplementsPagerProviderInterface($id, $providerDef->getClass());
57
                }
58
59
                $registeredProviders[$index][$type] = $id;
60
            }
61
        }
62
63
        $registry->replaceArgument(0, $registeredProviders);
64
    }
65
66
    /**
67
     * @param $providerId
68
     * @param $providerClass
69
     *
70
     * @throws \InvalidArgumentException if provider service does not implement PagerProviderInterface
71
     */
72
    private function assertClassImplementsPagerProviderInterface($providerId, $providerClass)
73
    {
74
        $rc = new \ReflectionClass($providerClass);
75
76
        if (!$rc->implementsInterface(PagerProviderInterface::class)) {
77
            throw new \InvalidArgumentException(sprintf(
78
                'Elastica provider "%s" with class "%s" must implement "%s".',
79
                $providerId,
80
                $providerClass,
81
                PagerProviderInterface::class
82
            ));
83
        }
84
    }
85
}
86