Completed
Pull Request — master (#20)
by
unknown
13:54
created

LoaderManager::hasLoader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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