Completed
Push — 3.1.x ( b8b6d2...fd2df9 )
by Karel
14:12 queued 11:28
created

Resetter   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 96%

Importance

Changes 5
Bugs 1 Features 1
Metric Value
wmc 13
c 5
b 1
f 1
lcom 1
cbo 11
dl 0
loc 143
ccs 48
cts 50
cp 0.96
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A resetAllIndexes() 0 6 2
A resetIndex() 0 21 4
B resetIndexType() 0 25 4
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 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 12
    public function __construct(
51
        ConfigManager $configManager,
52
        IndexManager $indexManager,
53
        AliasProcessor $aliasProcessor,
54
        MappingBuilder $mappingBuilder,
55
        EventDispatcherInterface $eventDispatcher
56
    ) {
57 12
        $this->aliasProcessor = $aliasProcessor;
58 12
        $this->configManager = $configManager;
59 12
        $this->dispatcher = $eventDispatcher;
60 12
        $this->indexManager = $indexManager;
61 12
        $this->mappingBuilder = $mappingBuilder;
62 12
    }
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 1
        }
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 7
    public function resetIndex($indexName, $populating = false, $force = false)
88
    {
89 7
        $indexConfig = $this->configManager->getIndexConfiguration($indexName);
90 6
        $index = $this->indexManager->getIndex($indexName);
91
92 6
        $event = new IndexResetEvent($indexName, $populating, $force);
93 6
        $this->dispatcher->dispatch(IndexResetEvent::PRE_INDEX_RESET, $event);
94
95 6
        if ($indexConfig->isUseAlias()) {
96 1
            $this->aliasProcessor->setRootName($indexConfig, $index);
97 1
        }
98
99 6
        $mapping = $this->mappingBuilder->buildIndexMapping($indexConfig);
100 6
        $index->create($mapping, true);
101
102 6
        if (!$populating and $indexConfig->isUseAlias()) {
103 1
            $this->aliasProcessor->switchIndexAlias($indexConfig, $index, $force);
104 1
        }
105
106 6
        $this->dispatcher->dispatch(IndexResetEvent::POST_INDEX_RESET, $event);
107 6
    }
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 2
        $type = $this->indexManager->getIndex($indexName)->getType($typeName);
122
123 2
        $event = new TypeResetEvent($indexName, $typeName);
124 2
        $this->dispatcher->dispatch(TypeResetEvent::PRE_TYPE_RESET, $event);
125
126
        try {
127 2
            $type->delete();
128 2
        } catch (ResponseException $e) {
129
            if (strpos($e->getMessage(), 'TypeMissingException') === false) {
130
                throw $e;
131
            }
132
        }
133
134 2
        $mapping = new Mapping();
135 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...
136 1
            $mapping->setParam($name, $field);
137 2
        }
138
139 2
        $type->setMapping($mapping);
140
141 2
        $this->dispatcher->dispatch(TypeResetEvent::POST_TYPE_RESET, $event);
142 2
    }
143
144
    /**
145
     * A command run when a population has finished.
146
     *
147
     * @param string $indexName
148
     */
149 2
    public function postPopulate($indexName)
150
    {
151 2
        $indexConfig = $this->configManager->getIndexConfiguration($indexName);
152
153 2
        if ($indexConfig->isUseAlias()) {
154 1
            $index = $this->indexManager->getIndex($indexName);
155 1
            $this->aliasProcessor->switchIndexAlias($indexConfig, $index);
156 1
        }
157 2
    }
158
}
159