Completed
Pull Request — master (#1569)
by
unknown
04:25
created

Resetter::resetIndexType()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 13
cts 13
cp 1
rs 9.584
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2
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 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);
86
        }
87
88 7
        $this->dispatcher->dispatch($event = new PreIndexResetEvent($indexName, $populating, $force));
89
90 7
        $mapping = $this->mappingBuilder->buildIndexMapping($indexConfig);
91 7
        $index->create($mapping, true);
92
93 7
        if (!$populating and $indexConfig->isUseAlias()) {
94 1
            $this->aliasProcessor->switchIndexAlias($indexConfig, $index, $force);
95
        }
96
97 7
        $this->dispatcher->dispatch(new PostIndexResetEvent($indexName, $populating, $force));
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);
112
        }
113 2
    }
114
}
115