Completed
Push — 1.2 ( b13b4a )
by Simonas
10s
created

ManagerFactory   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 4
Bugs 0 Features 2
Metric Value
wmc 11
lcom 1
cbo 3
dl 0
loc 129
rs 10
c 4
b 0
f 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A setEventDispatcher() 0 4 1
A setStopwatch() 0 4 1
B createManager() 0 56 8
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\Service;
13
14
use Elasticsearch\ClientBuilder;
15
use ONGR\ElasticsearchBundle\Mapping\MetadataCollector;
16
use ONGR\ElasticsearchBundle\Result\Converter;
17
use Psr\Log\LoggerInterface;
18
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
19
use Symfony\Component\Stopwatch\Stopwatch;
20
21
/**
22
 * Elasticsearch Manager factory class.
23
 */
24
class ManagerFactory
25
{
26
    /**
27
     * @var MetadataCollector
28
     */
29
    private $metadataCollector;
30
31
    /**
32
     * @var Converter
33
     */
34
    private $converter;
35
36
    /**
37
     * @var LoggerInterface
38
     */
39
    private $logger;
40
41
    /**
42
     * @var LoggerInterface
43
     */
44
    private $tracer;
45
46
    /**
47
     * @var EventDispatcherInterface
48
     */
49
    private $eventDispatcher;
50
51
    /**
52
     * @var Stopwatch
53
     */
54
    private $stopwatch;
55
56
    /**
57
     * @param MetadataCollector $metadataCollector Metadata collector service.
58
     * @param Converter         $converter         Converter service to transform arrays to objects and visa versa.
59
     * @param LoggerInterface   $tracer
60
     * @param LoggerInterface   $logger
61
     */
62
    public function __construct($metadataCollector, $converter, $tracer = null, $logger = null)
63
    {
64
        $this->metadataCollector = $metadataCollector;
65
        $this->converter = $converter;
66
        $this->tracer = $tracer;
67
        $this->logger = $logger;
68
    }
69
70
    /**
71
     * @param EventDispatcherInterface   $eventDispatcher
72
     */
73
    public function setEventDispatcher(EventDispatcherInterface $eventDispatcher)
74
    {
75
        $this->eventDispatcher = $eventDispatcher;
76
    }
77
78
    /**
79
     * @param Stopwatch $stopwatch
80
     */
81
    public function setStopwatch(Stopwatch $stopwatch)
82
    {
83
        $this->stopwatch = $stopwatch;
84
    }
85
86
    /**
87
     * Factory function to create a manager instance.
88
     *
89
     * @param string $managerName   Manager name.
90
     * @param array  $connection    Connection configuration.
91
     * @param array  $analysis      Analyzers, filters and tokenizers config.
92
     * @param array  $managerConfig Manager configuration.
93
     *
94
     * @return Manager
95
     */
96
    public function createManager($managerName, $connection, $analysis, $managerConfig)
97
    {
98
        foreach (array_keys($analysis) as $analyzerType) {
99
            foreach ($connection['analysis'][$analyzerType] as $name) {
100
                $connection['settings']['analysis'][$analyzerType][$name] = $analysis[$analyzerType][$name];
101
            }
102
        }
103
        unset($connection['analysis']);
104
105
        if (!isset($connection['settings']['number_of_replicas'])) {
106
            $connection['settings']['number_of_replicas'] = 0;
107
        }
108
109
        if (!isset($connection['settings']['number_of_shards'])) {
110
            $connection['settings']['number_of_shards'] = 1;
111
        }
112
113
        $mappings = $this->metadataCollector->getClientMapping($managerConfig['mappings']);
114
115
        $client = ClientBuilder::create();
116
        $client->setHosts($connection['hosts']);
117
        $client->setTracer($this->tracer);
118
119
        if ($this->logger && $managerConfig['logger']['enabled']) {
120
            $client->setLogger($this->logger);
121
        }
122
123
        $indexSettings = [
124
            'index' => $connection['index_name'],
125
            'body' => array_filter(
126
                [
127
                    'settings' => $connection['settings'],
128
                    'mappings' => $mappings,
129
                ]
130
            ),
131
        ];
132
133
        $manager = new Manager(
134
            $managerName,
135
            $managerConfig,
136
            $client->build(),
137
            $indexSettings,
138
            $this->metadataCollector,
139
            $this->converter
140
        );
141
142
        if (isset($this->stopwatch)) {
143
            $manager->setStopwatch($this->stopwatch);
144
        }
145
146
        $manager->setCommitMode($managerConfig['commit_mode']);
147
        $manager->setEventDispatcher($this->eventDispatcher);
148
        $manager->setBulkCommitSize($managerConfig['bulk_size']);
149
150
        return $manager;
151
    }
152
}
153