Completed
Pull Request — master (#12)
by
unknown
03:59
created

LoaderManager::getDecoderManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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