Completed
Pull Request — master (#1093)
by Oleg
06:31 queued 02:17
created

Resetter::switchIndexAlias()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
cc 2
eloc 5
nc 2
nop 2
crap 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 FOS\ElasticaBundle\Event\IndexResetEvent;
10
use FOS\ElasticaBundle\Event\TypeResetEvent;
11
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
12
13
/**
14
 * Deletes and recreates indexes.
15
 */
16
class Resetter
17
{
18
    /**
19
     * @var AliasProcessor
20
     */
21
    private $aliasProcessor;
22
23
    /***
24
     * @var ConfigManager
25
     */
26
    private $configManager;
27
28
    /**
29
     * @var EventDispatcherInterface
30
     */
31
    private $dispatcher;
32
33
    /**
34
     * @var IndexManager
35
     */
36
    private $indexManager;
37
38
    /**
39
     * @var MappingBuilder
40
     */
41
    private $mappingBuilder;
42
43
    /**
44
     * @param ConfigManager            $configManager
45
     * @param IndexManager             $indexManager
46
     * @param AliasProcessor           $aliasProcessor
47
     * @param MappingBuilder           $mappingBuilder
48
     * @param EventDispatcherInterface $eventDispatcher
49
     */
50 15
    public function __construct(
51
        ConfigManager $configManager,
52
        IndexManager $indexManager,
53
        AliasProcessor $aliasProcessor,
54
        MappingBuilder $mappingBuilder,
55
        EventDispatcherInterface $eventDispatcher
56
    ) {
57 15
        $this->aliasProcessor = $aliasProcessor;
58 15
        $this->configManager = $configManager;
59 15
        $this->dispatcher = $eventDispatcher;
60 15
        $this->indexManager = $indexManager;
61 15
        $this->mappingBuilder = $mappingBuilder;
62 15
    }
63
64
    /**
65
     * Deletes and recreates all indexes.
66
     *
67
     * @param bool $populating
68
     * @param bool $force
69
     */
70 1
    public function resetAllIndexes($populating = false, $force = false)
71
    {
72 1
        foreach ($this->configManager->getIndexNames() as $name) {
73 1
            $this->resetIndex($name, $populating, $force);
74
        }
75 1
    }
76
77
    /**
78
     * Deletes and recreates the named index. If populating, creates a new index
79
     * with a randomised name for an alias to be set after population.
80
     *
81
     * @param string $indexName
82
     * @param bool   $populating
83
     * @param bool   $force      If index exists with same name as alias, remove it
84
     *
85
     * @throws \InvalidArgumentException if no index exists for the given name
86
     */
87 12
    public function resetIndex($indexName, $populating = false, $force = false)
88
    {
89 12
        $indexConfig = $this->configManager->getIndexConfiguration($indexName);
90 11
        $index = $this->indexManager->getIndex($indexName);
91
92 11
        $event = new IndexResetEvent($indexName, $populating, $force);
93 11
        $this->dispatcher->dispatch(IndexResetEvent::PRE_INDEX_RESET, $event);
94
95 11
        if ($indexConfig->isUseAlias()) {
96 1
            $this->aliasProcessor->setRootName($indexConfig, $index);
97
        }
98
99 11
        $mapping = $this->mappingBuilder->buildIndexMapping($indexConfig);
100 11
        $index->create($mapping, true);
101
102 11
        if (!$populating and $indexConfig->isUseAlias()) {
103 1
            $this->aliasProcessor->switchIndexAlias($indexConfig, $index, $force);
104
        }
105
106 11
        $this->dispatcher->dispatch(IndexResetEvent::POST_INDEX_RESET, $event);
107 11
    }
108
109
    /**
110
     * Deletes and recreates a mapping type for the named index.
111
     *
112
     * @param string $indexName
113
     * @param string $typeName
114
     *
115
     * @throws \InvalidArgumentException if no index or type mapping exists for the given names
116
     * @throws ResponseException
117
     */
118 5
    public function resetIndexType($indexName, $typeName)
119
    {
120 5
        $typeConfig = $this->configManager->getTypeConfiguration($indexName, $typeName);
121
122 4
        $this->resetIndex($indexName, true);
123
124 4
        $index = $this->indexManager->getIndex($indexName);
125 4
        $type = $index->getType($typeName);
126
127 4
        $event = new TypeResetEvent($indexName, $typeName);
128 4
        $this->dispatcher->dispatch(TypeResetEvent::PRE_TYPE_RESET, $event);
129
130 4
        if (!empty($settings)) {
0 ignored issues
show
Bug introduced by
The variable $settings seems to never exist, and therefore empty should always return true. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
131
            unset($settings['number_of_shards']);
132
            $index->close();
133
            $index->setSettings($settings);
134
            $index->open();
135
        }
136
137 4
        $mapping = new Mapping();
138 4
        foreach ($this->mappingBuilder->buildTypeMapping($typeConfig) as $name => $field) {
139 2
            $mapping->setParam($name, $field);
140
        }
141
142 4
        $type->setMapping($mapping);
143
144 4
        $this->dispatcher->dispatch(TypeResetEvent::POST_TYPE_RESET, $event);
145 4
    }
146
147
    /**
148
     * A command run when a population has finished.
149
     *
150
     * @param string $indexName
151
     *
152
     * @deprecated
153
     */
154
    public function postPopulate($indexName)
155
    {
156
        $this->switchIndexAlias($indexName);
157
    }
158
159
    /**
160
     * Switching aliases
161
     *
162
     * @param string $indexName
163
     * @param bool   $delete Delete or close index
164
     *
165
     * @throws \FOS\ElasticaBundle\Exception\AliasIsIndexException
166
     */
167 2
    public function switchIndexAlias($indexName, $delete = true)
168
    {
169 2
        $indexConfig = $this->configManager->getIndexConfiguration($indexName);
170
171 2
        if ($indexConfig->isUseAlias()) {
172 1
            $index = $this->indexManager->getIndex($indexName);
173 1
            $this->aliasProcessor->switchIndexAlias($indexConfig, $index, false, $delete);
174
        }
175 2
    }
176
}
177