Completed
Pull Request — develop (#535)
by Bastian
14:26 queued 09:46
created

Registry::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 7
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 6
1
<?php
2
/**
3
 * Registry
4
 */
5
6
namespace Graviton\ProxyBundle\Service\Source;
7
8
use Graviton\ProxyBundle\Exception\RegistryException;
9
10
/**
11
 * Class Registry
12
 *
13
 * @package Graviton\ProxyBundle\Service\Source
14
 * @author  List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
15
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
16
 * @link    http://swisscom.ch
17
 */
18
class Registry
19
{
20
    /** @var array  */
21
    public $sources = [];
22
23
    /**
24
     * Adds a proxy source to the registry.
25
     *
26
     * @param SourceInterface $source The source to be registered.
27
     * @param string          $alias  The identifier the source shall be find by.
28
     *
29
     * @return Registry
30
     */
31
    public function add(SourceInterface $source, $alias)
32
    {
33
        $this->sources[$alias] = $source;
34
35
        return $this;
36
    }
37
38
    /**
39
     * Finds the source identified by the given id in the registry;
40
     *
41
     * @param string $name Identifier of the source to be returned.
42
     *
43
     * @return SourceInterface
44
     */
45
    public function get($name)
46
    {
47
        if (!empty($this->sources[$name])) {
48
            return $this->sources[$name];
49
        }
50
51
        throw new RegistryException('There is no source registered matching the given identifyer ('. $name .')');
52
    }
53
54
    /**
55
     * Indicates if the given name represents a registered source.
56
     *
57
     * @param string $name
58
     *
59
     * @return bool
60
     */
61
    public function has($name)
62
    {
63
        return in_array($name, $this->sources);
64
    }
65
}
66