Completed
Pull Request — master (#1343)
by Dmitry
06:11
created

TemplateResetter   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 6
dl 0
loc 70
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A resetAllIndexes() 0 6 2
A resetIndex() 0 16 3
A deleteTemplateIndexes() 0 4 1
1
<?php
2
/*
3
 * This file is part of the OpCart software.
4
 *
5
 * (c) 2018, ecentria group, Inc
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace FOS\ElasticaBundle\Index;
12
13
use FOS\ElasticaBundle\Configuration\ManagerInterface;
14
use Elastica\Client;
15
use Elastica\Request;
16
use FOS\ElasticaBundle\Configuration\IndexTemplateConfig;
17
18
class TemplateResetter implements ResetterInterface
19
{
20
    /***
21
     * @var ManagerInterface
22
     */
23
    private $configManager;
24
25
    /**
26
     * @var MappingBuilder
27
     */
28
    private $mappingBuilder;
29
30
    /**
31
     * @var Client
32
     */
33
    private $client;
34
35
    /**
36
     * Index template manager
37
     *
38
     * @var IndexTemplateManager
39
     */
40
    private $indexTemplateManager;
41
42
    public function __construct(
43
        ManagerInterface $configManager,
44
        MappingBuilder $mappingBuilder,
45
        Client $client,
46
        IndexTemplateManager $indexTemplateManager
47
    ) {
48
        $this->configManager = $configManager;
49
        $this->mappingBuilder = $mappingBuilder;
50
        $this->client = $client;
51
        $this->indexTemplateManager = $indexTemplateManager;
52
    }
53
54
    public function resetAllIndexes($deleteIndexes = false)
55
    {
56
        foreach ($this->configManager->getIndexNames() as $name) {
57
            $this->resetIndex($name);
58
        }
59
    }
60
61
    public function resetIndex($indexName, $deleteIndexes = false)
62
    {
63
        $indexTemplateConfig = $this->configManager->getIndexConfiguration($indexName);
64
        if (!$indexTemplateConfig instanceof IndexTemplateConfig) {
65
            throw new \RuntimeException(
66
                \sprintf('Incorrect index configuration object. Expecting IndexTemplateConfig, but got: %s ', \get_class($indexTemplateConfig))
67
            );
68
        }
69
        $indexTemplate = $this->indexTemplateManager->getIndexTemplate($indexName);
70
71
        $mapping = $this->mappingBuilder->buildIndexTemplateMapping($indexTemplateConfig);
72
        if ($deleteIndexes) {
73
            $this->deleteTemplateIndexes($indexTemplateConfig);
74
        }
75
        $indexTemplate->create($mapping);
76
    }
77
78
    /**
79
     * Delete all template indexes
80
     *
81
     * @param IndexTemplateConfig $template
82
     */
83
    public function deleteTemplateIndexes(IndexTemplateConfig $template)
84
    {
85
        $this->client->request($template->getTemplate() . '/', Request::DELETE);
86
    }
87
}
88