Completed
Pull Request — master (#20)
by
unknown
13:16
created

DecoderManager::__construct()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 10
nc 6
nop 2
dl 0
loc 19
rs 8.8571
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
    public function __construct(array $decoders = [], $defaultType = null)
20
    {
21
        if (empty($decoders)) {
22
            $this->registerDefaultDecoders();
23
            
24
            // Backwards compatibilty
25
            if ($defaultType === null) {
26
                $defaultType = 'json';
27
            }
28
        } else {
29
            foreach ($decoders as $type => $decoder) {
30
                $this->registerDecoder($type, $decoder);
31
            }
32
        }
33
34
        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
            $this->setDefaultType($defaultType);
36
        }
37
    }
38
39
    /**
40
     * Register a DecoderInterface for the given MIME-types.
41
     *
42
     * @param string $type
43
     * @param DecoderInterface $decoder
44
     */
45
    public function registerDecoder($type, DecoderInterface $decoder = null)
46
    {
47
        if ($decoder) {
48
            $this->decoders[$type] = $decoder;
49
        } else {
50
            unset($this->decoders[$type]);
51
        }
52
    }
53
54
    /**
55
     * Get all registered decoders, keyed by the extensions they are registered to decode schemas for.
56
     *
57
     * @return DecoderInterface[]
58
     */
59
    public function getDecoders()
60
    {
61
        return $this->decoders;
62
    }
63
64
    /**
65
     * Set the default type for unknown files
66
     *
67
     * @param string defaultType
68
     */
69
    public function setDefaultType($defaultType = null)
70
    {
71
        $this->registerDecoder(null, $defaultType ? $this->getDecoder($defaultType) : null);
72
    }
73
74
    /**
75
     * Get the decoder for the given extension.
76
     *
77
     * @param string $type
78
     *
79
     * @return DecoderInterface
80
     * @throws \InvalidArgumentException
81
     */
82
    public function getDecoder($type)
83
    {
84
        if (!$this->hasDecoder($type)) {
85
            if ($this->hasDecoder(null)) {
86
                $type = null;
87
            } else {
88
                throw new \InvalidArgumentException(
89
                    sprintf('A decoder is not registered for the extension "%s"', $type)
90
                );
91
            }
92
        }
93
94
        return $this->decoders[$type];
95
    }
96
    
97
    /**
98
     * @param string $type
99
     *
100
     * @return bool
101
     */
102
    public function hasDecoder($type)
103
    {
104
        return isset($this->decoders[$type]);
105
    }
106
107
    /**
108
     * Register the default decoders
109
     */
110
    private function registerDefaultDecoders()
111
    {
112
        $this->registerJsonDecoder();
113
    }
114
115
    /**
116
     * @param DecoderInterface $decoder
117
     */
118 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
        $decoder = $decoder ?: new JsonDecoder();
121
122
        $this->registerDecoder('json', $decoder);
123
        $this->registerDecoder('text/json', $decoder);
124
        $this->registerDecoder('application/json', $decoder);
125
        $this->registerDecoder('+json', $decoder);
126
    }
127
128
    /**
129
     * @param DecoderInterface $decoder
130
     */
131 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
        $decoder = $decoder ?: new YamlDecoder();
134
135
        $this->registerDecoder('yml', $decoder);
136
        $this->registerDecoder('yaml', $decoder);
137
        $this->registerDecoder('text/yaml', $decoder);
138
        $this->registerDecoder('application/x-yaml', $decoder);
139
        $this->registerDecoder('+yaml', $decoder);
140
    }
141
}
142