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 ( 658651...354c7c )
by Jamie
07:31
created

Operation::getMethod()   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

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace OpenStack\Common\Api;
4
5
use GuzzleHttp\Exception\RequestException;
6
use GuzzleHttp\Utils;
7
8
/**
9
 * This class represents an OpenStack API operation. It encapsulates most aspects of the REST operation: its HTTP
10
 * method, the URL path, its top-level JSON key, and all of its {@see Parameter} objects.
11
 *
12
 * An operation not only represents a remote operation, but it also provides the mechanism for executing it
13
 * over HTTP. To do this, it uses a {@see ClientInterface} that allows a {@see GuzzleHttp\Message\Request}
14
 * to be created from the user values provided. Once this request is assembled, it is then sent to the
15
 * remote API and the response is returned to whoever first invoked the Operation class.
16
 *
17
 * @package OpenStack\Common\Api
18
 */
19
class Operation
20
{
21
    /** @var string The HTTP method */
22
    private $method;
23
24
    /** @var string The URL path */
25
    private $path;
26
27
    /** @var string The top-level JSON key */
28
    private $jsonKey;
29
30
    /** @var []Parameter The parameters of this operation */
31
    private $params;
32
33
    /**
34
     * @param array $definition The data definition (in array form) that will populate this
35
     *                          operation. Usually this is retrieved from an {@see ApiInterface}
36
     *                          object method.
37
     */
38 184
    public function __construct(array $definition)
39
    {
40 184
        $this->method = $definition['method'];
41 184
        $this->path = $definition['path'];
42
43 184
        if (isset($definition['jsonKey'])) {
44 46
            $this->jsonKey = $definition['jsonKey'];
45 46
        }
46
47 184
        $this->params = self::toParamArray($definition['params']);
48 184
    }
49
50
    /**
51
     * @return string
52
     */
53 176
    public function getPath()
54
    {
55 176
        return $this->path;
56
    }
57
58
    /**
59
     * @return string
60
     */
61 176
    public function getMethod()
62
    {
63 176
        return $this->method;
64
    }
65
66
    /**
67
     * Indicates whether this operation supports a parameter.
68
     *
69
     * @param $key The name of a parameter
70
     *
71
     * @return bool
72
     */
73 1
    public function hasParam($key)
74
    {
75 1
        return isset($this->params[$key]);
76
    }
77
78
    /**
79
     * @param $name
80
     *
81
     * @return Parameter
82
     */
83 163
    public function getParam($name)
84
    {
85 163
        return isset($this->params[$name]) ? $this->params[$name] : null;
86
    }
87
88
    /**
89
     * @return string
90
     */
91 62
    public function getJsonKey()
92
    {
93 62
        return $this->jsonKey;
94
    }
95
96
    /**
97
     * A convenience method that will take a generic array of data and convert it into an array of
98
     * {@see Parameter} objects.
99
     *
100
     * @param array $data A generic data array
101
     *
102
     * @return array
103
     */
104 184
    public static function toParamArray(array $data)
105
    {
106 184
        $params = [];
107
108 184
        foreach ($data as $name => $param) {
109 178
            $params[$name] = new Parameter($param + ['name' => $name]);
110 184
        }
111
112 184
        return $params;
113
    }
114
115
    /**
116
     * This method will validate all of the user-provided values and throw an exception if any
117
     * failures are detected. This is useful for basic sanity-checking before a request is
118
     * serialized and sent to the API.
119
     *
120
     * @param array $userValues The user-defined values
121
     *
122
     * @return bool       TRUE if validation passes
123
     * @throws \Exception If validate fails
124
     */
125 180
    public function validate(array $userValues)
126
    {
127 180
        $this->checkDisallowedKeys($userValues);
128
129 179
        foreach ($this->params as $paramName => $param) {
130 174
            if (array_key_exists($paramName, $userValues)) {
131 164
                $param->validate($userValues[$paramName]);
132 174
            } elseif ($param->isRequired()) {
133 1
                throw new \Exception(sprintf('"%s" is a required option, but it was not provided', $paramName));
134
            }
135 179
        }
136
137 177
        return true;
138
    }
139
140 180
    private function checkDisallowedKeys(array $userValues)
141
    {
142 180
        if (!empty($disallowedKeys = array_keys(array_diff_key($userValues, $this->params)))) {
143 1
            throw new \Exception(sprintf(
144 1
                'The following keys are not supported: %s', implode($disallowedKeys, ', ')
145 1
            ));
146
        }
147 179
    }
148
}
149