Test Failed
Pull Request — master (#33)
by Joao
09:10
created

OpenApiSchema::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 7
Ratio 100 %

Importance

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