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

SwaggerSchema::validateArguments()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 10

Duplication

Lines 5
Ratio 50 %

Importance

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