Completed
Pull Request — master (#1583)
by Jeroen
41:02 queued 38:49
created

Resetter   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 155
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 93.48%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 11
dl 0
loc 155
ccs 43
cts 46
cp 0.9348
rs 10
c 0
b 0
f 0

6 Methods

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