IndexManager::getAllIndexes()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * This file is part of the FOSElasticaBundle package.
5
 *
6
 * (c) FriendsOfSymfony <https://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\Elastica\Index;
15
16
class IndexManager
17
{
18
    /**
19
     * @var Index
20
     */
21
    private $defaultIndex;
22
23
    /**
24
     * @var array
25
     */
26
    private $indexes;
27
28 11
    public function __construct(array $indexes, Index $defaultIndex)
29
    {
30 11
        $this->defaultIndex = $defaultIndex;
31 11
        $this->indexes = $indexes;
32 11
    }
33
34
    /**
35
     * Gets all registered indexes.
36
     */
37 1
    public function getAllIndexes(): array
38
    {
39 1
        return $this->indexes;
40
    }
41
42
    /**
43
     * Gets an index by its name or the default index.
44
     *
45
     * @throws \InvalidArgumentException if no index exists for the given name
46
     */
47 6
    public function getIndex(?string $name = null): Index
48
    {
49 6
        if (null === $name) {
50 1
            return $this->defaultIndex;
51
        }
52
53 5
        if (!isset($this->indexes[$name])) {
54 1
            throw new \InvalidArgumentException(\sprintf('The index "%s" does not exist', $name));
55
        }
56
57 4
        return $this->indexes[$name];
58
    }
59
60
    /**
61
     * Gets the default index.
62
     */
63 1
    public function getDefaultIndex(): Index
64
    {
65 1
        return $this->defaultIndex;
66
    }
67
}
68