Completed
Pull Request — master (#33)
by Joao
06:03
created

SwaggerSchema::getDefintion()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14

Duplication

Lines 6
Ratio 42.86 %

Importance

Changes 0
Metric Value
dl 6
loc 14
rs 9.7998
c 0
b 0
f 0
cc 4
nc 3
nop 1
1
<?php
2
3
namespace ByJG\ApiTools\Swagger;
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
12
class SwaggerSchema extends Schema
13
{
14 View Code Duplication
    public function __construct($jsonFile, $allowNullValues = false)
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...
15
    {
16
        if (!is_array($jsonFile)) {
17
            $jsonFile = json_decode($jsonFile, true);
18
        }
19
        $this->jsonFile = $jsonFile;
20
        $this->allowNullValues = (bool) $allowNullValues;
21
    }
22
23
    public function getHttpSchema()
24
    {
25
        return isset($this->jsonFile['schemes']) ? $this->jsonFile['schemes'][0] : '';
26
    }
27
28
    public function getHost()
29
    {
30
        return isset($this->jsonFile['host']) ? $this->jsonFile['host'] : '';
31
    }
32
33
    public function getBasePath()
34
    {
35
        return isset($this->jsonFile['basePath']) ? $this->jsonFile['basePath'] : '';
36
    }
37
38
    public function getServerUrl()
39
    {
40
        $httpSchema = $this->getHttpSchema();
41
        $host = $this->getHost();
42
        $basePath = $this->getBasePath();
43
        return "$httpSchema://$host$basePath";
44
    }
45
46
    /**
47
     * @param $parameterIn
48
     * @param $parameters
49
     * @param $arguments
50
     * @throws NotMatchedException
51
     */
52
    protected function validateArguments($parameterIn, $parameters, $arguments)
53
    {
54
        foreach ($parameters as $parameter) {
55 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...
56
                && $parameter['type'] === "integer"
57
                && filter_var($arguments[$parameter['name']], FILTER_VALIDATE_INT) === false) {
58
                throw new NotMatchedException('Path expected an integer value');
59
            }
60
        }
61
    }
62
63
    /**
64
     * @param $name
65
     * @return mixed
66
     * @throws DefinitionNotFoundException
67
     * @throws InvalidDefinitionException
68
     */
69
    public function getDefintion($name)
70
    {
71
        $nameParts = explode('/', $name);
72
73 View Code Duplication
        if (count($nameParts) < 3 || $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...
74
            throw new InvalidDefinitionException('Invalid Definition');
75
        }
76
77 View Code Duplication
        if (!isset($this->jsonFile[$nameParts[1]][$nameParts[2]])) {
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...
78
            throw new DefinitionNotFoundException("Definition '$name' not found");
79
        }
80
81
        return $this->jsonFile[$nameParts[1]][$nameParts[2]];
82
    }
83
84
    /**
85
     * @param $path
86
     * @param $method
87
     * @return SwaggerRequestBody
88
     * @throws DefinitionNotFoundException
89
     * @throws HttpMethodNotFoundException
90
     * @throws InvalidDefinitionException
91
     * @throws NotMatchedException
92
     * @throws PathNotFoundException
93
     */
94
    public function getRequestParameters($path, $method)
95
    {
96
        $structure = $this->getPathDefinition($path, $method);
97
98
        if (!isset($structure[self::SWAGGER_PARAMETERS])) {
99
            return new SwaggerRequestBody($this, "$method $path", []);
100
        }
101
        return new SwaggerRequestBody($this, "$method $path", $structure[self::SWAGGER_PARAMETERS]);
102
    }
103
104
    /**
105
     * OpenApi 2.0 doesn't describe null values, so this flag defines,
106
     * if match is ok when one of property
107
     *
108
     * @param $value
109
     */
110
    public function setAllowNullValues($value)
111
    {
112
        $this->allowNullValues = (bool) $value;
113
    }
114
}
115