Completed
Pull Request — master (#28)
by Jamal
02:52
created

LoaderCollection::has()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Majora\Framework\Loader;
4
5
/**
6
 * Class LoaderCollection
7
 */
8
class LoaderCollection
9
{
10
    /**
11
     * @var array
12
     */
13
    private $loaders;
14
15
    /**
16
     * Get a loader.
17
     *
18
     * @param $alias
19
     * @return LoaderInterface
20
     */
21
    public function get($alias)
22
    {
23
        if (!$this->has($alias)) {
24
            throw new \RuntimeException(sprintf('The loader with alias "%s" not found.', $alias));
25
        }
26
27
        return $this->loaders[$alias];
28
    }
29
30
    /**
31
     * Test if a loader exists.
32
     *
33
     * @param $alias
34
     * @return bool
35
     */
36
    public function has($alias)
37
    {
38
        return isset($this->loaders[$alias]);
39
    }
40
41
    /**
42
     * Add a new loader.
43
     *
44
     * @param $alias
45
     * @param LoaderInterface $loader
46
     */
47
    public function add($alias, LoaderInterface $loader)
48
    {
49
        if ($this->has($alias)) {
50
            throw new \RuntimeException(sprintf('Alias "%s" already used by another loader', $alias));
51
        }
52
53
        $this->loaders[$alias] = $loader;
54
    }
55
}