Completed
Push — master ( 805a89...b85748 )
by John
02:52
created

OperationObject::resolvePointerRecursively()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 3
eloc 8
nc 3
nop 3
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 array
100
     */
101
    public function getParameters()
102
    {
103
        return $this->hasParameters() ? $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
    public function createParameterPointer($parameterName)
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
    public function createParameterSchemaPointer($parameterName)
157
    {
158
        $segments = explode('.', $parameterName);
159
160
        $pointer = '/'
161
            . implode(
162
                '/',
163
                [
164
                    'paths',
165
                    str_replace(['~', '/'], ['~0', '~1'], $this->getPath()),
166
                    $this->getMethod(),
167
                    'x-request-schema',
168
                    'properties'
169
                ]
170
            );
171
172
173
        return self::resolvePointerRecursively(
174
            $pointer,
175
            $segments,
176
            $this->definition->{'x-request-schema'}->properties
177
        );
178
    }
179
180
    /**
181
     * @param string $pointer
182
     * @param array  $segments
183
     * @param object $context
184
     *
185
     * @return mixed
186
     */
187
    public static function resolvePointerRecursively($pointer, array $segments, $context)
188
    {
189
        $segment = str_replace(['~0', '~1'], ['~', '/'], array_shift($segments));
190
        if (property_exists($context, $segment)) {
191
            $pointer .= '/' . $segment;
192
            if (!count($segments)) {
193
                return $pointer;
194
            }
195
196
            return self::resolvePointerRecursively($pointer, $segments, $context->$segment);
197
        }
198
199
        throw new \InvalidArgumentException("Segment '$segment' not found in context '$pointer'");
200
    }
201
202
    /**
203
     * @return object
204
     */
205
    private function assembleRequestSchema()
206
    {
207
        if (!isset($this->definition->parameters)) {
208
            return new \stdClass;
209
        }
210
        $schema             = new \stdClass;
211
        $schema->type       = 'object';
212
        $schema->required   = [];
213
        $schema->properties = new \stdClass;
214
215
        foreach ($this->definition->parameters as $paramDefinition) {
216
            $isRequired = isset($paramDefinition->required) && $paramDefinition->required;
217
            if ($isRequired) {
218
                $schema->required[] = $paramDefinition->name;
219
            }
220
            if ($paramDefinition->in === 'body') {
221
                $schema->properties->{$paramDefinition->name}
222
                    = $bodySchema = property_exists($paramDefinition, 'schema')
0 ignored issues
show
Unused Code introduced by
$bodySchema is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
223
                    ? $paramDefinition->schema
224
                    : (object)['type' => 'object'];
225
                continue;
226
            }
227
228
            $type               = property_exists($paramDefinition, 'type') ? $paramDefinition->type : 'string';
229
            $propertyDefinition = $schema->properties->{$paramDefinition->name} = (object)['type' => $type];
230
            if (property_exists($paramDefinition, 'format')) {
231
                $propertyDefinition->format = $paramDefinition->format;
232
            }
233
            if (property_exists($paramDefinition, 'items')) {
234
                $propertyDefinition->items = $paramDefinition->items;
235
            }
236
        }
237
238
        return $schema;
239
    }
240
}
241