Code Duplication    Length = 68-68 lines in 2 locations

src/DependencyInjection/Compiler/RegisterPagerProvidersPass.php 1 location

@@ 18-85 (lines=68) @@
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') 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

src/DependencyInjection/Compiler/RegisterPersistersPass.php 1 location

@@ 18-85 (lines=68) @@
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') 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()) {
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, $persisterDef->getClass());
57
                }
58
59
                $registeredPersisters[$index][$type] = $id;
60
            }
61
        }
62
63
        $registry->replaceArgument(0, $registeredPersisters);
64
    }
65
66
    /**
67
     * @param $persisterId
68
     * @param $persisterClass
69
     *
70
     * @throws \InvalidArgumentException if persister service does not implement ObjectPersisterInterface
71
     */
72
    private function assertClassImplementsPersisterInterface($persisterId, $persisterClass)
73
    {
74
        $rc = new \ReflectionClass($persisterClass);
75
76
        if (!$rc->implementsInterface(ObjectPersisterInterface::class)) {
77
            throw new \InvalidArgumentException(sprintf(
78
                'Elastica persister "%s" with class "%s" must implement "%s".',
79
                $persisterId,
80
                $persisterClass,
81
                ObjectPersisterInterface::class
82
            ));
83
        }
84
    }
85
}