Completed
Pull Request — develop (#211)
by Wachter
15:13
created

ArticleIndexBuilder   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 6
c 1
b 0
f 1
lcom 1
cbo 2
dl 0
loc 52
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 4 1
A getDependencies() 0 4 1
A build() 0 5 1
A buildForManager() 0 14 3
1
<?php
2
3
/*
4
 * This file is part of Sulu.
5
 *
6
 * (c) MASSIVE ART WebServices GmbH
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Sulu\Bundle\ArticleBundle\Builder;
13
14
use ONGR\ElasticsearchBundle\Service\Manager;
15
use Sulu\Bundle\CoreBundle\Build\SuluBuilder;
16
17
/**
18
 * Builder for article-index.
19
 */
20
class ArticleIndexBuilder extends SuluBuilder
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25
    public function getName()
26
    {
27
        return 'article_index';
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function getDependencies()
34
    {
35
        return ['cache'];
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function build()
42
    {
43
        $this->buildForManager($this->container->get('es.manager.live'), $this->input->getOption('destroy'));
44
        $this->buildForManager($this->container->get('es.manager.default'), $this->input->getOption('destroy'));
45
    }
46
47
    /**
48
     * Build index for given manager.
49
     *
50
     * If index not exists - it will be created.
51
     * If index exists and destroy flag is true - drop and create index.
52
     * Else do nothing.
53
     *
54
     * @param Manager $manager
55
     * @param bool $destroy
56
     */
57
    private function buildForManager(Manager $manager, $destroy)
58
    {
59
        if (!$manager->indexExists()) {
60
            $manager->createIndex();
61
62
            return;
63
        }
64
65
        if (!$destroy) {
66
            return;
67
        }
68
69
        $manager->dropAndCreateIndex();
70
    }
71
}
72