Passed
Push — master ( 26d549...0e9514 )
by Alain
02:38
created

LoaderFactory   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 38.89%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 8
c 1
b 0
f 1
lcom 1
cbo 1
dl 0
loc 97
ccs 7
cts 18
cp 0.3889
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createFromUri() 0 15 3
A getLoader() 0 17 3
A registerLoader() 0 8 2
1
<?php
2
/**
3
 * Bright Nucleus Config Component.
4
 *
5
 * @package   BrightNucleus\Config
6
 * @author    Alain Schlesser <[email protected]>
7
 * @license   MIT
8
 * @link      http://www.brightnucleus.com/
9
 * @copyright 2016 Alain Schlesser, Bright Nucleus
10
 */
11
12
namespace BrightNucleus\Config\Loader;
13
14
use BrightNucleus\Config\Exception\FailedToLoadConfigException;
15
use Exception;
16
17
/**
18
 * Class LoaderFactory.
19
 *
20
 * @since   0.4.0
21
 *
22
 * @package BrightNucleus\Config\Loader
23
 * @author  Alain Schlesser <[email protected]>
24
 */
25
class LoaderFactory
26
{
27
28
    /**
29
     * Array of fully qualified class names of known loaders.
30
     *
31
     * @var array<string>
32
     *
33
     * @since 0.4.0
34
     */
35
    protected static $loaders = [
36
        'BrightNucleus\Config\Loader\PHPLoader',
37
    ];
38
39
    /**
40
     * Array of instantiated loaders.
41
     *
42
     * These are lazily instantiated and added as needed.
43
     *
44
     * @var LoaderInterface[]
45
     *
46
     * @since 0.4.0
47
     */
48
    protected static $loaderInstances = [];
49
50
    /**
51
     * Create a new Loader from an URI.
52
     *
53
     * @since 0.4.0
54
     *
55
     * @param string $uri URI of the resource to create a loader for.
56
     *
57
     * @return LoaderInterface Loader that is able to load the given URI.
58
     * @throws FailedToLoadConfigException If no suitable loader was found.
59
     */
60 1
    public static function createFromUri($uri)
61
    {
62 1
        foreach (static::$loaders as $loader) {
63 1
            if ($loader::canLoad($uri)) {
64 1
                return static::getLoader($loader);
65
            }
66
        }
67
68
        throw new FailedToLoadConfigException(
69
            sprintf(
70
                _('Could not find a suitable loader for URI "%1$s".'),
71
                $uri
72
            )
73
        );
74
    }
75
76
    /**
77
     * Get an instance of a specific loader.
78
     *
79
     * The loader is lazily instantiated if needed.
80
     *
81
     * @since 0.4.0
82
     *
83
     * @param string $loaderClass Fully qualified class name of the loader to get.
84
     *
85
     * @return LoaderInterface Instance of the requested loader.
86
     * @throws FailedToLoadConfigException If the loader class could not be instantiated.
87
     */
88 1
    public static function getLoader($loaderClass)
89
    {
90
        try {
91 1
            if (! array_key_exists($loaderClass, static::$loaderInstances)) {
92
                static::$loaderInstances[$loaderClass] = new $loaderClass;
93
            }
94
95 1
            return static::$loaderInstances[$loaderClass];
96
        } catch (Exception $exception) {
97
            throw new FailedToLoadConfigException(
98
                sprintf(
99
                    _('Could not instantiate the requested loader class "%1$s".'),
100
                    $loaderClass
101
                )
102
            );
103
        }
104
    }
105
106
    /**
107
     * Register a new loader.
108
     *
109
     * @since 0.4.0
110
     *
111
     * @param string $loader Fully qualified class name of a loader implementing LoaderInterface.
112
     */
113
    public static function registerLoader($loader)
114
    {
115
        if (in_array($loader, static::$loaders, true)) {
116
            return;
117
        }
118
119
        static::$loaders [] = $loader;
120
    }
121
}
122