Completed
Push — master ( 3fd00a...bd2156 )
by Simonas
03:03
created

MappingPass::process()   C

Complexity

Conditions 8
Paths 18

Size

Total Lines 75
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 75
rs 6.2413
cc 8
eloc 42
nc 18
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
            if (isset($connection['auth'])) {
50
                trigger_error(
51
                    '`auth` usage in elasticsearch bundle configuration is deprecated, ' .
52
                    'add your auth configuration directly in the host. This will be removed in v3.0. More: ' .
53
                    'https://www.elastic.co/guide/en/elasticsearch/client/php-api/2.0/_security.html',
54
                    E_USER_DEPRECATED
55
                );
56
            }
57
58
            $managerName = strtolower($managerName);
59
60
            $managerDefinition = new Definition(
61
                'ONGR\ElasticsearchBundle\Service\Manager',
62
                [
63
                    $managerName,
64
                    $connection,
65
                    $analysis,
66
                    $manager,
67
                ]
68
            );
69
            $managerDefinition->setFactory(
70
                [
71
                    new Reference('es.manager_factory'),
72
                    'createManager',
73
                ]
74
            );
75
76
            $container->setDefinition(sprintf('es.manager.%s', $managerName), $managerDefinition);
77
78
            // Make es.manager.default as es.manager service.
79
            if ($managerName === 'default') {
80
                $container->setAlias('es.manager', 'es.manager.default');
81
            }
82
83
            $mappings = $collector->getMappings($manager['mappings']);
84
85
            // Building repository services.
86
            foreach ($mappings as $repositoryType => $repositoryDetails) {
87
                $repositoryDefinition = new Definition(
88
                    'ONGR\ElasticsearchBundle\Service\Repository',
89
                    [$repositoryDetails['namespace']]
90
                );
91
                $repositoryDefinition->setFactory(
92
                    [
93
                        new Reference(sprintf('es.manager.%s', $managerName)),
94
                        'getRepository',
95
                    ]
96
                );
97
98
                $repositoryId = sprintf('es.manager.%s.%s', $managerName, $repositoryType);
99
                $container->setDefinition($repositoryId, $repositoryDefinition);
100
            }
101
        }
102
    }
103
}
104