Completed
Push — master ( fef1f2...3eb154 )
by Tim
7s
created

ConfigManager::getIndexConfiguration()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625
Metric Value
dl 0
loc 8
ccs 3
cts 4
cp 0.75
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2.0625
1
<?php
2
3
/**
4
 * This file is part of the FOSElasticaBundle project.
5
 *
6
 * (c) Tim Nagel <[email protected]>
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\Configuration;
13
14
/**
15
 * Central manager for index and type configuration.
16
 */
17
class ConfigManager implements ManagerInterface
18
{
19
    /**
20
     * @var IndexConfig[]
21
     */
22
    private $indexes = array();
23
24
    /**
25
     * @param Source\SourceInterface[] $sources
26
     */
27 6
    public function __construct(array $sources)
28
    {
29 6
        foreach ($sources as $source) {
30 6
            $this->indexes = array_merge($source->getConfiguration(), $this->indexes);
31 6
        }
32 6
    }
33
34 6
    public function getIndexConfiguration($indexName)
35
    {
36 6
        if (!$this->hasIndexConfiguration($indexName)) {
37
            throw new \InvalidArgumentException(sprintf('Index with name "%s" is not configured.', $indexName));
38
        }
39
40 6
        return $this->indexes[$indexName];
41
    }
42
43 1
    public function getIndexNames()
44 1
    {
45
        return array_keys($this->indexes);
46
    }
47
48 2
    public function getTypeConfiguration($indexName, $typeName)
49
    {
50 2
        $index = $this->getIndexConfiguration($indexName);
51 2
        $type = $index->getType($typeName);
52
53 2
        if (!$type) {
54
            throw new \InvalidArgumentException(sprintf('Type with name "%s" on index "%s" is not configured', $typeName, $indexName));
55
        }
56
57 2
        return $type;
58
    }
59
60 6
    public function hasIndexConfiguration($indexName)
61
    {
62 6
        return isset($this->indexes[$indexName]);
63
    }
64
}
65