Completed
Pull Request — master (#917)
by Dmitry
08:52
created

IndexManager::getDefaultIndex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace FOS\ElasticaBundle\Index;
4
5
use Elastica\IndexTemplate;
6
use FOS\ElasticaBundle\Elastica\Index;
7
8
class IndexManager
9
{
10
    /**
11
     * @var Index
12
     */
13
    private $defaultIndex;
14
15
    /**
16
     * @var array
17
     */
18
    private $indexes;
19
20
    /**
21
     * @var IndexTemplate[]
22
     */
23
    private $indexTemplates;
24
25
    /**
26
     * @param array $indexes
27
     * @param Index $defaultIndex
28
     * @param array $templates
29
     */
30 4
    public function __construct(array $indexes, Index $defaultIndex, array $templates = array())
31
    {
32 4
        $this->defaultIndex = $defaultIndex;
33 4
        $this->indexes = $indexes;
34 4
        $this->indexTemplates = $templates;
35 4
    }
36
37
    /**
38
     * Gets all registered indexes.
39
     *
40
     * @return array
41
     */
42 1
    public function getAllIndexes()
43
    {
44 1
        return $this->indexes;
45
    }
46
47
    /**
48
     * Gets an index by its name.
49
     *
50
     * @param string $name Index to return, or the default index if null
51
     *
52
     * @return Index
53
     *
54
     * @throws \InvalidArgumentException if no index exists for the given name
55
     */
56 3
    public function getIndex($name = null)
57
    {
58 3
        if (null === $name) {
59 1
            return $this->defaultIndex;
60
        }
61
62 2
        if (!isset($this->indexes[$name])) {
63 1
            throw new \InvalidArgumentException(sprintf('The index "%s" does not exist', $name));
64
        }
65
66 1
        return $this->indexes[$name];
67
    }
68
69
    /**
70
     * Gets an index template by its name.
71
     *
72
     * @param string $name Index template to return
73
     *
74
     * @return IndexTemplate
75
     *
76
     * @throws \InvalidArgumentException if no index template exists for the given name
77
     */
78
    public function getIndexTemplate($name = null)
79
    {
80
        if (!isset($this->indexTemplates[$name])) {
81
            throw new \InvalidArgumentException(sprintf('The index template "%s" does not exist', $name));
82
        }
83
84
        return $this->indexTemplates[$name];
85
    }
86
87
    /**
88
     * Gets the default index.
89
     *
90
     * @return Index
91
     */
92 1
    public function getDefaultIndex()
93
    {
94 1
        return $this->defaultIndex;
95
    }
96
}
97