Completed
Pull Request — master (#917)
by Dmitry
08:52
created

Resetter   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 190
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 14

Test Coverage

Coverage 94.12%

Importance

Changes 5
Bugs 1 Features 2
Metric Value
wmc 19
c 5
b 1
f 2
lcom 1
cbo 14
dl 0
loc 190
ccs 64
cts 68
cp 0.9412
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 1
A resetAllIndexes() 0 6 2
A resetIndex() 0 21 4
B resetIndexType() 0 35 5
A resetAllTemplates() 0 6 2
A resetTemplate() 0 11 2
A deleteTemplateIndexes() 0 4 1
A postPopulate() 0 9 2
1
<?php
2
3
namespace FOS\ElasticaBundle\Index;
4
5
use Elastica\Index;
6
use Elastica\Exception\ResponseException;
7
use Elastica\Type\Mapping;
8
use FOS\ElasticaBundle\Configuration\ConfigManager;
9
use Elastica\Client;
10
use Elastica\Request;
11
use FOS\ElasticaBundle\Configuration\IndexTemplateConfig;
12
use FOS\ElasticaBundle\Event\IndexResetEvent;
13
use FOS\ElasticaBundle\Event\TypeResetEvent;
14
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
15
16
/**
17
 * Deletes and recreates indexes.
18
 */
19
class Resetter
20
{
21
    /**
22
     * @var AliasProcessor
23
     */
24
    private $aliasProcessor;
25
26
    /***
27
     * @var ConfigManager
28
     */
29
    private $configManager;
30
31
    /**
32
     * @var EventDispatcherInterface
33
     */
34
    private $dispatcher;
35
36
    /**
37
     * @var IndexManager
38
     */
39
    private $indexManager;
40
41
    /**
42
     * @var MappingBuilder
43
     */
44
    private $mappingBuilder;
45
46
    /**
47
     * @var Client
48
     */
49
    private $client;
50
51
    /**
52
     * @param ConfigManager $configManager
53
     * @param IndexManager $indexManager
54
     * @param AliasProcessor $aliasProcessor
55
     * @param MappingBuilder $mappingBuilder
56
     * @param EventDispatcherInterface $eventDispatcher
57
     * @param Client $client
58
     */
59 14
    public function __construct(
60
        ConfigManager $configManager,
61
        IndexManager $indexManager,
62
        AliasProcessor $aliasProcessor,
63
        MappingBuilder $mappingBuilder,
64
        EventDispatcherInterface $eventDispatcher,
65
        Client $client
66
    ) {
67 14
        $this->aliasProcessor = $aliasProcessor;
68 14
        $this->configManager = $configManager;
69 14
        $this->dispatcher = $eventDispatcher;
70 14
        $this->indexManager = $indexManager;
71 14
        $this->mappingBuilder = $mappingBuilder;
72 14
        $this->client = $client;
73 14
    }
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 5
    public function resetIndex($indexName, $populating = false, $force = false)
99
    {
100 5
        $indexConfig = $this->configManager->getIndexConfiguration($indexName);
101 4
        $index = $this->indexManager->getIndex($indexName);
102
103 4
        $event = new IndexResetEvent($indexName, $populating, $force);
104 4
        $this->dispatcher->dispatch(IndexResetEvent::PRE_INDEX_RESET, $event);
105
106 4
        if ($indexConfig->isUseAlias()) {
107 1
            $this->aliasProcessor->setRootName($indexConfig, $index);
108
        }
109
110 4
        $mapping = $this->mappingBuilder->buildIndexMapping($indexConfig);
111 4
        $index->create($mapping, true);
112
113 4
        if (!$populating and $indexConfig->isUseAlias()) {
114 1
            $this->aliasProcessor->switchIndexAlias($indexConfig, $index, $force);
115
        }
116
117 4
        $this->dispatcher->dispatch(IndexResetEvent::POST_INDEX_RESET, $event);
118 4
    }
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 3
    public function resetIndexType($indexName, $typeName)
130
    {
131 3
        $typeConfig = $this->configManager->getTypeConfiguration($indexName, $typeName);
132 2
        $index = $this->indexManager->getIndex($indexName);
133 2
        $type = $index->getType($typeName);
134
135 2
        $indexConfig = $this->configManager->getIndexConfiguration($indexName);
136 2
        $settings = $indexConfig->getSettings();
137
138 2
        $event = new TypeResetEvent($indexName, $typeName);
139 2
        $this->dispatcher->dispatch(TypeResetEvent::PRE_TYPE_RESET, $event);
140
141
        try {
142 2
            $type->delete();
143
        } catch (ResponseException $e) {
144
            if (strpos($e->getMessage(), 'TypeMissingException') === false) {
145
                throw $e;
146
            }
147
        }
148
149 2
        if (!empty($settings)) {
150 1
            $index->close();
151 1
            $index->setSettings($settings);
152 1
            $index->open();
153
        }
154
155 2
        $mapping = new Mapping();
156 2
        foreach ($this->mappingBuilder->buildTypeMapping($typeConfig) as $name => $field) {
0 ignored issues
show
Bug introduced by
The expression $this->mappingBuilder->b...ypeMapping($typeConfig) of type object<stdClass>|array<s...ynamic_templates":"?"}> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
157
            $mapping->setParam($name, $field);
158
        }
159
160 2
        $type->setMapping($mapping);
161
162 2
        $this->dispatcher->dispatch(TypeResetEvent::POST_TYPE_RESET, $event);
163 2
    }
164
165 2
    public function resetAllTemplates($deleteIndexes = false)
166
    {
167 2
        foreach ($this->configManager->getIndexTemplatesNames() as $name) {
168 2
            $this->resetTemplate($name, $deleteIndexes);
169
        }
170 2
    }
171
172 4
    public function resetTemplate($indexTemplateName, $deleteIndexes = false)
173
    {
174 4
        $indexTemplateConfig = $this->configManager->getIndexTemplateConfiguration($indexTemplateName);
175 4
        $indexTemplate = $this->indexManager->getIndexTemplate($indexTemplateName);
176
177 4
        $mapping = $this->mappingBuilder->buildIndexTemplateMapping($indexTemplateConfig);
178 4
        if ($deleteIndexes) {
179 2
            $this->deleteTemplateIndexes($indexTemplateConfig);
180
        }
181 4
        $indexTemplate->create($mapping);
182 4
    }
183
184
    /**
185
     * Delete all template indexes
186
     *
187
     * @param IndexTemplateConfig $template
188
     */
189 2
    public function deleteTemplateIndexes(IndexTemplateConfig $template)
190
    {
191 2
        $this->client->request($template->getTemplate() . '/', Request::DELETE);
192 2
    }
193
194
    /**
195
     * A command run when a population has finished.
196
     *
197
     * @param string $indexName
198
     */
199 2
    public function postPopulate($indexName)
200
    {
201 2
        $indexConfig = $this->configManager->getIndexConfiguration($indexName);
202
203 2
        if ($indexConfig->isUseAlias()) {
204 1
            $index = $this->indexManager->getIndex($indexName);
205 1
            $this->aliasProcessor->switchIndexAlias($indexConfig, $index);
206
        }
207 2
    }
208
}
209