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

RegisterPagerProvidersPass   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 68
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
c 0
b 0
f 0
lcom 0
cbo 2
dl 68
loc 68
ccs 0
cts 45
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
C process() 42 42 8
A assertClassImplementsPagerProviderInterface() 13 13 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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