Completed
Pull Request — master (#20)
by
unknown
01:30
created

DecoderManager::hasDecoder()   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 1
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\Decoder\JsonDecoder;
6
use League\JsonReference\Decoder\YamlDecoder;
7
8
final class DecoderManager
9
{
10
    /**
11
     * @var ParserInterface[]
12
     */
13
    private $decoders = [];
14
15
    /**
16
     * @param DecoderInterface[] $decoders
17
     * @param string $defaultType
18
     */
19 118
    public function __construct(array $decoders = [], $defaultType = null)
20
    {
21 118
        if (empty($decoders)) {
22 108
            $this->registerDefaultDecoders();
23
            
24
            // Backwards compatibilty
25 108
            if ($defaultType === null) {
26 72
                $defaultType = 'json';
27 18
            }
28 54
        } else {
29 10
            foreach ($decoders as $type => $decoder) {
30 10
                $this->registerDecoder($type, $decoder);
31 5
            }
32
        }
33
34 118
        if ($defaultType) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $defaultType of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
35 108
            $this->setDefaultType($defaultType);
36 54
        }
37 118
    }
38
39
    /**
40
     * Register a DecoderInterface for the given MIME-types.
41
     *
42
     * @param string $type
43
     * @param DecoderInterface $decoder
44
     */
45 118
    public function registerDecoder($type, DecoderInterface $decoder = null)
46
    {
47 118
        if ($decoder) {
48 118
            $this->decoders[$type] = $decoder;
49 59
        } else {
50 8
            unset($this->decoders[$type]);
51
        }
52 118
    }
53
54
    /**
55
     * Get all registered decoders, keyed by the extensions they are registered to decode schemas for.
56
     *
57
     * @return DecoderInterface[]
58
     */
59 26
    public function getDecoders()
60
    {
61 26
        return $this->decoders;
62
    }
63
64
    /**
65
     * Set the default type for unknown files
66
     *
67
     * @param string defaultType
68
     */
69 108
    public function setDefaultType($defaultType = null)
70
    {
71 108
        $this->registerDecoder(null, $defaultType ? $this->getDecoder($defaultType) : null);
72 108
    }
73
74
    /**
75
     * Get the decoder for the given extension.
76
     *
77
     * @param string $type
78
     *
79
     * @return DecoderInterface
80
     * @throws \InvalidArgumentException
81
     */
82 116
    public function getDecoder($type)
83
    {
84 116
        if (!$this->hasDecoder($type)) {
85 16
            if ($this->hasDecoder(null)) {
86 10
                $type = null;
87 5
            } else {
88 6
                throw new \InvalidArgumentException(
89 6
                    sprintf('A decoder is not registered for the extension "%s"', $type)
90 3
                );
91
            }
92 5
        }
93
94 116
        return $this->decoders[$type];
95
    }
96
    
97
    /**
98
     * @param string $type
99
     *
100
     * @return bool
101
     */
102 118
    public function hasDecoder($type)
103
    {
104 118
        return isset($this->decoders[$type]);
105
    }
106
107
    /**
108
     * Register the default decoders
109
     */
110 108
    private function registerDefaultDecoders()
111
    {
112 108
        $this->registerJsonDecoder();
113 108
    }
114
115
    /**
116
     * @param DecoderInterface $decoder
117
     */
118 108 View Code Duplication
    public function registerJsonDecoder(DecoderInterface $decoder = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
119
    {
120 108
        $decoder = $decoder ?: new JsonDecoder();
121
122 108
        $this->registerDecoder('json', $decoder);
123 108
        $this->registerDecoder('text/json', $decoder);
124 108
        $this->registerDecoder('application/json', $decoder);
125 108
        $this->registerDecoder('+json', $decoder);
126 108
    }
127
128
    /**
129
     * @param DecoderInterface $decoder
130
     */
131 2 View Code Duplication
    public function registerYamlDecoder(DecoderInterface $decoder = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
132
    {
133 2
        $decoder = $decoder ?: new YamlDecoder();
134
135 2
        $this->registerDecoder('yml', $decoder);
136 2
        $this->registerDecoder('yaml', $decoder);
137 2
        $this->registerDecoder('text/yaml', $decoder);
138 2
        $this->registerDecoder('application/x-yaml', $decoder);
139 2
        $this->registerDecoder('+yaml', $decoder);
140 2
    }
141
}
142