Completed
Pull Request — 1.0 (#638)
by
unknown
03:04
created

MappingPass   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
c 2
b 0
f 0
lcom 0
cbo 4
dl 0
loc 67
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B process() 0 61 5
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 (!isset($connections[$manager['connection']])) {
38
                throw new InvalidConfigurationException(
39
                    'There is no ES connection with the name: ' . $manager['connection']
40
                );
41
            }
42
43
            $managerName = strtolower($managerName);
44
            $connection = $connections[$manager['connection']];
45
46
            $managerDefinition = new Definition(
47
                'ONGR\ElasticsearchBundle\Service\Manager',
48
                [
49
                    $managerName,
50
                    $connection,
51
                    $analysis,
52
                    $manager,
53
                ]
54
            );
55
            $managerDefinition->setFactory(
56
                [
57
                    new Reference('es.manager_factory'),
58
                    'createManager',
59
                ]
60
            );
61
62
            $container->setDefinition(sprintf('es.manager.%s', $managerName), $managerDefinition);
63
64
            // Make es.manager.default as es.manager service.
65
            if ($managerName === 'default') {
66
                $container->setAlias('es.manager', 'es.manager.default');
67
            }
68
69
            $mappings = $collector->getMappings($manager['mappings']);
70
71
            // Building repository services.
72
            foreach ($mappings as $repositoryType => $repositoryDetails) {
73
                $repositoryDefinition = new Definition(
74
                    'ONGR\ElasticsearchBundle\Service\Repository',
75
                    [$repositoryDetails['namespace']]
76
                );
77
                $repositoryDefinition->setFactory(
78
                    [
79
                        new Reference(sprintf('es.manager.%s', $managerName)),
80
                        'getRepository',
81
                    ]
82
                );
83
84
                $repositoryId = sprintf('es.manager.%s.%s', $managerName, $repositoryType);
85
                $container->setDefinition($repositoryId, $repositoryDefinition);
86
            }
87
        }
88
    }
89
}
90