Completed
Pull Request — develop (#512)
by Adrian
16:23 queued 09:34
created

LoaderFactory::addLoaderDefinition()   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
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 2
1
<?php
2
/**
3
 * LoaderFactory
4
 */
5
6
namespace Graviton\ProxyBundle\Definition\Loader;
7
8
use Graviton\ProxyBundle\Exception\LoaderException;
9
10
/**
11
 * Class LoaderFactory
12
 *
13
 * @package Graviton\ProxyBundle\Definition\Loader
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 LoaderFactory
19
{
20
    /** @var array  */
21
    private $loader;
22
23
24
    /**
25
     * Adds a loader to the set of registered loaders.
26
     *
27
     * @param LoaderInterface $loader Loader definintion
28
     * @param string          $key    Indentifier to find the registered loader
29
     *
30
     * @return void
31
     */
32
    public function addLoaderDefinition(LoaderInterface $loader, $key)
33
    {
34
        $this->loader[$key] = $loader;
35
    }
36
37
    /**
38
     * Provides the definition loader identified by the given key.
39
     *
40
     * @param string $key Name of the loader to be returned
41
     *
42
     * @return LoaderInterface|null
43
     */
44
    public function getLoaderDefinition($key)
45
    {
46
        if (array_key_exists($key, $this->loader)) {
47
            return $this->loader[$key];
48
        }
49
50
        return null;
51
    }
52
53
    /**
54
     * Provides a list of registered loaders
55
     *
56
     * @return array
57
     */
58
    public function getLoaderDefinitions()
59
    {
60
        return $this->loader;
61
    }
62
63
    /**
64
     * Provides
65
     *
66
     * @param string $source Information of what loader to be initiated.
67
     *
68
     * @return LoaderInterface
69
     *
70
     * @throws LoaderException
71
     */
72
    public function create($source)
73
    {
74
        if (array_key_exists($source, $this->loader)) {
75
            return $this->loader[$source];
76
        }
77
78
        throw new LoaderException('Expected Loader for source ('. $source .') does not exist.');
79
    }
80
}
81