Completed
Pull Request — master (#12)
by
unknown
04:08 queued 02:45
created

DecoderManager   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 90.63%

Importance

Changes 0
Metric Value
dl 0
loc 101
ccs 29
cts 32
cp 0.9063
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 2

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 3
A registerDecoder() 0 4 1
A getDecoders() 0 4 1
A setIgnoreUnknownExtension() 0 4 1
A getDecoder() 0 12 3
A hasDecoder() 0 4 1
A registerDefaultDecoder() 0 6 1
1
<?php
2
3
namespace League\JsonReference;
4
5
use League\JsonReference\Decoder\JsonDecoder;
6
use League\JsonReference\Decoder\YamlDecoder;
7
8
final class DecoderManager
9
{
10
11
    
12
    /**
13
     * @var ParserInterface[]
14
     */
15
    private $decoders = [];
16
17
    /**
18
     * @var bool
19
     */
20
    private $ignoreUnknownExtension = [];
21
22
    /**
23
     * @param DecoderInterface[] $decoders
24
     */
25 104
    public function __construct(array $decoders = [], $ignoreUnknownExtension = true)
26
    {
27 104
        if (empty($decoders)) {
28 102
            $this->registerDefaultDecoder();
29 51
        }
30
        
31 104
        foreach ($decoders as $extension => $decoder) {
32 2
            $this->registerDecoder($extension, $decoder);
33 52
        }
34
35 104
        $this->ignoreUnknownExtension = $ignoreUnknownExtension;
36 104
    }
37
38
    /**
39
     * Register a DecoderInterface for the given extension.
40
     *
41
     * @param DecoderInterface $decoder
42
     */
43 104
    public function registerDecoder($extension, DecoderInterface $decoder)
44
    {
45 104
        $this->decoders[$extension] = $decoder;
46 104
    }
47
48
    /**
49
     * Get all registered decoders, keyed by the extensions they are registered to decode schemas for.
50
     *
51
     * @return DecoderInterface[]
52
     */
53 2
    public function getDecoders()
54
    {
55 2
        return $this->decoders;
56
    }
57
58
    /**
59
     * Set to true to use default decoder for unknown file extensions
60
     *
61
     * @param bool
62
     */
63
    public function setIgnoreUnknownExtension($ignoreUnknownExtension)
64
    {
65
        $this->ignoreUnknownExtension = $ignoreUnknownExtension;
66
    }
67
68
    /**
69
     * Get the decoder for the given extension.
70
     *
71
     * @param string $extension
72
     *
73
     * @return DecoderInterface
74
     * @throws \InvalidArgumentException
75
     */
76 58
    public function getDecoder($extension)
77
    {
78 58
        if (!$this->hasDecoder($extension)) {
79 4
            if ($this->ignoreUnknownExtension) {
80 2
                $extension = 'json';
81 1
            } else {
82 2
                throw new \InvalidArgumentException(sprintf('A decoder is not registered for the extension "%s"', $extension));
83
            }
84 1
        }
85
86 56
        return $this->decoders[$extension];
87
    }
88
    
89
    /**
90
     * @param string $extension
91
     *
92
     * @return bool
93
     */
94 60
    public function hasDecoder($extension)
95
    {
96 60
        return isset($this->decoders[$extension]);
97
    }
98
99
    /**
100
     * Register the default decoder.
101
     */
102 102
    private function registerDefaultDecoder()
103
    {
104 102
        $this->registerDecoder('json', new JsonDecoder());
105 102
        $this->registerDecoder('yaml', new YamlDecoder());
106 102
        $this->registerDecoder('yml', $this->decoders['yaml']);
107 102
    }
108
}
109