Resetter::resetIndex()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 12
cts 12
cp 1
rs 9.6
c 0
b 0
f 0
cc 4
nc 4
nop 3
crap 4
1
<?php
2
3
/*
4
 * This file is part of the FOSElasticaBundle package.
5
 *
6
 * (c) FriendsOfSymfony <https://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 FOS\ElasticaBundle\Configuration\ManagerInterface;
15
use FOS\ElasticaBundle\Event\PostIndexResetEvent;
16
use FOS\ElasticaBundle\Event\PreIndexResetEvent;
17
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
18
19
/**
20
 * Deletes and recreates indexes.
21
 */
22
class Resetter implements ResetterInterface
23
{
24
    /**
25
     * @var AliasProcessor
26
     */
27
    private $aliasProcessor;
28
29
    /***
30
     * @var ManagerInterface
31
     */
32
    private $configManager;
33
34
    /**
35
     * @var EventDispatcherInterface
36
     */
37
    private $dispatcher;
38
39
    /**
40
     * @var IndexManager
41
     */
42
    private $indexManager;
43
44
    /**
45
     * @var MappingBuilder
46
     */
47
    private $mappingBuilder;
48
49 15 View Code Duplication
    public function __construct(
50
        ManagerInterface $configManager,
51
        IndexManager $indexManager,
52
        AliasProcessor $aliasProcessor,
53
        MappingBuilder $mappingBuilder,
54
        EventDispatcherInterface $eventDispatcher
55
    ) {
56 15
        $this->aliasProcessor = $aliasProcessor;
57 15
        $this->configManager = $configManager;
58 15
        $this->dispatcher = $eventDispatcher;
59 15
        $this->indexManager = $indexManager;
60 15
        $this->mappingBuilder = $mappingBuilder;
61 15
    }
62
63
    /**
64
     * Deletes and recreates all indexes.
65
     */
66 1
    public function resetAllIndexes(bool $populating = false, bool $force = false)
67
    {
68 1
        foreach ($this->configManager->getIndexNames() as $name) {
69 1
            $this->resetIndex($name, $populating, $force);
70
        }
71 1
    }
72
73
    /**
74
     * Deletes and recreates the named index. If populating, creates a new index
75
     * with a randomised name for an alias to be set after population.
76
     *
77
     * @throws \InvalidArgumentException if no index exists for the given name
78
     */
79 8
    public function resetIndex(string $indexName, bool $populating = false, bool $force = false)
80
    {
81 8
        $indexConfig = $this->configManager->getIndexConfiguration($indexName);
82 7
        $index = $this->indexManager->getIndex($indexName);
83
84 7
        if ($indexConfig->isUseAlias()) {
85 1
            $this->aliasProcessor->setRootName($indexConfig, $index);
0 ignored issues
show
Compatibility introduced by
$indexConfig of type object<FOS\ElasticaBundl...n\IndexConfigInterface> is not a sub-type of object<FOS\ElasticaBundl...figuration\IndexConfig>. It seems like you assume a concrete implementation of the interface FOS\ElasticaBundle\Confi...on\IndexConfigInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
86
        }
87
88 7
        $this->dispatcher->dispatch($event = new PreIndexResetEvent($indexName, $populating, $force));
0 ignored issues
show
Documentation introduced by
$event = new \FOS\Elasti...e, $populating, $force) is of type object<FOS\ElasticaBundl...ent\PreIndexResetEvent>, but the function expects a object<Symfony\Contracts\EventDispatcher\object>.

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...
89
90 7
        $mapping = $this->mappingBuilder->buildIndexMapping($indexConfig);
91 7
        $index->create($mapping, ['recreate' => true]);
92
93 7
        if (!$populating and $indexConfig->isUseAlias()) {
94 1
            $this->aliasProcessor->switchIndexAlias($indexConfig, $index, $force);
0 ignored issues
show
Compatibility introduced by
$indexConfig of type object<FOS\ElasticaBundl...n\IndexConfigInterface> is not a sub-type of object<FOS\ElasticaBundl...figuration\IndexConfig>. It seems like you assume a concrete implementation of the interface FOS\ElasticaBundle\Confi...on\IndexConfigInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
95
        }
96
97 7
        $this->dispatcher->dispatch(new PostIndexResetEvent($indexName, $populating, $force));
0 ignored issues
show
Documentation introduced by
new \FOS\ElasticaBundle\...e, $populating, $force) is of type object<FOS\ElasticaBundl...nt\PostIndexResetEvent>, but the function expects a object<Symfony\Contracts\EventDispatcher\object>.

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...
98 7
    }
99
100
    /**
101
     * Switch index alias.
102
     *
103
     * @throws \FOS\ElasticaBundle\Exception\AliasIsIndexException
104
     */
105 2
    public function switchIndexAlias(string $indexName, bool $delete = true)
106
    {
107 2
        $indexConfig = $this->configManager->getIndexConfiguration($indexName);
108
109 2
        if ($indexConfig->isUseAlias()) {
110 1
            $index = $this->indexManager->getIndex($indexName);
111 1
            $this->aliasProcessor->switchIndexAlias($indexConfig, $index, false, $delete);
0 ignored issues
show
Compatibility introduced by
$indexConfig of type object<FOS\ElasticaBundl...n\IndexConfigInterface> is not a sub-type of object<FOS\ElasticaBundl...figuration\IndexConfig>. It seems like you assume a concrete implementation of the interface FOS\ElasticaBundle\Confi...on\IndexConfigInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
112
        }
113 2
    }
114
}
115