Completed
Push — 5.2 ( b6cef4...e52332 )
by Simonas
04:33 queued 03:11
created

ManagerFactory::createManager()   C

Complexity

Conditions 10
Paths 160

Size

Total Lines 70

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 70
rs 6.3878
c 0
b 0
f 0
cc 10
nc 160
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 PackageVersions\Versions;
21
use Psr\Log\LoggerInterface;
22
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
23
use Symfony\Component\Stopwatch\Stopwatch;
24
25
/**
26
 * Elasticsearch Manager factory class.
27
 */
28
class ManagerFactory
29
{
30
    /**
31
     * @var MetadataCollector
32
     */
33
    private $metadataCollector;
34
35
    /**
36
     * @var Converter
37
     */
38
    private $converter;
39
40
    /**
41
     * @var LoggerInterface
42
     */
43
    private $logger;
44
45
    /**
46
     * @var LoggerInterface
47
     */
48
    private $tracer;
49
50
    /**
51
     * @var EventDispatcherInterface
52
     */
53
    private $eventDispatcher;
54
55
    /**
56
     * @var Stopwatch
57
     */
58
    private $stopwatch;
59
60
    /**
61
     * @param MetadataCollector $metadataCollector Metadata collector service.
62
     * @param Converter         $converter         Converter service to transform arrays to objects and visa versa.
63
     * @param LoggerInterface   $tracer
64
     * @param LoggerInterface   $logger
65
     */
66
    public function __construct($metadataCollector, $converter, $tracer = null, $logger = null)
67
    {
68
        $this->metadataCollector = $metadataCollector;
69
        $this->converter = $converter;
70
        $this->tracer = $tracer;
71
        $this->logger = $logger;
72
    }
73
74
    /**
75
     * @param EventDispatcherInterface   $eventDispatcher
76
     */
77
    public function setEventDispatcher(EventDispatcherInterface $eventDispatcher)
78
    {
79
        $this->eventDispatcher = $eventDispatcher;
80
    }
81
82
    /**
83
     * @param Stopwatch $stopwatch
84
     */
85
    public function setStopwatch(Stopwatch $stopwatch)
86
    {
87
        $this->stopwatch = $stopwatch;
88
    }
89
90
    /**
91
     * Factory function to create a manager instance.
92
     *
93
     * @param string $managerName   Manager name.
94
     * @param array  $connection    Connection configuration.
95
     * @param array  $analysis      Analyzers, filters and tokenizers config.
96
     * @param array  $managerConfig Manager configuration.
97
     *
98
     * @return Manager
99
     */
100
    public function createManager($managerName, $connection, $analysis, $managerConfig)
101
    {
102
        $mappings = $this->metadataCollector->getClientMapping($managerConfig['mappings']);
103
104
        $client = ClientBuilder::create();
105
        $client->setHosts($connection['hosts']);
106
107
        if ($this->tracer) {
108
            $client->setTracer($this->tracer);
109
        }
110
111
        if ($this->logger && $managerConfig['logger']['enabled']) {
112
            $client->setLogger($this->logger);
113
        }
114
115
        $indexSettings = [
116
            'index' => $connection['index_name'],
117
            'body' => array_filter(
118
                [
119
                    'settings' => array_merge(
120
                        $connection['settings'],
121
                        [
122
                            'analysis' =>
123
                                $this->metadataCollector->getClientAnalysis($managerConfig['mappings'], $analysis),
124
                        ]
125
                    ),
126
                    'mappings' => $mappings,
127
                ]
128
            ),
129
        ];
130
131
        if (class_exists(Versions::class)) {
132
            $elasticSearchVersion = explode('@', Versions::getVersion('ongr/elasticsearch-dsl'))[0];
133
            if (0 === strpos($elasticSearchVersion, 'v')) {
134
                $elasticSearchVersion = substr($elasticSearchVersion, 1);
135
            }
136
            if (version_compare($elasticSearchVersion, '7.0.0', '>=')) {
137
                $indexSettings['include_type_name'] = true;
138
            }
139
        }
140
141
        $this->eventDispatcher &&
142
            $this->eventDispatcher->dispatch(
143
                Events::PRE_MANAGER_CREATE,
144
                $preCreateEvent = new PreCreateManagerEvent($client, $indexSettings)
145
            );
146
147
        $manager = new Manager(
148
            $managerName,
149
            $managerConfig,
150
            $client->build(),
151
            $preCreateEvent->getIndexSettings(),
0 ignored issues
show
Bug introduced by
The variable $preCreateEvent does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
152
            $this->metadataCollector,
153
            $this->converter
154
        );
155
156
        if (isset($this->stopwatch)) {
157
            $manager->setStopwatch($this->stopwatch);
158
        }
159
160
        $manager->setCommitMode($managerConfig['commit_mode']);
161
        $manager->setEventDispatcher($this->eventDispatcher);
162
        $manager->setCommitMode($managerConfig['commit_mode']);
163
        $manager->setBulkCommitSize($managerConfig['bulk_size']);
164
165
        $this->eventDispatcher &&
166
            $this->eventDispatcher->dispatch(Events::POST_MANAGER_CREATE, new PostCreateManagerEvent($manager));
167
168
        return $manager;
169
    }
170
}
171