Completed
Pull Request — master (#1343)
by Dmitry
04:26
created

Resetter::resetAllIndexes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
ccs 1
cts 1
cp 1
cc 2
eloc 3
nc 2
nop 2
crap 2
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\Index;
16
use Elastica\Type\Mapping;
17
use FOS\ElasticaBundle\Configuration\ManagerInterface;
18
use FOS\ElasticaBundle\Configuration\ConfigManager;
19
use Elastica\Client;
20
use Elastica\Request;
21
use FOS\ElasticaBundle\Configuration\IndexTemplateConfig;
22
use FOS\ElasticaBundle\Event\IndexResetEvent;
23
use FOS\ElasticaBundle\Event\TypeResetEvent;
24
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
25
26
/**
27
 * Deletes and recreates indexes.
28
 */
29
class Resetter
30
{
31
    /**
32
     * @var AliasProcessor
33
     */
34
    private $aliasProcessor;
35
36
    /***
37
     * @var ManagerInterface
38
     */
39
    private $configManager;
40
41
    /**
42
     * @var EventDispatcherInterface
43
     */
44
    private $dispatcher;
45
46
    /**
47
     * @var IndexManager
48
     */
49
    private $indexManager;
50
51
    /**
52
     * @var MappingBuilder
53
     */
54
    private $mappingBuilder;
55
56
    /**
57
     * @var Client
58
     */
59 15
    private $client;
60
61
    /**
62
     * @param ManagerInterface         $configManager
63
     * @param IndexManager             $indexManager
64
     * @param AliasProcessor           $aliasProcessor
65
     * @param MappingBuilder           $mappingBuilder
66 15
     * @param EventDispatcherInterface $eventDispatcher
67 15
     * @param Client                   $client
68 15
     */
69 15
    public function __construct(
70 15
        ManagerInterface $configManager,
71 15
        IndexManager $indexManager,
72
        AliasProcessor $aliasProcessor,
73
        MappingBuilder $mappingBuilder,
74
        EventDispatcherInterface $eventDispatcher,
75
        Client $client
76
    ) {
77
        $this->aliasProcessor = $aliasProcessor;
78
        $this->configManager = $configManager;
79 1
        $this->dispatcher = $eventDispatcher;
80
        $this->indexManager = $indexManager;
81 1
        $this->mappingBuilder = $mappingBuilder;
82 1
        $this->client = $client;
83
    }
84 1
85
    /**
86
     * Deletes and recreates all indexes.
87
     *
88
     * @param bool $populating
89
     * @param bool $force
90
     */
91
    public function resetAllIndexes($populating = false, $force = false)
92
    {
93
        foreach ($this->configManager->getIndexNames() as $name) {
94
            $this->resetIndex($name, $populating, $force);
95
        }
96 12
    }
97
98 12
    /**
99 11
     * Deletes and recreates the named index. If populating, creates a new index
100
     * with a randomised name for an alias to be set after population.
101 11
     *
102 1
     * @param string $indexName
103
     * @param bool   $populating
104
     * @param bool   $force      If index exists with same name as alias, remove it
105 11
     *
106 11
     * @throws \InvalidArgumentException if no index exists for the given name
107
     */
108 11
    public function resetIndex($indexName, $populating = false, $force = false)
109 11
    {
110
        $indexConfig = $this->configManager->getIndexConfiguration($indexName);
111 11
        $index = $this->indexManager->getIndex($indexName);
112 1
113
        if ($indexConfig->isUseAlias()) {
114
            $this->aliasProcessor->setRootName($indexConfig, $index);
115 11
        }
116 11
117
        $event = new IndexResetEvent($indexName, $populating, $force);
118
        $this->dispatcher->dispatch(IndexResetEvent::PRE_INDEX_RESET, $event);
119
120
        $mapping = $this->mappingBuilder->buildIndexMapping($indexConfig);
121
        $index->create($mapping, true);
122
123
        if (!$populating and $indexConfig->isUseAlias()) {
124
            $this->aliasProcessor->switchIndexAlias($indexConfig, $index, $force);
125
        }
126
127 5
        $this->dispatcher->dispatch(IndexResetEvent::POST_INDEX_RESET, $event);
128
    }
129 5
130
    /**
131 4
     * Deletes and recreates a mapping type for the named index.
132
     *
133 4
     * @param string $indexName
134 4
     * @param string $typeName
135
     *
136 4
     * @throws \InvalidArgumentException if no index or type mapping exists for the given names
137 4
     * @throws ResponseException
138
     */
139 4
    public function resetIndexType($indexName, $typeName)
140 4
    {
141 2
        $typeConfig = $this->configManager->getTypeConfiguration($indexName, $typeName);
142
143
        $this->resetIndex($indexName, true);
144 4
145
        $index = $this->indexManager->getIndex($indexName);
146 4
        $type = $index->getType($typeName);
147 4
148
        $event = new TypeResetEvent($indexName, $typeName);
149
        $this->dispatcher->dispatch(TypeResetEvent::PRE_TYPE_RESET, $event);
150
151
        $mapping = new Mapping();
152
        foreach ($this->mappingBuilder->buildTypeMapping($typeConfig) as $name => $field) {
153
            $mapping->setParam($name, $field);
154
        }
155
156
        $type->setMapping($mapping);
157
158
        $this->dispatcher->dispatch(TypeResetEvent::POST_TYPE_RESET, $event);
159
    }
160
161
    public function resetAllTemplates($deleteIndexes = false)
162
    {
163
        foreach ($this->configManager->getIndexTemplatesNames() as $name) {
164
            $this->resetTemplate($name, $deleteIndexes);
165
        }
166
    }
167
168
    public function resetTemplate($indexTemplateName, $deleteIndexes = false)
169 2
    {
170
        $indexTemplateConfig = $this->configManager->getIndexTemplateConfiguration($indexTemplateName);
171 2
        $indexTemplate = $this->indexManager->getIndexTemplate($indexTemplateName);
172
173 2
        $mapping = $this->mappingBuilder->buildIndexTemplateMapping($indexTemplateConfig);
174 1
        if ($deleteIndexes) {
175 1
            $this->deleteTemplateIndexes($indexTemplateConfig);
176
        }
177 2
        $indexTemplate->create($mapping);
178
    }
179
180
    /**
181
     * Delete all template indexes
182
     *
183
     * @param IndexTemplateConfig $template
184
     */
185
    public function deleteTemplateIndexes(IndexTemplateConfig $template)
186
    {
187
        $this->client->request($template->getTemplate() . '/', Request::DELETE);
188
    }
189
190
    /**
191
     * A command run when a population has finished.
192
     *
193
     * @param string $indexName
194
     *
195
     * @deprecated
196
     */
197
    public function postPopulate($indexName)
198
    {
199
        $this->switchIndexAlias($indexName);
200
    }
201
202
    /**
203
     * Switching aliases.
204
     *
205
     * @param string $indexName
206
     * @param bool   $delete    Delete or close index
207
     *
208
     * @throws \FOS\ElasticaBundle\Exception\AliasIsIndexException
209
     */
210
    public function switchIndexAlias($indexName, $delete = true)
211
    {
212
        $indexConfig = $this->configManager->getIndexConfiguration($indexName);
213
214
        if ($indexConfig->isUseAlias()) {
215
            $index = $this->indexManager->getIndex($indexName);
216
            $this->aliasProcessor->switchIndexAlias($indexConfig, $index, false, $delete);
217
        }
218
    }
219
}
220