Completed
Pull Request — 5.0 (#720)
by
unknown
02:01
created

MappingPass::process()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 52
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 52
rs 8.9408
cc 4
eloc 27
nc 5
nop 1

How to fix   Long Method   

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 ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[email protected]>
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 ONGR\ElasticsearchBundle\DependencyInjection\Compiler;
13
14
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
use Symfony\Component\DependencyInjection\Definition;
17
use Symfony\Component\DependencyInjection\Reference;
18
19
/**
20
 * Compiles elastic search data.
21
 */
22
class MappingPass implements CompilerPassInterface
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function process(ContainerBuilder $container)
28
    {
29
        $managers = $container->getParameter('es.managers');
30
31
        $collector = $container->get('es.metadata_collector');
32
33
        foreach ($managers as $managerName => $manager) {
34
            $connection = $manager['index'];
35
            $managerName = strtolower($managerName);
36
37
            $managerDefinition = new Definition(
38
                'ONGR\ElasticsearchBundle\Service\Manager',
39
                [
40
                    $managerName,
41
                    $connection,
42
                    $manager,
43
                ]
44
            );
45
            $managerDefinition->setFactory(
46
                [
47
                    new Reference('es.manager_factory'),
48
                    'createManager',
49
                ]
50
            );
51
52
            $container->setDefinition(sprintf('es.manager.%s', $managerName), $managerDefinition);
53
54
            // Make es.manager.default as es.manager service.
55
            if ($managerName === 'default') {
56
                $container->setAlias('es.manager', 'es.manager.default');
57
            }
58
59
            $mappings = $collector->getMappings($manager['mappings']);
60
61
            // Building repository services.
62
            foreach ($mappings as $repositoryType => $repositoryDetails) {
63
                $repositoryDefinition = new Definition(
64
                    'ONGR\ElasticsearchBundle\Service\Repository',
65
                    [$repositoryDetails['namespace']]
66
                );
67
                $repositoryDefinition->setFactory(
68
                    [
69
                        new Reference(sprintf('es.manager.%s', $managerName)),
70
                        'getRepository',
71
                    ]
72
                );
73
74
                $repositoryId = sprintf('es.manager.%s.%s', $managerName, $repositoryType);
75
                $container->setDefinition($repositoryId, $repositoryDefinition);
76
            }
77
        }
78
    }
79
}
80