GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( b7663d...658651 )
by Jamie
08:03
created

Parameter::hasLocation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace OpenStack\Common\Api;
4
5
use OpenStack\Common\HydratorStrategyTrait;
6
7
/**
8
 * Represents an individual request parameter in a RESTful operation. A parameter can take on many forms:
9
 * in a URL path, in a URL query, in a JSON body, and in a HTTP header. It is worth documenting brifly each
10
 * variety of parameter:
11
 *
12
 * * Header parameters are those which populate a HTTP header in a request. Header parameters can have
13
 *   aliases; for example, a user-facing name of "Foo" can be sent over the wire as "X-Foo_Bar", as defined
14
 *   by ``sentAs``. Prefixes can also be used.
15
 *
16
 * * Query parameters are those which populate a URL query parameter. The value is therefore usually
17
 *   confined to a string.
18
 *
19
 * * JSON parameters are those which populate a JSON request body. These are the most complex variety
20
 *   of Parameter, since there are so many different ways a JSON document can be constructed. The SDK
21
 *   supports deep-nesting according to a XPath syntax; for more information, see {@see \OpenStack\Common\JsonPath}.
22
 *   Nested object and array properties are also supported since JSON is a recursive data type. What
23
 *   this means is that a Parameter can have an assortment of child Parameters, one for each object
24
 *   property or array element.
25
 *
26
 * * Raw parameters are those which populate a non-JSON request body. This is typically used for
27
 *   uploading payloads (such as Swift object data) to a remote API.
28
 *
29
 * * Path parameters are those which populate a URL path. They are serialized according to URL
30
 *   placeholders.
31
 *
32
 * @package OpenStack\Common\Api
33
 */
34
class Parameter
35
{
36
    use HydratorStrategyTrait;
37
38
    const DEFAULT_LOCATION = 'json';
39
40
    /**
41
     * The human-friendly name of the parameter. This is what the user will input.
42
     *
43
     * @var string
44
     */
45
    private $name;
46
47
    /**
48
     * The alias for this parameter. Although the user will always interact with the human-friendly $name property,
49
     * the $sentAs is what's used over the wire.
50
     *
51
     * @var string
52
     */
53
    private $sentAs;
54
55
    /**
56
     * For array parameters (for example, an array of security group names when creating a server), each array element
57
     * will need to adhere to a common schema. For the aforementioned example, each element will need to be a string.
58
     * For more complicated parameters, you might be validated an array of complicated objects.
59
     *
60
     * @var Parameter
61
     */
62
    private $itemSchema;
63
64
    /**
65
     * For object parameters, each property will need to adhere to a specific schema. For every property in the
66
     * object, it has its own schema - meaning that this property is a hash of name/schema pairs.
67
     *
68
     * The *only* exception to this rule is for metadata parameters, which are arbitrary key/value pairs. Since it does
69
     * not make sense to have a schema for each metadata key, a common schema is use for every one. So instead of this
70
     * property being a hash of schemas, it is a single Parameter object instead. This single Parameter schema will
71
     * then be applied to each metadata key provided.
72
     *
73
     * @var []Parameter|Parameter
74
     */
75
    private $properties;
76
77
    /**
78
     * The value's PHP type which this parameter represents; either "string", "bool", "object", "array", "NULL".
79
     *
80
     * @var string
81
     */
82
    private $type;
83
84
    /**
85
     * Indicates whether this parameter requires a value from the user.
86
     *
87
     * @var bool
88
     */
89
    private $required;
90
91
    /**
92
     * The location in the HTTP request where this parameter will populate; either "header", "url", "query", "raw" or
93
     * "json".
94
     *
95
     * @var string
96
     */
97
    private $location;
98
99
    /**
100
     * Relevant to "json" location parameters only. This property allows for deep nesting through the use of
101
     * {@see OpenStack\Common\JsonPath}.
102
     *
103
     * @var string
104
     */
105
    private $path;
106
107
    /**
108
     * Allows for the prefixing of parameter names.
109
     *
110
     * @var string
111
     */
112
    private $prefix;
113
114
    /**
115
     * @param array $data
116
     */
117 195
    public function __construct(array $data)
118
    {
119 195
        $this->hydrate($data);
120
121 195
        if (!$this->type) {
122 99
            $this->type = 'string';
123 99
        }
124
125 195
        $this->location = $this->location ?: self::DEFAULT_LOCATION;
126 195
        $this->required = (bool)$this->required;
127
128 195
        $this->stockItemSchema($data);
129 195
        $this->stockProperties($data);
130 195
    }
131
132 195
    private function stockItemSchema(array $data)
133
    {
134 195
        if (isset($data['items'])) {
135 25
            $this->itemSchema = new Parameter($data['items']);
136 25
        }
137 195
    }
138
139 195
    private function stockProperties(array $data)
140
    {
141 195
        if (isset($data['properties'])) {
142 39
            if (strpos(strtolower($this->name), 'metadata') !== false) {
143 22
                $this->properties = new Parameter($data['properties']);
144 22
            } else {
145 27
                foreach ($data['properties'] as $name => $property) {
146 27
                    $this->properties[$name] = new Parameter($property + ['name' => $name]);
147 27
                }
148
            }
149 39
        }
150 195
    }
151
152
    /**
153
     * Retrieve the name that will be used over the wire.
154
     *
155
     * @return string
156
     */
157 88
    public function getName()
158
    {
159 88
        return $this->sentAs ?: $this->name;
160
    }
161
162
    /**
163
     * Indicates whether the user must provide a value for this parameter.
164
     *
165
     * @return bool
166
     */
167 4
    public function isRequired()
168
    {
169 4
        return $this->required === true;
170
    }
171
172
    /**
173
     * Validates a given user value and checks whether it passes basic sanity checking, such as types.
174
     *
175
     * @param $userValues The value provided by the user
176
     *
177
     * @return bool       TRUE if the validation passes
178
     * @throws \Exception If validation fails
179
     */
180 9
    public function validate($userValues)
181
    {
182 9
        $this->validateType($userValues);
183
184 8
        if ($this->isArray()) {
185 2
            $this->validateArray($userValues);
186 7
        } elseif ($this->isObject()) {
187 3
            $this->validateObject($userValues);
188 2
        }
189
190 6
        return true;
191
    }
192
193 9
    private function validateType($userValues)
194
    {
195 9
        if (!$this->hasCorrectType($userValues)) {
196 3
            throw new \Exception(sprintf(
197 3
                'The key provided "%s" has the wrong value type. You provided %s but was expecting %s',
198 3
                $this->name, print_r($userValues, true), $this->type
199 3
            ));
200
        }
201 8
    }
202
203 2
    private function validateArray($userValues)
204
    {
205 2
        foreach ($userValues as $userValue) {
206 2
            $this->itemSchema->validate($userValue);
207 1
        }
208 1
    }
209
210 3
    private function validateObject($userValues)
211
    {
212 3
        foreach ($userValues as $key => $userValue) {
213 3
            $property = $this->getNestedProperty($key);
214 2
            $property->validate($userValue);
215 2
        }
216 2
    }
217
218
    /**
219
     * Internal method which retrieves a nested property for object parameters.
220
     *
221
     * @param $key The name of the child parameter
222
     *
223
     * @returns Parameter
224
     * @throws \Exception
225
     */
226 3
    private function getNestedProperty($key)
227
    {
228 3
        if ($this->name == 'metadata' && $this->properties instanceof Parameter) {
229 1
            return $this->properties;
230 2
        } elseif (isset($this->properties[$key])) {
231 1
            return $this->properties[$key];
232
        } else {
233 1
            throw new \Exception(sprintf('The key provided "%s" is not defined', $key));
234
        }
235
    }
236
237
    /**
238
     * Internal method which indicates whether the user value is of the same type as the one expected
239
     * by this parameter.
240
     *
241
     * @param $userValue The value being checked
242
     *
243
     * @return bool
244
     */
245
    private function hasCorrectType($userValue)
246
    {
247
        // Helper fn to see whether an array is associative (i.e. a JSON object)
248 9
        $isAssociative = function ($value) {
249 4
            return is_array($value) && (bool)count(array_filter(array_keys($value), 'is_string'));
250 9
        };
251
252
        // For params defined as objects, we'll let the user get away with
253
        // passing in an associative array - since it's effectively a hash
254 9
        if ($this->type == 'object' && $isAssociative($userValue)) {
255 3
            return true;
256
        }
257
258 8
        return gettype($userValue) == $this->type;
259
    }
260
261
    /**
262
     * Indicates whether this parameter represents an array type
263
     *
264
     * @return bool
265
     */
266 69
    public function isArray()
267
    {
268 69
        return $this->type == 'array' && $this->itemSchema instanceof Parameter;
269
    }
270
271
    /**
272
     * Indicates whether this parameter represents an object type
273
     *
274
     * @return bool
275
     */
276 68
    public function isObject()
277
    {
278 68
        return $this->type == 'object' && !empty($this->properties);
279
    }
280
281 162
    public function getLocation()
282
    {
283 162
        return $this->location;
284
    }
285
286
    /**
287
     * Verifies whether the given location matches the parameter's location.
288
     *
289
     * @param $value
290
     *
291
     * @return bool
292
     */
293 1
    public function hasLocation($value)
294
    {
295 1
        return $this->location == $value;
296
    }
297
298
    /**
299
     * Retrieves the parameter's path.
300
     *
301
     * @return string|null
302
     */
303 62
    public function getPath()
304
    {
305 62
        return $this->path;
306
    }
307
308
    /**
309
     * Retrieves the common schema that an array parameter applies to all its child elements.
310
     *
311
     * @return Parameter
312
     */
313 18
    public function getItemSchema()
314
    {
315 18
        return $this->itemSchema;
316
    }
317
318
    /**
319
     * Sets the name of the parameter to a new value
320
     *
321
     * @param string $name
322
     */
323 13
    public function setName($name)
324
    {
325 13
        $this->name = $name;
326 13
    }
327
328
    /**
329
     * Retrieves the child parameter for an object parameter.
330
     *
331
     * @param string $name The name of the child property
332
     *
333
     * @return null|Parameter
334
     */
335 21
    public function getProperty($name)
336
    {
337 21
        if ($this->properties instanceof Parameter) {
338 12
            $this->properties->setName($name);
339 12
            return $this->properties;
340
        }
341
342 10
        return isset($this->properties[$name]) ? $this->properties[$name] : null;
343
    }
344
345
    /**
346
     * Retrieves the prefix for a parameter, if any.
347
     *
348
     * @return string|null
349
     */
350 15
    public function getPrefix()
351
    {
352 15
        return $this->prefix;
353
    }
354
}