Completed
Push — master ( dc5e6b...6fa286 )
by Christophe
13s
created

RegisterPersistersPass::process()   D

Complexity

Conditions 10
Paths 14

Size

Total Lines 46
Code Lines 26

Duplication

Lines 46
Ratio 100 %

Code Coverage

Tests 25
CRAP Score 10

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 46
loc 46
ccs 25
cts 25
cp 1
rs 4.983
c 1
b 0
f 0
cc 10
eloc 26
nc 14
nop 1
crap 10

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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