Completed
Push — master ( f27ecd...d25871 )
by Karel
32:32 queued 28:32
created

Resetter   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 154
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 91.3%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 12
c 4
b 0
f 0
lcom 1
cbo 10
dl 0
loc 154
ccs 42
cts 46
cp 0.913
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A resetIndex() 0 21 4
A __construct() 0 13 1
A resetAllIndexes() 0 6 2
A resetIndexType() 0 21 2
A postPopulate() 0 4 1
A switchIndexAlias() 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 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 11
    public function __construct(
51
        ConfigManager $configManager,
52
        IndexManager $indexManager,
53
        AliasProcessor $aliasProcessor,
54
        MappingBuilder $mappingBuilder,
55
        EventDispatcherInterface $eventDispatcher
56
    ) {
57 11
        $this->aliasProcessor = $aliasProcessor;
58 11
        $this->configManager = $configManager;
59 11
        $this->dispatcher = $eventDispatcher;
60 11
        $this->indexManager = $indexManager;
61 11
        $this->mappingBuilder = $mappingBuilder;
62 11
    }
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 8
    public function resetIndex($indexName, $populating = false, $force = false)
88
    {
89 8
        $indexConfig = $this->configManager->getIndexConfiguration($indexName);
90 7
        $index = $this->indexManager->getIndex($indexName);
91
92 7
        if ($indexConfig->isUseAlias()) {
93 1
            $this->aliasProcessor->setRootName($indexConfig, $index);
94
        }
95
96 7
        $event = new IndexResetEvent($indexName, $populating, $force);
97 7
        $this->dispatcher->dispatch(IndexResetEvent::PRE_INDEX_RESET, $event);
98
99 7
        $mapping = $this->mappingBuilder->buildIndexMapping($indexConfig);
100 7
        $index->create($mapping, true);
101
102 7
        if (!$populating and $indexConfig->isUseAlias()) {
103 1
            $this->aliasProcessor->switchIndexAlias($indexConfig, $index, $force);
104
        }
105
106 7
        $this->dispatcher->dispatch(IndexResetEvent::POST_INDEX_RESET, $event);
107 7
    }
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 3
    public function resetIndexType($indexName, $typeName)
119
    {
120 3
        $typeConfig = $this->configManager->getTypeConfiguration($indexName, $typeName);
121
122 2
        $this->resetIndex($indexName, true);
123
124 2
        $index = $this->indexManager->getIndex($indexName);
125 2
        $type = $index->getType($typeName);
126
127 2
        $event = new TypeResetEvent($indexName, $typeName);
128 2
        $this->dispatcher->dispatch(TypeResetEvent::PRE_TYPE_RESET, $event);
129
130 2
        $mapping = new Mapping();
131 2
        foreach ($this->mappingBuilder->buildTypeMapping($typeConfig) as $name => $field) {
132
            $mapping->setParam($name, $field);
133
        }
134
135 2
        $type->setMapping($mapping);
136
137 2
        $this->dispatcher->dispatch(TypeResetEvent::POST_TYPE_RESET, $event);
138 2
    }
139
140
    /**
141
     * A command run when a population has finished.
142
     *
143
     * @param string $indexName
144
     *
145
     * @deprecated
146
     */
147
    public function postPopulate($indexName)
148
    {
149
        $this->switchIndexAlias($indexName);
150
    }
151
152
    /**
153
     * Switching aliases
154
     *
155
     * @param string $indexName
156
     * @param bool   $delete Delete or close index
157
     *
158
     * @throws \FOS\ElasticaBundle\Exception\AliasIsIndexException
159
     */
160 2
    public function switchIndexAlias($indexName, $delete = true)
161
    {
162 2
        $indexConfig = $this->configManager->getIndexConfiguration($indexName);
163
164 2
        if ($indexConfig->isUseAlias()) {
165 1
            $index = $this->indexManager->getIndex($indexName);
166 1
            $this->aliasProcessor->switchIndexAlias($indexConfig, $index, false, $delete);
167
        }
168 2
    }
169
}
170