Completed
Push — master ( bb241f...f9eb32 )
by Simonas
04:41 queued 01:25
created

ManagerFactory::createManager()   B

Complexity

Conditions 6
Paths 16

Size

Total Lines 57
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 2
Metric Value
dl 0
loc 57
rs 8.7433
c 3
b 0
f 2
cc 6
eloc 35
nc 16
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\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
        $mappings = $this->metadataCollector->getClientMapping($managerConfig['mappings']);
102
103
        $client = ClientBuilder::create();
104
        $client->setHosts($connection['hosts']);
105
        $client->setTracer($this->tracer);
106
107
        if ($this->logger && $managerConfig['logger']['enabled']) {
108
            $client->setLogger($this->logger);
109
        }
110
111
        $indexSettings = [
112
            'index' => $connection['index_name'],
113
            'body' => array_filter(
114
                [
115
                    'settings' => array_merge(
116
                        $connection['settings'],
117
                        [
118
                            'analysis' =>
119
                                $this->metadataCollector->getClientAnalysis($managerConfig['mappings'], $analysis),
120
                        ]
121
                    ),
122
                    'mappings' => $mappings,
123
                ]
124
            ),
125
        ];
126
127
        $this->eventDispatcher &&
128
            $this->eventDispatcher->dispatch(
129
                Events::PRE_MANAGER_CREATE,
130
                new PreCreateManagerEvent($client, $indexSettings)
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->setCommitMode($managerConfig['commit_mode']);
149
        $manager->setBulkCommitSize($managerConfig['bulk_size']);
150
151
        $this->eventDispatcher &&
152
            $this->eventDispatcher->dispatch(Events::POST_MANAGER_CREATE, new PostCreateManagerEvent($manager));
153
154
        return $manager;
155
    }
156
}
157