Passed
Pull Request — master (#14)
by
unknown
08:37
created

LoaderFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 53
ccs 0
cts 20
cp 0
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A make() 0 11 3
A forType() 0 7 2
A __construct() 0 3 1
1
<?php
2
3
namespace Northwoods\Config\Loader;
4
5
class LoaderFactory
6
{
7
    /**
8
     * @var array
9
     */
10
    private $loaders = [
0 ignored issues
show
Coding Style introduced by
Private member variable "loaders" must contain a leading underscore
Loading history...
Coding Style introduced by
Expected 1 blank line before member var; 0 found
Loading history...
11
        'php' => PhpLoader::class,
12
        'yaml' => YamlLoader::class,
13
    ];
14
15
    /**
16
     * @var LoaderInterface[]
17
     */
18
    private $instances = [];
0 ignored issues
show
Coding Style introduced by
Private member variable "instances" must contain a leading underscore
Loading history...
19
20
    /**
21
     * @param array $loaders
22
     */
23
    public function __construct(array $loaders = [])
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
24
    {
25
        $this->loaders = array_replace($this->loaders, $loaders);
26
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
27
28
    /**
29
     * @param string $extension
30
     * @return LoaderInterface
31
     */
32
    public function forType($extension)
33
    {
34
        if (empty($this->instances[$extension])) {
35
            $this->instances[$extension] = $this->make($this->loaders[$extension]);
36
        }
37
38
        return $this->instances[$extension];
39
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
40
41
    /**
42
     * @param string $loader
43
     * @return LoaderInterface
44
     * @throws LoaderException
45
     *  If the requested loader cannot be created.
46
     */
47
    private function make($loader)
48
    {
49
        if (!is_a($loader, LoaderInterface::class, true)) {
0 ignored issues
show
Coding Style introduced by
There must be a single space after a NOT operator; 0 found
Loading history...
50
            throw LoaderException::invalidLoader($loader);
51
        }
52
53
        if (!call_user_func([$loader, 'isSupported'])) {
0 ignored issues
show
Coding Style introduced by
There must be a single space after a NOT operator; 0 found
Loading history...
54
            throw LoaderException::unsupportedLoader($loader);
55
        }
56
57
        return new $loader();
58
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
59
}
60