Completed
Pull Request — master (#1531)
by
unknown
07:59
created

Resetter::resetAllIndexes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 4
cp 0.75
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2.0625
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\Type\Mapping;
16
use FOS\ElasticaBundle\Configuration\ManagerInterface;
17
use Elastica\Client;
18
use FOS\ElasticaBundle\Event\IndexResetEvent;
19
use FOS\ElasticaBundle\Event\TypeResetEvent;
20
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
21
22
/**
23
 * Deletes and recreates indexes.
24
 */
25
class Resetter implements ResetterInterface
26
{
27
    /**
28
     * @var AliasProcessor
29
     */
30
    private $aliasProcessor;
31
32
    /***
33
     * @var ManagerInterface
34
     */
35
    private $configManager;
36
37
    /**
38
     * @var EventDispatcherInterface
39
     */
40
    private $dispatcher;
41
42
    /**
43
     * @var IndexManager
44
     */
45
    private $indexManager;
46
47
    /**
48
     * @var MappingBuilder
49
     */
50
    private $mappingBuilder;
51
52
    /**
53
     * @param ManagerInterface         $configManager
54
     * @param IndexManager             $indexManager
55
     * @param AliasProcessor           $aliasProcessor
56
     * @param MappingBuilder           $mappingBuilder
57
     * @param EventDispatcherInterface $eventDispatcher
58
     * @param Client                   $client
0 ignored issues
show
Bug introduced by
There is no parameter named $client. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
59
     */
60 20 View Code Duplication
    public function __construct(
61
        ManagerInterface $configManager,
62
        IndexManager $indexManager,
63
        AliasProcessor $aliasProcessor,
64
        MappingBuilder $mappingBuilder,
65
        EventDispatcherInterface $eventDispatcher
66
    ) {
67 20
        $this->aliasProcessor = $aliasProcessor;
68 20
        $this->configManager = $configManager;
69 20
        $this->dispatcher = $eventDispatcher;
70 20
        $this->indexManager = $indexManager;
71 20
        $this->mappingBuilder = $mappingBuilder;
72 20
    }
73
74
    /**
75
     * Deletes and recreates all indexes.
76
     *
77
     * @param bool $populating
78
     * @param bool $force
79
     */
80 1
    public function resetAllIndexes($populating = false, $force = false)
81
    {
82 1
        foreach ($this->configManager->getIndexNames() as $name) {
83 1
            $this->resetIndex($name, $populating, $force);
84
        }
85
    }
86
87
    /**
88
     * Deletes and recreates the named index. If populating, creates a new index
89
     * with a randomised name for an alias to be set after population.
90
     *
91
     * @param string $indexName
92
     * @param bool   $populating
93
     * @param bool   $force      If index exists with same name as alias, remove it
94
     *
95
     * @throws \InvalidArgumentException if no index exists for the given name
96
     */
97 12
    public function resetIndex($indexName, $populating = false, $force = false)
98
    {
99 12
        $indexConfig = $this->configManager->getIndexConfiguration($indexName);
100 11
        $index = $this->indexManager->getIndex($indexName);
101
102 11
        if ($indexConfig->isUseAlias()) {
103 1
            $this->aliasProcessor->setRootName($indexConfig, $index);
104
        }
105
106 11
        $event = new IndexResetEvent($indexName, $populating, $force);
107 11
        $this->dispatcher->dispatch($event, IndexResetEvent::PRE_INDEX_RESET);
0 ignored issues
show
Documentation introduced by
$event is of type object<FOS\ElasticaBundle\Event\IndexResetEvent>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
108
109 6
        $mapping = $this->mappingBuilder->buildIndexMapping($indexConfig);
110 6
        $index->create($mapping, true);
111
112 6
        if (!$populating and $indexConfig->isUseAlias()) {
113
            $this->aliasProcessor->switchIndexAlias($indexConfig, $index, $force);
114
        }
115
116 6
        $this->dispatcher->dispatch($event, IndexResetEvent::POST_INDEX_RESET);
0 ignored issues
show
Documentation introduced by
$event is of type object<FOS\ElasticaBundle\Event\IndexResetEvent>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
117 6
    }
118
119
    /**
120
     * Deletes and recreates a mapping type for the named index.
121
     *
122
     * @param string $indexName
123
     * @param string $typeName
124
     *
125
     * @throws \InvalidArgumentException if no index or type mapping exists for the given names
126
     * @throws ResponseException
127
     */
128 5
    public function resetIndexType($indexName, $typeName)
129
    {
130 5
        $typeConfig = $this->configManager->getTypeConfiguration($indexName, $typeName);
131
132 4
        $this->resetIndex($indexName, true);
133
134 3
        $index = $this->indexManager->getIndex($indexName);
135 3
        $type = $index->getType($typeName);
136
137 3
        $event = new TypeResetEvent($indexName, $typeName);
138 3
        $this->dispatcher->dispatch($event, TypeResetEvent::PRE_TYPE_RESET);
0 ignored issues
show
Documentation introduced by
$event is of type object<FOS\ElasticaBundle\Event\TypeResetEvent>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
139
140 3
        $mapping = new Mapping();
141 3
        foreach ($this->mappingBuilder->buildTypeMapping($typeConfig) as $name => $field) {
142 2
            $mapping->setParam($name, $field);
143
        }
144
145 3
        $type->setMapping($mapping);
146
147 3
        $this->dispatcher->dispatch($event, TypeResetEvent::POST_TYPE_RESET);
0 ignored issues
show
Documentation introduced by
$event is of type object<FOS\ElasticaBundle\Event\TypeResetEvent>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
148 3
    }
149
150
    /**
151
     * A command run when a population has finished.
152
     *
153
     * @param string $indexName
154
     *
155
     * @deprecated
156
     */
157
    public function postPopulate($indexName)
158
    {
159
        $this->switchIndexAlias($indexName);
160
    }
161
162
    /**
163
     * Switching aliases.
164
     *
165
     * @param string $indexName
166
     * @param bool   $delete    Delete or close index
167
     *
168
     * @throws \FOS\ElasticaBundle\Exception\AliasIsIndexException
169
     */
170 2
    public function switchIndexAlias($indexName, $delete = true)
171
    {
172 2
        $indexConfig = $this->configManager->getIndexConfiguration($indexName);
173
174 2
        if ($indexConfig->isUseAlias()) {
175 1
            $index = $this->indexManager->getIndex($indexName);
176 1
            $this->aliasProcessor->switchIndexAlias($indexConfig, $index, false, $delete);
177
        }
178 2
    }
179
}
180