Completed
Push — master ( 09a07f...8de6a8 )
by Simonas
01:54
created

MappingPass::process()   B

Complexity

Conditions 7
Paths 10

Size

Total Lines 66
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 66
rs 7.0832
c 1
b 0
f 1
cc 7
eloc 36
nc 10
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\Config\Definition\Exception\InvalidConfigurationException;
15
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17
use Symfony\Component\DependencyInjection\Definition;
18
use Symfony\Component\DependencyInjection\Reference;
19
20
/**
21
 * Compiles elastic search data.
22
 */
23
class MappingPass implements CompilerPassInterface
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function process(ContainerBuilder $container)
29
    {
30
        $analysis = $container->getParameter('es.analysis');
31
        $connections = $container->getParameter('es.connections');
32
        $managers = $container->getParameter('es.managers');
33
34
        $collector = $container->get('es.metadata_collector');
35
36
        foreach ($managers as $managerName => $manager) {
37
            if (!empty($manager['index'])) {
38
                $connection = $manager['index'];
39
            } else {
40
                if (!isset($manager['connection']) || !isset($connections[$manager['connection']])) {
41
                    throw new InvalidConfigurationException(
42
                        'There is an error in the ES connection configuration of the manager: ' . $managerName
43
                    );
44
                }
45
46
                $connection = $connections[$manager['connection']];
47
            }
48
49
            $managerName = strtolower($managerName);
50
51
            $managerDefinition = new Definition(
52
                'ONGR\ElasticsearchBundle\Service\Manager',
53
                [
54
                    $managerName,
55
                    $connection,
56
                    $analysis,
57
                    $manager,
58
                ]
59
            );
60
            $managerDefinition->setFactory(
61
                [
62
                    new Reference('es.manager_factory'),
63
                    'createManager',
64
                ]
65
            );
66
67
            $container->setDefinition(sprintf('es.manager.%s', $managerName), $managerDefinition);
68
69
            // Make es.manager.default as es.manager service.
70
            if ($managerName === 'default') {
71
                $container->setAlias('es.manager', 'es.manager.default');
72
            }
73
74
            $mappings = $collector->getMappings($manager['mappings']);
75
76
            // Building repository services.
77
            foreach ($mappings as $repositoryType => $repositoryDetails) {
78
                $repositoryDefinition = new Definition(
79
                    'ONGR\ElasticsearchBundle\Service\Repository',
80
                    [$repositoryDetails['namespace']]
81
                );
82
                $repositoryDefinition->setFactory(
83
                    [
84
                        new Reference(sprintf('es.manager.%s', $managerName)),
85
                        'getRepository',
86
                    ]
87
                );
88
89
                $repositoryId = sprintf('es.manager.%s.%s', $managerName, $repositoryType);
90
                $container->setDefinition($repositoryId, $repositoryDefinition);
91
            }
92
        }
93
    }
94
}
95