GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 643d67...78595b )
by Sergey
02:43
created

DecodersFactory::getDocumentDecoder()   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 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/*
3
 * This file is part of the reva2/jsonapi.
4
 *
5
 * (c) Sergey Revenko <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Reva2\JsonApi\Decoders;
12
13
use Reva2\JsonApi\Contracts\Decoders\DecodersFactoryInterface;
14
use Reva2\JsonApi\Contracts\Decoders\DocumentDecoderInterface;
15
use Reva2\JsonApi\Contracts\Decoders\QueryParamsDecoderInterface;
16
use Reva2\JsonApi\Contracts\Decoders\ResourceDecoderInterface;
17
18
/**
19
 * JSON API decoders factory
20
 *
21
 * @package Reva2\JsonApi\Decoders
22
 * @author Sergey Revenko <[email protected]>
23
 */
24
class DecodersFactory implements DecodersFactoryInterface
25
{
26
    /**
27
     * Decoders map
28
     *
29
     * @var array
30
     */
31
    protected $map;
32
33
    /**
34
     * Decoders instances
35
     *
36
     * @var array
37
     */
38
    protected $instances;
39
40
    /**
41
     * Constructor
42
     */
43 9
    public function __construct()
44
    {
45 9
        $this->map = [
46 9
            'doc' => [],
47 9
            'res' => [],
48 9
            'query' => []
49 9
        ];
50
51 9
        $this->instances = [
52 9
            'doc' => [],
53 9
            'res' => [],
54 9
            'query' => []
55 9
        ];
56 9
    }
57
58
    /**
59
     * Register decoder for JSON API resource
60
     *
61
     * @param string $resType
62
     * @param string|\Closure $decoder
63
     * @return DecodersFactory
64
     */
65 2
    public function registerResourceDecoder($resType, $decoder)
66
    {
67 2
        return $this->registerDecoderByCategory('res', $resType, $decoder);
68
    }
69
70
    /**
71
     * Register decoder for JSON API document
72
     *
73
     * @param $docType
74
     * @param $decoder
75
     * @return DecodersFactory
76
     */
77 2
    public function registerDocumentDecoder($docType, $decoder)
78
    {
79 2
        return $this->registerDecoderByCategory('doc', $docType, $decoder);
80
    }
81
82
    /**
83
     * Register decoder for JSON API request query parameter
84
     * 
85
     * @param string $queryType
86
     * @param string|\Closure $decoder
87
     * @return DecodersFactory
88
     */
89 2
    public function registerQueryParamsDecoder($queryType, $decoder)
90
    {
91 2
        return $this->registerDecoderByCategory('query', $queryType, $decoder);
92
    }
93
94
    /**
95
     * @inheritdoc
96
     */
97 3
    public function getResourceDecoder($type)
98
    {
99 3
        return $this->getDecoderInstanceByCategory('res', $type);
100
    }
101
102
    /**
103
     * @inheritdoc
104
     */
105 3
    public function getDocumentDecoder($type)
106
    {
107 3
        return $this->getDecoderInstanceByCategory('doc', $type);
108
    }
109
110
    /**
111
     * @inheritdoc
112
     */
113 3
    public function getQueryParamsDecoder($type)
114
    {
115 3
        return $this->getDecoderInstanceByCategory('query', $type);
116
    }
117
118
    /**
119
     * Register decoder in specified category
120
     *
121
     * @param string $category
122
     * @param string $resType
123
     * @param string|\Closure $decoder
124
     * @return $this
125
     */
126 6
    private function registerDecoderByCategory($category, $resType, $decoder)
127
    {
128 6
        if ((!is_string($decoder)) && (!$decoder instanceof \Closure)) {
129
            throw new \InvalidArgumentException('Decoder must be a string containing class name or \Closure instance');
130
        }
131
        
132 6
        $this->map[$category][$resType] = $decoder;
133
        
134 6
        return $this;
135
    }
136
137
    /**
138
     * Returns decoder instance for specified type from specified category
139
     *
140
     * @param string $category
141
     * @param string $type
142
     * @return mixed
143
     */
144 9
    private function getDecoderInstanceByCategory($category, $type)
145
    {
146 9
        if (!array_key_exists($type, $this->instances[$category])) {
147 9
            if (!array_key_exists($type, $this->map[$category])) {
148 3
                throw new \RuntimeException(sprintf("Decoder for type '%s' is not registered", $type));
149
            }
150
151 6
            $this->instances[$category][$type] = $this->createDecoder($category, $this->map[$category][$type]);
152 3
        }
153
154 3
        return $this->instances[$category][$type];
155
    }
156
157
    /**
158
     * Create decoder instance
159
     *
160
     * @param string $category
161
     * @param string|\Closure $decoder
162
     * @return mixed
163
     */
164 6
    private function createDecoder($category, $decoder)
165
    {
166 6
        $decoder = (is_string($decoder)) ? new $decoder() : $decoder();
167
        
168 6
        if (('res' === $category) && (!$decoder instanceof ResourceDecoderInterface)) {
169 1
            throw new \LogicException(sprintf(
170 1
                "Resource decoder must implement %s interface",
171
                ResourceDecoderInterface::class
172 1
            ));
173 5
        } elseif (('query' === $category) && (!$decoder instanceof QueryParamsDecoderInterface)) {
174 1
            throw new \LogicException(sprintf(
175 1
                "Query parameters decoder must implement %s interface",
176
                QueryParamsDecoderInterface::class
177 1
            ));
178 4
        } elseif (('doc' === $category) && (!$decoder instanceof DocumentDecoderInterface)) {
179 1
            throw new \LogicException(sprintf(
180 1
                "Document decoder must implement %s interface",
181
                DocumentDecoderInterface::class
182 1
            ));
183
        }
184
185 3
        return $decoder;
186
    }
187
}