Completed
Branch master (a1bb4f)
by Veaceslav
16:31 queued 03:03
created

Schema::getBasePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * This file is part of Rebilly.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @see http://rebilly.com
9
 */
10
11
namespace Rebilly\OpenAPI;
12
13
/**
14
 * Schema representation.
15
 */
16
final class Schema
17
{
18
    /**
19
     * Schema definition.
20
     *
21
     * @var object
22
     */
23
    private $schema;
24
25
    /**
26
     * @param object $schema
27
     */
28
    public function __construct($schema)
29
    {
30
        if (!(isset($schema->swagger) && $schema->swagger === '2.0')) {
31
            throw new UnexpectedValueException('Unsupported OpenAPI Specification schema');
32
        }
33
34
        $this->schema = $schema;
35
    }
36
37
    /**
38
     * @return string
39
     */
40
    public function getHost()
41
    {
42
        return $this->fetch($this->schema, 'host');
43
    }
44
45
    /**
46
     * @return string
47
     */
48
    public function getBasePath()
49
    {
50
        return $this->fetch($this->schema, 'basePath');
51
    }
52
53
    /**
54
     * @param string $name
55
     *
56
     * @return object
57
     */
58
    public function getDefinition($name)
59
    {
60
        return $this->fetch($this->schema, 'definitions', $name);
61
    }
62
63
    /**
64
     * @return string[]
65
     */
66
    public function getDefinitionNames()
67
    {
68
        return array_keys((array) $this->fetch($this->schema, 'definitions'));
69
    }
70
71
    /**
72
     * @param string $template
73
     *
74
     * @return object
75
     */
76
    public function getPathSchema($template)
77
    {
78
        return $this->fetch($this->schema, 'paths', $template);
79
    }
80
81
    /**
82
     * @return string[]
83
     */
84
    public function getAvailablePaths()
85
    {
86
        return array_keys((array) $this->fetch($this->schema, 'paths'));
87
    }
88
89
    /**
90
     * @param string $template Schema path template.
91
     *
92
     * @return string[]
93
     */
94
    public function getAllowedMethods($template)
95
    {
96
        $schema = $this->getPathSchema($template);
97
        $methods = [
98
            'OPTIONS' => true,
99
            'HEAD' => isset($schema->get),
100
            'GET' => isset($schema->get),
101
            'POST' => isset($schema->post),
102
            'PUT' => isset($schema->put),
103
            'DELETE' => isset($schema->delete),
104
            'PATCH' => isset($schema->patch),
105
        ];
106
107
        return array_keys(array_filter($methods));
108
    }
109
110
    /**
111
     * The transfer protocol for the operation.
112
     * The value overrides the top-level schemes definition.
113
     *
114
     * @see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#operationObject
115
     *
116
     * @param string $template
117
     * @param string $method
118
     *
119
     * @return string[]
120
     */
121 View Code Duplication
    public function getSupportedSchemes($template, $method)
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...
122
    {
123
        $schemes = $this->fetch(
124
            $this->schema,
125
            'paths',
126
            $template,
127
            $method,
128
            'schemes'
129
        );
130
131
        if (!$schemes) {
132
            $schemes = $this->fetch($this->schema, 'schemes');
133
        }
134
135
        return (array) $schemes;
136
    }
137
138
    /**
139
     * @param string $template
140
     * @param string $method
141
     *
142
     * @return object[]
143
     */
144
    public function getRequestHeaderSchemas($template, $method)
145
    {
146
        return $this->getRequestParameters($template, $method, 'header');
147
    }
148
149
    /**
150
     * @param string $template
151
     * @param string $method
152
     *
153
     * @return object|null
154
     */
155
    public function getRequestBodySchema($template, $method)
156
    {
157
        $parameters = $this->getRequestParameters($template, $method, 'body');
158
159
        if (count($parameters) > 1) {
160
            throw new UnexpectedValueException('Multiple body parameters found');
161
        }
162
163
        return reset($parameters) ?: null;
164
    }
165
166
    /**
167
     * @param string $template
168
     * @param string $method
169
     *
170
     * @return object[]
171
     */
172
    public function getRequestPathParameters($template, $method)
173
    {
174
        return $this->getRequestParameters($template, $method, 'path');
175
    }
176
177
    /**
178
     * @param string $template
179
     * @param string $method
180
     *
181
     * @return object[]
182
     */
183
    public function getRequestQueryParameters($template, $method)
184
    {
185
        return $this->getRequestParameters($template, $method, 'query');
186
    }
187
188
    /**
189
     * @param string $template
190
     * @param string $method
191
     *
192
     * @return string[]
193
     */
194 View Code Duplication
    public function getRequestContentTypes($template, $method)
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...
195
    {
196
        $items = $this->fetch($this->schema, 'paths', $template, $method, 'consumes');
197
198
        if (!$items) {
199
            $items = $this->fetch($this->schema, 'consumes');
200
        }
201
202
        return (array) $items;
203
    }
204
205
    /**
206
     * @param string $template
207
     * @param string $method
208
     *
209
     * @return string[]
210
     */
211
    public function getResponseCodes($template, $method)
212
    {
213
        return array_filter(
214
            array_keys((array) $this->fetch($this->schema, 'paths', $template, $method, 'responses')),
215
            'is_numeric'
216
        );
217
    }
218
219
    /**
220
     * @param string $template
221
     * @param string $method
222
     *
223
     * @return string[]
224
     */
225 View Code Duplication
    public function getResponseContentTypes($template, $method)
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...
226
    {
227
        $items = $this->fetch($this->schema, 'paths', $template, $method, 'produces');
228
229
        if (!$items) {
230
            $items = $this->fetch($this->schema, 'produces');
231
        }
232
233
        return (array) $items;
234
    }
235
236
    /**
237
     * @param string $template
238
     * @param string $method
239
     * @param string $status
240
     *
241
     * TODO: Normalize headers list to JSON schema (seems it is validator deals)
242
     * TODO: If status does not defined, check default response declaration
243
     *
244
     * @return object[]
245
     */
246
    public function getResponseHeaderSchemas($template, $method, $status)
247
    {
248
        return (array) $this->fetch(
249
            $this->schema,
250
            'paths',
251
            $template,
252
            $method,
253
            'responses',
254
            $status,
255
            'headers'
256
        );
257
    }
258
259
    /**
260
     * Returns body schema.
261
     *
262
     * @param string $template
263
     * @param string $method
264
     * @param string $status
265
     *
266
     * @return object|null
267
     */
268
    public function getResponseBodySchema($template, $method, $status)
269
    {
270
        return $this->fetch(
271
            $this->schema,
272
            'paths',
273
            $template,
274
            $method,
275
            'responses',
276
            $status,
277
            'schema'
278
        );
279
    }
280
281
    /**
282
     * @param $schema
283
     * @param array ...$paths
284
     *
285
     * @return mixed
286
     */
287
    private static function fetch($schema, ...$paths)
288
    {
289
        foreach ($paths as $path) {
290
            if (!isset($schema->{$path})) {
291
                return null;
292
            }
293
294
            $schema = $schema->{$path};
295
        }
296
297
        return $schema;
298
    }
299
300
    /**
301
     * A list of parameters that are applicable for this operation.
302
     * If a parameter is already defined at the Path Item,
303
     * the new definition will override it, but can never remove it.
304
     *
305
     * @param string $template
306
     * @param string $method
307
     * @param string $location
308
     *
309
     * @return object[]
310
     */
311
    private function getRequestParameters($template, $method, $location)
312
    {
313
        $path = $this->fetch($this->schema, 'paths', $template);
314
315
        $parameters = $this->fetch($path, $method, 'parameters') ?: $this->fetch($path, 'parameters');
316
        $schemas = [];
317
318
        foreach ((array) $parameters as $parameter) {
319
            if ($parameter->in !== $location) {
320
                continue;
321
            }
322
323
            if (isset($schemas[$parameter->name])) {
324
                throw new UnexpectedValueException('Multiple parameters found');
325
            }
326
327
            $schemas[$parameter->name] = clone $parameter;
328
            unset($schemas[$parameter->name]->name, $schemas[$parameter->name]->in);
329
        }
330
331
        return $schemas;
332
    }
333
}
334