Completed
Push — master ( da6615...13704a )
by Joao Gilberto
03:04
created

SwaggerSchema::getHttpSchema()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 0
1
<?php
2
/**
3
 * User: jg
4
 * Date: 22/05/17
5
 * Time: 09:29
6
 */
7
8
namespace ByJG\Swagger;
9
10
use ByJG\Swagger\Exception\DefinitionNotFoundException;
11
use ByJG\Swagger\Exception\HttpMethodNotFoundException;
12
use ByJG\Swagger\Exception\InvalidDefinitionException;
13
use ByJG\Swagger\Exception\PathNotFoundException;
14
15
class SwaggerSchema
16
{
17
    protected $jsonFile;
18
19
    public function __construct($jsonFile)
20
    {
21
        $this->jsonFile = json_decode($jsonFile, true);
22
    }
23
24
    public function getHttpSchema()
25
    {
26
        return isset($this->jsonFile['schemes']) ? $this->jsonFile['schemes'][0] : '';
27
    }
28
29
    public function getHost()
30
    {
31
        return isset($this->jsonFile['host']) ? $this->jsonFile['host'] : '';
32
    }
33
34
    public function getBasePath()
35
    {
36
        return isset($this->jsonFile['basePath']) ? $this->jsonFile['basePath'] : '';
37
    }
38
39
    public function getPath($path)
40
    {
41
        $path = preg_replace('~^' . $this->getBasePath() . '~', '', $path);
42
43
        // Try direct match
44
        if (isset($this->jsonFile['paths'][$path])) {
45
            return $this->jsonFile['paths'][$path];
46
        }
47
48
        // Try inline parameter
49
        foreach (array_keys($this->jsonFile['paths']) as $pathItem) {
50
            if (strpos($pathItem, '{') === false) {
51
                continue;
52
            }
53
54
            $pathItemPattern = '~^' . preg_replace('~\{.*?\}~', '([^/]+)', $pathItem) . '$~';
55
56
            if (preg_match($pathItemPattern, $path)) {
57
                return $this->jsonFile['paths'][$pathItem];
58
            }
59
        }
60
61
        throw new PathNotFoundException('Path "' . $path . '" not found');
62
    }
63
64
    public function getPathStructure($path, $method)
65
    {
66
        $pathDef = $this->getPath($path);
67
        $method = strtolower($method);
68
        if (!isset($pathDef[$method])) {
69
            throw new HttpMethodNotFoundException("The http method '$method' not found in '$path'");
70
        }
71
        return $pathDef[$method];
72
    }
73
74
    public function getDefintion($name)
75
    {
76
        $nameParts = explode('/', $name);
77
78
        if (count($nameParts) < 3 || $nameParts[0] != '#') {
79
            throw new InvalidDefinitionException('Invalid Definition');
80
        }
81
82
        if (!isset($this->jsonFile[$nameParts[1]][$nameParts[2]])) {
83
            throw new DefinitionNotFoundException("Definition '$name' not found");
84
        }
85
86
        return $this->jsonFile[$nameParts[1]][$nameParts[2]];
87
    }
88
89
    public function getRequestParameters($path, $method)
90
    {
91
        $structure = $this->getPathStructure($path, $method);
92
93
        return new SwaggerRequestBody($this, "$method $path", $structure['parameters']);
94
    }
95
96
    public function getResponseParameters($path, $method, $status)
97
    {
98
        $structure = $this->getPathStructure($path, $method);
99
100
        if (!isset($structure['responses'][$status])) {
101
            throw new InvalidDefinitionException("Could not found status code '$status' in '$path' and '$method'");
102
        }
103
104
        return new SwaggerResponseBody($this, "$method $status $path", $structure['responses'][$status]);
105
    }
106
107
108
}
109