Completed
Pull Request — master (#1603)
by
unknown
03:49
created

Resetter   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 160
Duplicated Lines 11.25 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 91.67%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 11
dl 18
loc 160
ccs 44
cts 48
cp 0.9167
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 18 18 2
A resetAllIndexes() 0 6 2
A resetIndex() 0 21 4
A postPopulate() 0 4 1
A switchIndexAlias() 0 9 2
A resetIndexType() 0 21 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/*
4
 * This file is part of the FOSElasticaBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
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 FOS\ElasticaBundle\Index;
13
14
use Elastica\Exception\ResponseException;
15
use Elastica\Type\Mapping;
16
use FOS\ElasticaBundle\Configuration\ManagerInterface;
17
use Elastica\Client;
18
use FOS\ElasticaBundle\Event\IndexResetEvent;
19
use FOS\ElasticaBundle\Event\TypeResetEvent;
20
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
21
use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;
22
23
/**
24
 * Deletes and recreates indexes.
25
 */
26
class Resetter implements ResetterInterface
27
{
28
    /**
29
     * @var AliasProcessor
30
     */
31
    private $aliasProcessor;
32
33
    /***
34
     * @var ManagerInterface
35
     */
36
    private $configManager;
37
38
    /**
39
     * @var EventDispatcherInterface
40
     */
41
    private $dispatcher;
42
43
    /**
44
     * @var IndexManager
45
     */
46
    private $indexManager;
47
48
    /**
49
     * @var MappingBuilder
50
     */
51
    private $mappingBuilder;
52
53
    /**
54
     * @param ManagerInterface         $configManager
55
     * @param IndexManager             $indexManager
56
     * @param AliasProcessor           $aliasProcessor
57
     * @param MappingBuilder           $mappingBuilder
58
     * @param EventDispatcherInterface $eventDispatcher
59
     * @param Client                   $client
0 ignored issues
show
Bug introduced by
There is no parameter named $client. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
60
     */
61 20 View Code Duplication
    public function __construct(
62
        ManagerInterface $configManager,
63
        IndexManager $indexManager,
64
        AliasProcessor $aliasProcessor,
65
        MappingBuilder $mappingBuilder,
66
        EventDispatcherInterface $eventDispatcher
67
    ) {
68 20
        $this->aliasProcessor = $aliasProcessor;
69 20
        $this->configManager = $configManager;
70 20
        $this->dispatcher = $eventDispatcher;
71
72 20
        if (class_exists(LegacyEventDispatcherProxy::class)) {
73 20
            $this->dispatcher = LegacyEventDispatcherProxy::decorate($eventDispatcher);
74
        }
75
76 20
        $this->indexManager = $indexManager;
77 20
        $this->mappingBuilder = $mappingBuilder;
78 20
    }
79
80
    /**
81
     * Deletes and recreates all indexes.
82
     *
83
     * @param bool $populating
84
     * @param bool $force
85
     */
86 1
    public function resetAllIndexes($populating = false, $force = false)
87
    {
88 1
        foreach ($this->configManager->getIndexNames() as $name) {
89 1
            $this->resetIndex($name, $populating, $force);
90
        }
91 1
    }
92
93
    /**
94
     * Deletes and recreates the named index. If populating, creates a new index
95
     * with a randomised name for an alias to be set after population.
96
     *
97
     * @param string $indexName
98
     * @param bool   $populating
99
     * @param bool   $force      If index exists with same name as alias, remove it
100
     *
101
     * @throws \InvalidArgumentException if no index exists for the given name
102
     */
103 12
    public function resetIndex($indexName, $populating = false, $force = false)
104
    {
105 12
        $indexConfig = $this->configManager->getIndexConfiguration($indexName);
106 11
        $index = $this->indexManager->getIndex($indexName);
107
108 11
        if ($indexConfig->isUseAlias()) {
109 1
            $this->aliasProcessor->setRootName($indexConfig, $index);
110
        }
111
112 11
        $event = new IndexResetEvent($indexName, $populating, $force);
113 11
        $this->dispatcher->dispatch(IndexResetEvent::PRE_INDEX_RESET, $event);
114
115 6
        $mapping = $this->mappingBuilder->buildIndexMapping($indexConfig);
116 6
        $index->create($mapping, true);
117
118 6
        if (!$populating and $indexConfig->isUseAlias()) {
119 1
            $this->aliasProcessor->switchIndexAlias($indexConfig, $index, $force);
120
        }
121
122 6
        $this->dispatcher->dispatch(IndexResetEvent::POST_INDEX_RESET, $event);
123 6
    }
124
125
    /**
126
     * Deletes and recreates a mapping type for the named index.
127
     *
128
     * @param string $indexName
129
     * @param string $typeName
130
     *
131
     * @throws \InvalidArgumentException if no index or type mapping exists for the given names
132
     * @throws ResponseException
133
     */
134 5
    public function resetIndexType($indexName, $typeName)
135
    {
136 5
        $typeConfig = $this->configManager->getTypeConfiguration($indexName, $typeName);
137
138 4
        $this->resetIndex($indexName, true);
139
140 2
        $index = $this->indexManager->getIndex($indexName);
141 2
        $type = $index->getType($typeName);
142
143 2
        $event = new TypeResetEvent($indexName, $typeName);
144 2
        $this->dispatcher->dispatch(TypeResetEvent::PRE_TYPE_RESET, $event);
145
146 2
        $mapping = new Mapping();
147 2
        foreach ($this->mappingBuilder->buildTypeMapping($typeConfig) as $name => $field) {
148
            $mapping->setParam($name, $field);
149
        }
150
151 2
        $type->setMapping($mapping);
152
153 2
        $this->dispatcher->dispatch(TypeResetEvent::POST_TYPE_RESET, $event);
154 2
    }
155
156
    /**
157
     * A command run when a population has finished.
158
     *
159
     * @param string $indexName
160
     *
161
     * @deprecated
162
     */
163
    public function postPopulate($indexName)
164
    {
165
        $this->switchIndexAlias($indexName);
166
    }
167
168
    /**
169
     * Switching aliases.
170
     *
171
     * @param string $indexName
172
     * @param bool   $delete    Delete or close index
173
     *
174
     * @throws \FOS\ElasticaBundle\Exception\AliasIsIndexException
175
     */
176 2
    public function switchIndexAlias($indexName, $delete = true)
177
    {
178 2
        $indexConfig = $this->configManager->getIndexConfiguration($indexName);
179
180 2
        if ($indexConfig->isUseAlias()) {
181 1
            $index = $this->indexManager->getIndex($indexName);
182 1
            $this->aliasProcessor->switchIndexAlias($indexConfig, $index, false, $delete);
183
        }
184 2
    }
185
}
186