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\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
|
|||
19 | { |
||
20 | /** |
||
21 | * {@inheritdoc} |
||
22 | */ |
||
23 | 16 | public function process(ContainerBuilder $container) |
|
24 | { |
||
25 | 16 | if (!$container->hasDefinition('fos_elastica.persister_registry')) { |
|
26 | 1 | return; |
|
27 | } |
||
28 | |||
29 | 15 | $defaultIndex = $container->getParameter('fos_elastica.default_index'); |
|
30 | 15 | $registry = $container->getDefinition('fos_elastica.persister_registry'); |
|
31 | |||
32 | 15 | $registeredPersisters = []; |
|
33 | 15 | 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 | $id, |
||
46 | 1 | $registeredPersisters[$index][$type], |
|
47 | $index, |
||
48 | $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 | 3 | $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 | 8 | $registeredPersisters[$index][$type] = $id; |
|
64 | } |
||
65 | } |
||
66 | |||
67 | 12 | $registry->replaceArgument(0, $registeredPersisters); |
|
68 | 12 | } |
|
69 | |||
70 | /** |
||
71 | * @param $persisterId |
||
72 | * @param $persisterClass |
||
73 | * |
||
74 | * @throws \InvalidArgumentException if persister service does not implement ObjectPersisterInterface |
||
75 | */ |
||
76 | 3 | private function assertClassImplementsPersisterInterface($persisterId, $persisterClass) |
|
77 | { |
||
78 | 3 | $rc = new \ReflectionClass($persisterClass); |
|
79 | |||
80 | 3 | if (!$rc->implementsInterface(ObjectPersisterInterface::class)) { |
|
81 | 1 | throw new \InvalidArgumentException(sprintf( |
|
82 | 1 | 'Elastica persister "%s" with class "%s" must implement "%s".', |
|
83 | $persisterId, |
||
84 | $persisterClass, |
||
85 | 1 | ObjectPersisterInterface::class |
|
86 | )); |
||
87 | } |
||
88 | 2 | } |
|
89 | } |
||
90 |
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.