Completed
Pull Request — master (#52)
by John
02:41
created

OperationObject::getParameters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/*
3
 * This file is part of the KleijnWeb\SwaggerBundle package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace KleijnWeb\SwaggerBundle\Document;
10
11
/**
12
 * @author John Kleijn <[email protected]>
13
 */
14
class OperationObject
15
{
16
    /**
17
     * @var SwaggerDocument
18
     */
19
    private $document;
20
21
    /**
22
     * @var object
23
     */
24
    private $definition;
25
26
    /**
27
     * @var string
28
     */
29
    private $path;
30
31
    /**
32
     * @var string
33
     */
34
    private $method;
35
36
    /**
37
     * @param SwaggerDocument $document
38
     * @param string          $path
39
     * @param string          $method
40
     */
41
    public function __construct(SwaggerDocument $document, $path, $method)
42
    {
43
        $paths = $document->getPathDefinitions();
44
45
        if (!property_exists($paths, $path)) {
46
            throw new \InvalidArgumentException("Path '$path' not in Swagger document");
47
        }
48
        $method = strtolower($method);
49
        if (!property_exists($paths->$path, $method)) {
50
            throw new \InvalidArgumentException("Method '$method' not supported for path '$path'");
51
        }
52
53
        $this->document = $document;
54
        $this->path = $path;
55
        $this->method = $method;
56
        $this->definition = $paths->$path->$method;
57
        $this->definition->{'x-request-schema'} = $this->assembleRequestSchema();
58
    }
59
60
    /**
61
     * @param object $definition
62
     * @param string $path
63
     * @param string $method
64
     *
65
     * @return static
66
     */
67
    public static function createFromOperationDefinition($definition, $path = '/', $method = 'GET')
68
    {
69
        $method = strtolower($method);
70
        $documentDefinition = (object)[
71
            'paths' => (object)[
72
                $path => (object)[
73
                    $method => $definition
74
                ]
75
            ]
76
        ];
77
        $document = new SwaggerDocument('', $documentDefinition);
78
79
        return new static($document, $path, $method);
80
    }
81
82
    /**
83
     * @return object
84
     */
85
    public function getRequestSchema()
86
    {
87
        return $this->definition->{'x-request-schema'};
88
    }
89
90
    /**
91
     * @return bool
92
     */
93
    public function hasParameters()
94
    {
95
        return property_exists($this->definition, 'parameters');
96
    }
97
98
    /**
99
     * @return object
100
     */
101
    public function getParameters()
102
    {
103
        return $this->definition->parameters;
104
    }
105
106
    /**
107
     * @return string
108
     */
109
    public function getPath()
110
    {
111
        return $this->path;
112
    }
113
114
    /**
115
     * @return string
116
     */
117
    public function getMethod()
118
    {
119
        return $this->method;
120
    }
121
122
    /**
123
     * @return object
124
     */
125
    public function getDefinition()
126
    {
127
        return $this->definition;
128
    }
129
130
    /**
131
     * @param string $parameterName
132
     *
133
     * @return string
134
     */
135 View Code Duplication
    public function createParameterPointer($parameterName)
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...
136
    {
137
        foreach ($this->definition->parameters as $i => $paramDefinition) {
138
            if ($paramDefinition->name === $parameterName) {
139
                return '/' . implode('/', [
140
                    'paths',
141
                    str_replace(['~', '/'], ['~0', '~1'], $this->getPath()),
142
                    $this->getMethod(),
143
                    'parameters',
144
                    $i
145
                ]);
146
            }
147
        }
148
        throw new \InvalidArgumentException("Parameter '$parameterName' not in document");
149
    }
150
151
    /**
152
     * @param string $parameterName
153
     *
154
     * @return string
155
     */
156 View Code Duplication
    public function createParameterSchemaPointer($parameterName)
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...
157
    {
158
        foreach ($this->definition->{'x-request-schema'}->properties as $propertyName => $schema) {
159
            if ($propertyName === $parameterName) {
160
                return '/' . implode('/', [
161
                    'paths',
162
                    str_replace(['~', '/'], ['~0', '~1'], $this->getPath()),
163
                    $this->getMethod(),
164
                    'x-request-schema',
165
                    'properties',
166
                    $propertyName
167
                ]);
168
            }
169
        }
170
        throw new \InvalidArgumentException("Parameter '$parameterName' not in document");
171
    }
172
173
    /**
174
     * @return object
175
     */
176
    private function assembleRequestSchema()
177
    {
178
        if (!isset($this->definition->parameters)) {
179
            return new \stdClass;
180
        }
181
        $schema = new \stdClass;
182
        $schema->type = 'object';
183
        $schema->required = [];
184
        $schema->properties = new \stdClass;
185
186
        foreach ($this->definition->parameters as $paramDefinition) {
187
            if (isset($paramDefinition->required) && $paramDefinition->required) {
188
                $schema->required[] = $paramDefinition->name;
189
            }
190
            if ($paramDefinition->in === 'body') {
191
                $schema->properties->{$paramDefinition->name} = property_exists($paramDefinition, 'schema')
192
                    ? $paramDefinition->schema
193
                    : (object)['type' => 'object'];
194
                continue;
195
            }
196
197
            $type = property_exists($paramDefinition, 'type') ? $paramDefinition->type : 'string';
198
            $propertyDefinition = $schema->properties->{$paramDefinition->name} = (object)['type' => $type];
199
            if (property_exists($paramDefinition, 'format')) {
200
                $propertyDefinition->format = $paramDefinition->format;
201
            }
202
            if (property_exists($paramDefinition, 'items')) {
203
                $propertyDefinition->items = $paramDefinition->items;
204
            }
205
        }
206
207
        return $schema;
208
    }
209
}
210