Completed
Push — master ( 212f0a...214901 )
by Lukas Kahwe
23s queued 10s
created

Compiler/RegisterPagerProvidersPass.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
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 16
    public function process(ContainerBuilder $container)
24
    {
25 16
        if (!$container->hasDefinition('fos_elastica.pager_provider_registry')) {
26 1
            return;
27
        }
28
29 15
        $defaultIndex = $container->getParameter('fos_elastica.default_index');
30 15
        $registry = $container->getDefinition('fos_elastica.pager_provider_registry');
31
32 15
        $registeredProviders = [];
33 15
        foreach ($container->findTaggedServiceIds('fos_elastica.pager_provider', true) as $id => $attributes) {
34 10
            foreach ($attributes as $attribute) {
35 10
                if (!isset($attribute['type'])) {
36 1
                    throw new \InvalidArgumentException(sprintf('Elastica provider "%s" must specify the "type" attribute.', $id));
37
                }
38
39 9
                $index = isset($attribute['index']) ? $attribute['index'] : $defaultIndex;
40 9
                $type = $attribute['type'];
41
42 9
                if (isset($registeredProviders[$index][$type])) {
43 1
                    throw new \InvalidArgumentException(sprintf(
44 1
                        'Cannot register provider "%s". The provider "%s" has been registered for same index "%s" and type "%s"',
45
                        $id,
46 1
                        $registeredProviders[$index][$type],
47
                        $index,
48
                        $type
49
                    ));
50
                }
51
52 9
                $providerDef = $container->getDefinition($id);
53 9
                if (!$providerDef->getFactory() && $providerDef->getClass()) {
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 3
                    $this->assertClassImplementsPagerProviderInterface($id, $container->getParameterBag()->resolveValue($providerDef->getClass()));
57
                }
58
59 8
                if (!$providerDef->isPublic()) {
60
                    throw new \InvalidArgumentException(sprintf('Elastica persister "%s" must be a public service', $id));
61
                }
62
63 8
                $registeredProviders[$index][$type] = $id;
64
            }
65
        }
66
67 12
        $registry->replaceArgument(0, $registeredProviders);
68 12
    }
69
70
    /**
71
     * @param $providerId
72
     * @param $providerClass
73
     *
74
     * @throws \InvalidArgumentException if provider service does not implement PagerProviderInterface
75
     */
76 3
    private function assertClassImplementsPagerProviderInterface($providerId, $providerClass)
77
    {
78 3
        $rc = new \ReflectionClass($providerClass);
79
80 3
        if (!$rc->implementsInterface(PagerProviderInterface::class)) {
81 1
            throw new \InvalidArgumentException(sprintf(
82 1
                'Elastica provider "%s" with class "%s" must implement "%s".',
83
                $providerId,
84
                $providerClass,
85 1
                PagerProviderInterface::class
86
            ));
87
        }
88 2
    }
89
}
90