Completed
Pull Request — master (#615)
by
unknown
02:55
created

ManagerFactory::createManager()   B

Complexity

Conditions 7
Paths 24

Size

Total Lines 52
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 2
Metric Value
c 4
b 0
f 2
dl 0
loc 52
rs 7.2396
cc 7
eloc 31
nc 24
nop 4

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