Passed
Push — master ( 711a5c...4de2a2 )
by Joao
05:28 queued 11s
created

OpenApiSchema::getServerUrl()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 8.9617
c 0
b 0
f 0
cc 6
nc 6
nop 0
1
<?php
2
3
namespace ByJG\ApiTools\OpenApi;
4
5
use ByJG\ApiTools\Base\Schema;
6
use ByJG\ApiTools\Exception\DefinitionNotFoundException;
7
use ByJG\ApiTools\Exception\HttpMethodNotFoundException;
8
use ByJG\ApiTools\Exception\InvalidDefinitionException;
9
use ByJG\ApiTools\Exception\NotMatchedException;
10
use ByJG\ApiTools\Exception\PathNotFoundException;
11
use ByJG\Util\Uri;
12
use InvalidArgumentException;
13
14
class OpenApiSchema extends Schema
15
{
16
17
    protected $serverVariables = [];
18
19
    /**
20
     * Initialize with schema data, which can be a PHP array or encoded as JSON.
21
     *
22
     * @param array|string $data
23
     */
24 View Code Duplication
    public function __construct($data)
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...
25
    {
26
        // when given a string, decode from JSON
27
        if (is_string($data)) {
28
            $data = json_decode($data, true);
29
        }
30
        // make sure we got an array
31
        if (!is_array($data)) {
32
            throw new InvalidArgumentException('schema must be given as array or JSON string');
33
        }
34
        $this->jsonFile = $data;
35
    }
36
37
    public function getServerUrl()
38
    {
39
        if (!isset($this->jsonFile['servers'])) {
40
            return '';
41
        }
42
        $serverUrl = $this->jsonFile['servers'][0]['url'];
43
44
        if (isset($this->jsonFile['servers'][0]['variables'])) {
45
            foreach ($this->jsonFile['servers'][0]['variables'] as $var => $value) {
46
                if (!isset($this->serverVariables[$var])) {
47
                    $this->serverVariables[$var] = $value['default'];
48
                }
49
            }
50
        }
51
52
        foreach ($this->serverVariables as $var => $value) {
53
            $serverUrl = preg_replace("/\{$var\}/", $value, $serverUrl);
54
        }
55
56
        return $serverUrl;
57
    }
58
59
    public function getBasePath()
60
    {
61
        $uriServer = new Uri($this->getServerUrl());
62
        return $uriServer->getPath();
63
    }
64
65
    /**
66
     * @param $parameterIn
67
     * @param $parameters
68
     * @param $arguments
69
     * @throws DefinitionNotFoundException
70
     * @throws InvalidDefinitionException
71
     * @throws NotMatchedException
72
     */
73
    protected function validateArguments($parameterIn, $parameters, $arguments)
74
    {
75
        foreach ($parameters as $parameter) {
76
            if (isset($parameter['$ref'])) {
77
                $paramParts = explode("/", $parameter['$ref']);
78
                if (count($paramParts) != 4 || $paramParts[0] != "#" || $paramParts[1] != self::SWAGGER_COMPONENTS || $paramParts[2] != self::SWAGGER_PARAMETERS) {
79
                    throw new InvalidDefinitionException(
80
                        "Not get the reference in the expected format #/components/parameters/<NAME>"
81
                    );
82
                }
83
                if (!isset($this->jsonFile[self::SWAGGER_COMPONENTS][self::SWAGGER_PARAMETERS][$paramParts[3]])) {
84
                    throw new DefinitionNotFoundException(
85
                        "Not find reference #/components/parameters/${paramParts[3]}"
86
                    );
87
                }
88
                $parameter = $this->jsonFile[self::SWAGGER_COMPONENTS][self::SWAGGER_PARAMETERS][$paramParts[3]];
89
            }
90 View Code Duplication
            if ($parameter['in'] === $parameterIn &&
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
91
                $parameter['schema']['type'] === "integer"
92
                && filter_var($arguments[$parameter['name']], FILTER_VALIDATE_INT) === false) {
93
                throw new NotMatchedException('Path expected an integer value');
94
            }
95
        }
96
    }
97
98
    /**
99
     * @param $name
100
     * @return mixed
101
     * @throws DefinitionNotFoundException
102
     * @throws InvalidDefinitionException
103
     */
104
    public function getDefinition($name)
105
    {
106
        $nameParts = explode('/', $name);
107
108 View Code Duplication
        if (count($nameParts) < 4 || $nameParts[0] !== '#') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
109
            throw new InvalidDefinitionException('Invalid Component');
110
        }
111
112 View Code Duplication
        if (!isset($this->jsonFile[$nameParts[1]][$nameParts[2]][$nameParts[3]])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
113
            throw new DefinitionNotFoundException("Component'$name' not found");
114
        }
115
116
        return $this->jsonFile[$nameParts[1]][$nameParts[2]][$nameParts[3]];
117
    }
118
119
    /**
120
     * @param $path
121
     * @param $method
122
     * @return OpenApiRequestBody
123
     * @throws DefinitionNotFoundException
124
     * @throws HttpMethodNotFoundException
125
     * @throws InvalidDefinitionException
126
     * @throws NotMatchedException
127
     * @throws PathNotFoundException
128
     */
129
    public function getRequestParameters($path, $method)
130
    {
131
        $structure = $this->getPathDefinition($path, $method);
132
133
        if (!isset($structure['requestBody'])) {
134
            return new OpenApiRequestBody($this, "$method $path", []);
135
        }
136
        return new OpenApiRequestBody($this, "$method $path", $structure['requestBody']);
137
    }
138
139
    public function setServerVariable($var, $value)
140
    {
141
        $this->serverVariables[$var] = $value;
142
    }
143
}
144