Completed
Pull Request — master (#91)
by Matt
13:03 queued 11:05
created

LoaderManager   A

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\JsonGuard;
4
5
use League\JsonGuard\Loaders\CurlWebLoader;
6
use League\JsonGuard\Loaders\FileGetContentsWebLoader;
7
use League\JsonGuard\Loaders\FileLoader;
8
9
class LoaderManager
10
{
11
    /**
12
     * @var Loader[]
13
     */
14
    private $loaders = [];
15
16
    /**
17
     * @param Loader[] $loaders
18
     */
19 172
    public function __construct(array $loaders = [])
20
    {
21 172
        if (!$loaders) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $loaders of type League\JsonGuard\Loader[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
22 170
            $this->registerDefaultFileLoader();
23 170
            $this->registerDefaultWebLoaders();
24 170
            return;
25
        }
26
27 2
        foreach ($loaders as $prefix => $loader) {
28 2
            $this->registerLoader($prefix, $loader);
29 2
        }
30 2
    }
31
32
    /**
33
     * Register a Loader for the given prefix.
34
     *
35
     * @param string $prefix
36
     * @param Loader $loader
37
     */
38 126
    public function registerLoader($prefix, Loader $loader)
39
    {
40 126
        $this->loaders[$prefix] = $loader;
41 126
    }
42
43
    /**
44
     * Get all registered loaders, keyed by the prefix they are registered to load schemas for.
45
     *
46
     * @return Loader[]
47
     */
48 2
    public function getLoaders()
49
    {
50 2
        return $this->loaders;
51
    }
52
53
    /**
54
     * Get the loader for the given prefix.
55
     *
56
     * @param string $prefix
57
     *
58
     * @return Loader
59
     * @throws \InvalidArgumentException
60
     */
61 48
    public function getLoader($prefix)
62
    {
63 48
        if (!$this->hasLoader($prefix)) {
64 2
            throw new \InvalidArgumentException(sprintf('A loader is not registered for the prefix "%s"', $prefix));
65
        }
66
67 46
        return $this->loaders[$prefix];
68
    }
69
70
    /**
71
     * @param string $prefix
72
     *
73
     * @return bool
74
     */
75 50
    public function hasLoader($prefix)
76
    {
77 50
        return isset($this->loaders[$prefix]);
78
    }
79
80
    /**
81
     * Register the default file loader.
82
     */
83 170
    private function registerDefaultFileLoader()
84
    {
85 170
        $this->loaders['file'] = new FileLoader();
86 170
    }
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 170
    private function registerDefaultWebLoaders()
95
    {
96 170
        if (function_exists('curl_init')) {
97 170
            $this->loaders['https'] = new CurlWebLoader('https://');
98 170
            $this->loaders['http']  = new CurlWebLoader('http://');
99 170
        } else {
100
            $this->loaders['https'] = new FileGetContentsWebLoader('https://');
101
            $this->loaders['http']  = new FileGetContentsWebLoader('http://');
102
        }
103 170
    }
104
}
105