Code Duplication    Length = 72-72 lines in 2 locations

src/DependencyInjection/Compiler/RegisterPagerProvidersPass.php 1 location

@@ 18-89 (lines=72) @@
15
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17
18
final class RegisterPagerProvidersPass implements CompilerPassInterface
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', true) 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() && $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
                    $this->assertClassImplementsPagerProviderInterface($id, $container->getParameterBag()->resolveValue($providerDef->getClass()));
57
                }
58
59
                if (!$providerDef->isPublic()) {
60
                    throw new \InvalidArgumentException(sprintf('Elastica persister "%s" must be a public service', $id));
61
                }
62
63
                $registeredProviders[$index][$type] = $id;
64
            }
65
        }
66
67
        $registry->replaceArgument(0, $registeredProviders);
68
    }
69
70
    /**
71
     * @param $providerId
72
     * @param $providerClass
73
     *
74
     * @throws \InvalidArgumentException if provider service does not implement PagerProviderInterface
75
     */
76
    private function assertClassImplementsPagerProviderInterface($providerId, $providerClass)
77
    {
78
        $rc = new \ReflectionClass($providerClass);
79
80
        if (!$rc->implementsInterface(PagerProviderInterface::class)) {
81
            throw new \InvalidArgumentException(sprintf(
82
                'Elastica provider "%s" with class "%s" must implement "%s".',
83
                $providerId,
84
                $providerClass,
85
                PagerProviderInterface::class
86
            ));
87
        }
88
    }
89
}
90

src/DependencyInjection/Compiler/RegisterPersistersPass.php 1 location

@@ 18-89 (lines=72) @@
15
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17
18
final class RegisterPersistersPass implements CompilerPassInterface
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23
    public function process(ContainerBuilder $container)
24
    {
25
        if (!$container->hasDefinition('fos_elastica.persister_registry')) {
26
            return;
27
        }
28
29
        $defaultIndex = $container->getParameter('fos_elastica.default_index');
30
        $registry = $container->getDefinition('fos_elastica.persister_registry');
31
32
        $registeredPersisters = [];
33
        foreach ($container->findTaggedServiceIds('fos_elastica.persister', true) as $id => $attributes) {
34
            foreach ($attributes as $attribute) {
35
                if (!isset($attribute['type'])) {
36
                    throw new \InvalidArgumentException(sprintf('Elastica persister "%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($registeredPersisters[$index][$type])) {
43
                    throw new \InvalidArgumentException(sprintf(
44
                        'Cannot register persister "%s". The persister "%s" has been registered for same index "%s" and type "%s"',
45
                        $id,
46
                        $registeredPersisters[$index][$type],
47
                        $index,
48
                        $type
49
                    ));
50
                }
51
52
                $persisterDef = $container->getDefinition($id);
53
                if (!$persisterDef->getFactory() && $persisterDef->getClass()) {
54
                    // You are on your own if you use a factory to create a persister.
55
                    // It would fail in runtime if the factory does not return a proper persister.
56
                    $this->assertClassImplementsPersisterInterface($id, $container->getParameterBag()->resolveValue($persisterDef->getClass()));
57
                }
58
59
                if (!$persisterDef->isPublic()) {
60
                    throw new \InvalidArgumentException(sprintf('Elastica persister "%s" must be a public service', $id));
61
                }
62
63
                $registeredPersisters[$index][$type] = $id;
64
            }
65
        }
66
67
        $registry->replaceArgument(0, $registeredPersisters);
68
    }
69
70
    /**
71
     * @param $persisterId
72
     * @param $persisterClass
73
     *
74
     * @throws \InvalidArgumentException if persister service does not implement ObjectPersisterInterface
75
     */
76
    private function assertClassImplementsPersisterInterface($persisterId, $persisterClass)
77
    {
78
        $rc = new \ReflectionClass($persisterClass);
79
80
        if (!$rc->implementsInterface(ObjectPersisterInterface::class)) {
81
            throw new \InvalidArgumentException(sprintf(
82
                'Elastica persister "%s" with class "%s" must implement "%s".',
83
                $persisterId,
84
                $persisterClass,
85
                ObjectPersisterInterface::class
86
            ));
87
        }
88
    }
89
}
90