LoaderResolver   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 47
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A resolveLoader() 0 10 3
A loadLoaders() 0 10 4
A loadLoader() 0 5 1
1
<?php
2
3
/*
4
 * This file is part of the Yosymfony config-loader.
5
 *
6
 * (c) YoSymfony <http://github.com/yosymfony>
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 Yosymfony\ConfigLoader;
13
14
use Yosymfony\ConfigLoader\Exception\LoaderLoadException;
15
16
/**
17
 * Loader resolver implementation
18
 *
19
 * @author Victor Puertas <[email protected]>
20
 */
21
class LoaderResolver implements LoaderResolverInterface
22
{
23
    private $loaders = [];
24
25
    /**
26
     * Constructor
27
     *
28
     * @param LoaderInterface[] The list of loaders
29
     *
30
     * @throws InvalidArgumentException If the Array of loaders is empty or null
31
     */
32
    public function __construct(array $loaders)
33
    {
34
        $this->loadLoaders($loaders);
35
    }
36
    
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function resolveLoader(string $resource, string $type = null) : ?LoaderInterface
41
    {
42
        foreach ($this->loaders as $loader) {
43
            if ($loader->supports($resource, $type)) {
44
                return $loader;
45
            }
46
        }
47
48
        return null;
49
    }
50
51
    private function loadLoaders(array $loaders) : void
52
    {
53
        if ($loaders === null || count($loaders) === 0) {
54
            throw new \InvalidArgumentException('The Array of loaders is empty or null.');
55
        }
56
57
        foreach ($loaders as $loader) {
58
            $this->loadLoader($loader);
59
        }
60
    }
61
62
    private function loadLoader(LoaderInterface $loader) : void
63
    {
64
        $loader->setLoaderResolver($this);
65
        $this->loaders[] = $loader;
66
    }
67
}
68