Completed
Pull Request — 5.0 (#720)
by
unknown
02:01
created

ManagerFactory::createManager()   B

Complexity

Conditions 8
Paths 64

Size

Total Lines 61
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 2
Metric Value
c 5
b 0
f 2
dl 0
loc 61
rs 7.0047
cc 8
eloc 37
nc 64
nop 3

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\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  $managerConfig Manager configuration.
95
     *
96
     * @return Manager
97
     */
98
    public function createManager($managerName, $connection, $managerConfig)
99
    {
100
        $connection['settings']['analysis'] = $this->metadataCollector
101
            ->getManagerAnalysis($managerName, $managerConfig['mappings']);
102
103
        if (!isset($connection['settings']['number_of_replicas'])) {
104
            $connection['settings']['number_of_replicas'] = 0;
105
        }
106
107
        if (!isset($connection['settings']['number_of_shards'])) {
108
            $connection['settings']['number_of_shards'] = 1;
109
        }
110
111
        $mappings = $this->metadataCollector->getClientMapping($managerConfig['mappings']);
112
113
        $client = ClientBuilder::create();
114
        $client->setHosts($connection['hosts']);
115
        $client->setTracer($this->tracer);
116
117
        if ($this->logger && $managerConfig['logger']['enabled']) {
118
            $client->setLogger($this->logger);
119
        }
120
121
        $indexSettings = [
122
            'index' => $connection['index_name'],
123
            'body' => array_filter(
124
                [
125
                    'settings' => $connection['settings'],
126
                    'mappings' => $mappings,
127
                ]
128
            ),
129
        ];
130
131
        $this->eventDispatcher &&
132
            $this->eventDispatcher->dispatch(
133
                Events::PRE_MANAGER_CREATE,
134
                new PreCreateManagerEvent($client, $indexSettings)
135
            );
136
137
        $manager = new Manager(
138
            $managerName,
139
            $managerConfig,
140
            $client->build(),
141
            $indexSettings,
142
            $this->metadataCollector,
143
            $this->converter
144
        );
145
146
        if (isset($this->stopwatch)) {
147
            $manager->setStopwatch($this->stopwatch);
148
        }
149
150
        $manager->setCommitMode($managerConfig['commit_mode']);
151
        $manager->setEventDispatcher($this->eventDispatcher);
152
        $manager->setBulkCommitSize($managerConfig['bulk_size']);
153
154
        $this->eventDispatcher &&
155
            $this->eventDispatcher->dispatch(Events::POST_MANAGER_CREATE, new PostCreateManagerEvent($manager));
156
157
        return $manager;
158
    }
159
}
160