Completed
Push — master ( e504c7...3fd00a )
by Simonas
17:33
created

ManagerFactory::createManager()   C

Complexity

Conditions 10
Paths 192

Size

Total Lines 65
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 3
Metric Value
c 5
b 0
f 3
dl 0
loc 65
rs 5.8461
cc 10
eloc 39
nc 192
nop 4

How to fix   Long Method    Complexity   

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