Completed
Pull Request — master (#992)
by David
05:34
created

ConfigManager::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4286
cc 2
eloc 3
nc 2
nop 1
crap 2
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 4
    public function __construct(array $sources)
28
    {
29 4
        foreach ($sources as $source) {
30 4
            $this->indexes = array_merge($source->getConfiguration(), $this->indexes);
31
        }
32 4
    }
33
34 4
    public function getIndexConfiguration($indexName)
35
    {
36 4
        if (!$this->hasIndexConfiguration($indexName)) {
37
            throw new \InvalidArgumentException(sprintf('Index with name "%s" is not configured.', $indexName));
38
        }
39
40 4
        return $this->indexes[$indexName];
41
    }
42
43
    public function getIndexNames()
44
    {
45
        return array_keys($this->indexes);
46
    }
47
48 1
    public function getTypeConfiguration($indexName, $typeName)
49
    {
50 1
        $index = $this->getIndexConfiguration($indexName);
51 1
        $type = $index->getType($typeName);
52
53 1
        if (!$type) {
54
            throw new \InvalidArgumentException(sprintf('Type with name "%s" on index "%s" is not configured', $typeName, $indexName));
55
        }
56
57 1
        return $type;
58
    }
59
60 4
    public function hasIndexConfiguration($indexName)
61
    {
62 4
        return isset($this->indexes[$indexName]);
63
    }
64
}
65