Completed
Push — master ( 778ca6...bcebcf )
by Tobias
10:24
created

ConfigurationManager::addConfiguration()   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
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 2
1
<?php
2
3
/*
4
 * This file is part of the PHP Translation package.
5
 *
6
 * (c) PHP Translation team <[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 Translation\Bundle\Service;
13
14
use Translation\Bundle\Model\Configuration;
15
16
/**
17
 * A service to easily access different configurations.
18
 *
19
 * @author Tobias Nyholm <[email protected]>
20
 */
21
final class ConfigurationManager
22
{
23
    /**
24
     * @var Configuration[]
25
     */
26
    private $configuration = [];
27
28
    /**
29
     * @param string        $name
30
     * @param Configuration $configuration
31
     */
32
    public function addConfiguration($name, Configuration $configuration)
33
    {
34
        $this->configuration[$name] = $configuration;
35
    }
36
37
    /**
38
     * @param string $name
39
     *
40
     * @return null|Configuration
41
     */
42
    public function getConfiguration($name = 'default')
43
    {
44
        if (isset($this->configuration[$name])) {
45
            return $this->configuration[$name];
46
        }
47
48
        if ($name === 'default' || empty($name)) {
49
            $name = $this->getFirstName();
50
            if (isset($this->configuration[$name])) {
51
                return $this->configuration[$name];
52
            }
53
        }
54
    }
55
56
    /**
57
     * @return string
58
     */
59
    public function getFirstName()
60
    {
61
        foreach ($this->configuration as $name => $config) {
62
            return $name;
63
        }
64
    }
65
66
    /**
67
     * @return array
68
     */
69
    public function getNames()
70
    {
71
        return array_keys($this->configuration);
72
    }
73
}
74