LoaderManager   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 93.55%

Importance

Changes 0
Metric Value
dl 0
loc 96
ccs 29
cts 31
cp 0.9355
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 3

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 3
A registerLoader() 0 4 1
A getLoaders() 0 4 1
A getLoader() 0 8 2
A hasLoader() 0 4 1
A registerDefaultFileLoader() 0 4 1
A registerDefaultWebLoaders() 0 10 2
1
<?php
2
3
namespace League\JsonReference;
4
5
use League\JsonReference\Loader\CurlWebLoader;
6
use League\JsonReference\Loader\FileGetContentsWebLoader;
7
use League\JsonReference\Loader\FileLoader;
8
9
final class LoaderManager
10
{
11
    /**
12
     * @var LoaderInterface[]
13
     */
14
    private $loaders = [];
15
16
    /**
17
     * @param LoaderInterface[] $loaders
18
     */
19 62
    public function __construct(array $loaders = [])
20
    {
21 62
        if (empty($loaders)) {
22 60
            $this->registerDefaultFileLoader();
23 60
            $this->registerDefaultWebLoaders();
24 60
            return;
25
        }
26
27 2
        foreach ($loaders as $prefix => $loader) {
28 2
            $this->registerLoader($prefix, $loader);
29 1
        }
30 2
    }
31
32
    /**
33
     * Register a LoaderInterface for the given prefix.
34
     *
35
     * @param string          $prefix
36
     * @param LoaderInterface $loader
37
     */
38 14
    public function registerLoader($prefix, LoaderInterface $loader)
39
    {
40 14
        $this->loaders[$prefix] = $loader;
41 14
    }
42
43
    /**
44
     * Get all registered loaders, keyed by the prefix they are registered to load schemas for.
45
     *
46
     * @return LoaderInterface[]
47
     */
48 10
    public function getLoaders()
49
    {
50 10
        return $this->loaders;
51
    }
52
53
    /**
54
     * Get the loader for the given prefix.
55
     *
56
     * @param string $prefix
57
     *
58
     * @return LoaderInterface
59
     * @throws \InvalidArgumentException
60
     */
61 50
    public function getLoader($prefix)
62
    {
63 50
        if (!$this->hasLoader($prefix)) {
64 2
            throw new \InvalidArgumentException(sprintf('A loader is not registered for the prefix "%s"', $prefix));
65
        }
66
67 48
        return $this->loaders[$prefix];
68
    }
69
70
    /**
71
     * @param string $prefix
72
     *
73
     * @return bool
74
     */
75 52
    public function hasLoader($prefix)
76
    {
77 52
        return isset($this->loaders[$prefix]);
78
    }
79
80
    /**
81
     * Register the default file loader.
82
     */
83 60
    private function registerDefaultFileLoader()
84
    {
85 60
        $this->loaders['file'] = new FileLoader();
86 60
    }
87
88
    /**
89
     * Register the default web loaders.  If the curl extension is loaded,
90
     * the CurlWebLoader will be used.  Otherwise the FileGetContentsWebLoader
91
     * will be used.  You can override this by registering your own loader
92
     * for the 'http' and 'https' protocols.
93
     */
94 60
    private function registerDefaultWebLoaders()
95
    {
96 60
        if (function_exists('curl_init')) {
97 60
            $this->loaders['https'] = new CurlWebLoader('https://');
98 60
            $this->loaders['http']  = new CurlWebLoader('http://');
99 30
        } else {
100
            $this->loaders['https'] = new FileGetContentsWebLoader('https://');
101
            $this->loaders['http']  = new FileGetContentsWebLoader('http://');
102
        }
103 60
    }
104
}
105