Completed
Pull Request — master (#12)
by
unknown
14:43
created

LoaderManager::setDecoderManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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