Completed
Pull Request — 1.1 (#636)
by
unknown
02:42
created

ManagerFactory::createManager()   B

Complexity

Conditions 8
Paths 48

Size

Total Lines 55
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 2
Metric Value
c 4
b 0
f 2
dl 0
loc 55
rs 7.4033
cc 8
eloc 32
nc 48
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\Stopwatch\Stopwatch;
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 Stopwatch
47
     */
48
    private $stopwatch;
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 LoggerInterface   $tracer
54
     * @param LoggerInterface   $logger
55
     */
56
    public function __construct($metadataCollector, $converter, $tracer = null, $logger = null)
57
    {
58
        $this->metadataCollector = $metadataCollector;
59
        $this->converter = $converter;
60
        $this->tracer = $tracer;
61
        $this->logger = $logger;
62
    }
63
64
    /**
65
     * @param Stopwatch $stopwatch
66
     */
67
    public function setStopwatch(Stopwatch $stopwatch)
68
    {
69
        $this->stopwatch = $stopwatch;
70
    }
71
72
    /**
73
     * Factory function to create a manager instance.
74
     *
75
     * @param string $managerName   Manager name.
76
     * @param array  $connection    Connection configuration.
77
     * @param array  $analysis      Analyzers, filters and tokenizers config.
78
     * @param array  $managerConfig Manager configuration.
79
     *
80
     * @return Manager
81
     */
82
    public function createManager($managerName, $connection, $analysis, $managerConfig)
83
    {
84
        foreach (array_keys($analysis) as $analyzerType) {
85
            foreach ($connection['analysis'][$analyzerType] as $name) {
86
                $connection['settings']['analysis'][$analyzerType][$name] = $analysis[$analyzerType][$name];
87
            }
88
        }
89
        unset($connection['analysis']);
90
91
        if (!isset($connection['settings']['number_of_replicas'])) {
92
            $connection['settings']['number_of_replicas'] = 0;
93
        }
94
95
        if (!isset($connection['settings']['number_of_shards'])) {
96
            $connection['settings']['number_of_shards'] = 1;
97
        }
98
99
        $mappings = $this->metadataCollector->getClientMapping($managerConfig['mappings']);
100
101
        $client = ClientBuilder::create();
102
        $client->setHosts($connection['hosts']);
103
        $client->setTracer($this->tracer);
104
105
        if ($this->logger && $managerConfig['logger']['enabled']) {
106
            $client->setLogger($this->logger);
107
        }
108
109
        $indexSettings = [
110
            'index' => $connection['index_name'],
111
            'body' => array_filter(
112
                [
113
                    'settings' => $connection['settings'],
114
                    'mappings' => $mappings,
115
                ]
116
            ),
117
        ];
118
119
        $manager = new Manager(
120
            $managerName,
121
            $managerConfig,
122
            $client->build(),
123
            $indexSettings,
124
            $this->metadataCollector,
125
            $this->converter
126
        );
127
128
        if (isset($this->stopwatch)) {
129
            $manager->setStopwatch($this->stopwatch);
130
        }
131
132
        $manager->setCommitMode($managerConfig['commit_mode']);
133
        $manager->setBulkCommitSize($managerConfig['bulk_size']);
134
135
        return $manager;
136
    }
137
}
138