Completed
Pull Request — master (#1343)
by Dmitry
04:26
created

IndexManager::getAllIndexes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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