Completed
Push — master ( 18b9a5...070941 )
by Simonas
02:16
created

MappingPass   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 6
c 1
b 0
f 1
lcom 0
cbo 3
dl 0
loc 65
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B process() 0 59 6
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
        $analysis = $container->getParameter('es.analysis');
30
        $managers = $container->getParameter('es.managers');
31
32
        $collector = $container->get('es.metadata_collector');
33
34
        foreach ($managers as $managerName => $manager) {
35
            $connection = $manager['index'];
36
            $managerName = strtolower($managerName);
37
38
            $managerDefinition = new Definition(
39
                'ONGR\ElasticsearchBundle\Service\Manager',
40
                [
41
                    $managerName,
42
                    $connection,
43
                    $analysis,
44
                    $manager,
45
                ]
46
            );
47
            $managerDefinition->setFactory(
48
                [
49
                    new Reference('es.manager_factory'),
50
                    'createManager',
51
                ]
52
            );
53
54
            $container->setDefinition(sprintf('es.manager.%s', $managerName), $managerDefinition);
55
56
            // Make es.manager.default as es.manager service.
57
            if ($managerName === 'default') {
58
                $container->setAlias('es.manager', 'es.manager.default');
59
            }
60
61
            $mappings = $collector->getMappings($manager['mappings']);
62
63
            // Building repository services.
64
            foreach ($mappings as $repositoryType => $repositoryDetails) {
65
                $repositoryDefinition = new Definition(
66
                    'ONGR\ElasticsearchBundle\Service\Repository',
67
                    [$repositoryDetails['namespace']]
68
                );
69
70
                if (isset($repositoryDetails['directory_name']) && $managerName == 'default') {
71
                    $container->get('es.document_finder')->setDocumentDir($repositoryDetails['directory_name']);
72
                }
73
74
                $repositoryDefinition->setFactory(
75
                    [
76
                        new Reference(sprintf('es.manager.%s', $managerName)),
77
                        'getRepository',
78
                    ]
79
                );
80
81
                $repositoryId = sprintf('es.manager.%s.%s', $managerName, $repositoryType);
82
                $container->setDefinition($repositoryId, $repositoryDefinition);
83
            }
84
        }
85
    }
86
}
87