Operation::getName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
namespace GuzzleHttp\Command\Guzzle;
3
4
use GuzzleHttp\Command\ToArrayInterface;
5
6
/**
7
 * Guzzle operation
8
 */
9
class Operation implements ToArrayInterface
10
{
11
    /** @var array Parameters */
12
    private $parameters = [];
13
14
    /** @var Parameter Additional parameters schema */
15
    private $additionalParameters;
16
17
    /** @var DescriptionInterface */
18
    private $description;
19
20
    /** @var array Config data */
21
    private $config;
22
23
    /**
24
     * Builds an Operation object using an array of configuration data.
25
     *
26
     * - name: (string) Name of the command
27
     * - httpMethod: (string) HTTP method of the operation
28
     * - uri: (string) URI template that can create a relative or absolute URL
29
     * - parameters: (array) Associative array of parameters for the command.
30
     *   Each value must be an array that is used to create {@see Parameter}
31
     *   objects.
32
     * - summary: (string) This is a short summary of what the operation does
33
     * - notes: (string) A longer description of the operation.
34
     * - documentationUrl: (string) Reference URL providing more information
35
     *   about the operation.
36
     * - responseModel: (string) The model name used for processing response.
37
     * - deprecated: (bool) Set to true if this is a deprecated command
38
     * - errorResponses: (array) Errors that could occur when executing the
39
     *   command. Array of hashes, each with a 'code' (the HTTP response code),
40
     *   'phrase' (response reason phrase or description of the error), and
41
     *   'class' (a custom exception class that would be thrown if the error is
42
     *   encountered).
43
     * - data: (array) Any extra data that might be used to help build or
44
     *   serialize the operation
45
     * - additionalParameters: (null|array) Parameter schema to use when an
46
     *   option is passed to the operation that is not in the schema
47
     *
48
     * @param array                 $config      Array of configuration data
49
     * @param DescriptionInterface  $description Service description used to resolve models if $ref tags are found
50
     * @throws \InvalidArgumentException
51
     */
52 10
    public function __construct(array $config = [], DescriptionInterface $description = null)
53
    {
54
        static $defaults = [
55
            'name' => '',
56
            'httpMethod' => '',
57
            'uri' => '',
58
            'responseModel' => null,
59
            'notes' => '',
60
            'summary' => '',
61
            'documentationUrl' => null,
62
            'deprecated' => false,
63
            'data' => [],
64
            'parameters' => [],
65
            'additionalParameters' => null,
66
            'errorResponses' => []
67 10
        ];
68
69 10
        $this->description = $description === null ? new Description([]) : $description;
70
71 10
        if (isset($config['extends'])) {
72 1
            $config = $this->resolveExtends($config['extends'], $config);
73 1
        }
74
75 10
        $this->config = $config + $defaults;
76
77
        // Account for the old style of using responseClass
78 10
        if (isset($config['responseClass'])) {
79
            $this->config['responseModel'] = $config['responseClass'];
80
        }
81
82 10
        $this->resolveParameters();
83 9
    }
84
85
    /**
86
     * @return array
87
     */
88 1
    public function toArray()
89
    {
90 1
        return $this->config;
91
    }
92
93
    /**
94
     * Get the service description that the operation belongs to
95
     *
96
     * @return Description
97
     */
98 1
    public function getServiceDescription()
99
    {
100 1
        return $this->description;
101
    }
102
103
    /**
104
     * Get the params of the operation
105
     *
106
     * @return Parameter[]
107
     */
108 1
    public function getParams()
109
    {
110 1
        return $this->parameters;
111
    }
112
113
    /**
114
     * Get additionalParameters of the operation
115
     *
116
     * @return Parameter|null
117
     */
118 1
    public function getAdditionalParameters()
119
    {
120 1
        return $this->additionalParameters;
121
    }
122
123
    /**
124
     * Check if the operation has a specific parameter by name
125
     *
126
     * @param string $name Name of the param
127
     *
128
     * @return bool
129
     */
130 2
    public function hasParam($name)
131
    {
132 2
        return isset($this->parameters[$name]);
133
    }
134
135
    /**
136
     * Get a single parameter of the operation
137
     *
138
     * @param string $name Parameter to retrieve by name
139
     *
140
     * @return Parameter|null
141
     */
142 3
    public function getParam($name)
143
    {
144 3
        return isset($this->parameters[$name])
145 3
            ? $this->parameters[$name]
146 3
            : null;
147
    }
148
149
    /**
150
     * Get the HTTP method of the operation
151
     *
152
     * @return string|null
153
     */
154 1
    public function getHttpMethod()
155
    {
156 1
        return $this->config['httpMethod'];
157
    }
158
159
    /**
160
     * Get the name of the operation
161
     *
162
     * @return string|null
163
     */
164 1
    public function getName()
165
    {
166 1
        return $this->config['name'];
167
    }
168
169
    /**
170
     * Get a short summary of what the operation does
171
     *
172
     * @return string|null
173
     */
174 2
    public function getSummary()
175
    {
176 2
        return $this->config['summary'];
177
    }
178
179
    /**
180
     * Get a longer text field to explain the behavior of the operation
181
     *
182
     * @return string|null
183
     */
184 1
    public function getNotes()
185
    {
186 1
        return $this->config['notes'];
187
    }
188
189
    /**
190
     * Get the documentation URL of the operation
191
     *
192
     * @return string|null
193
     */
194 1
    public function getDocumentationUrl()
195
    {
196 1
        return $this->config['documentationUrl'];
197
    }
198
199
    /**
200
     * Get the name of the model used for processing the response.
201
     *
202
     * @return string
203
     */
204 1
    public function getResponseModel()
205
    {
206 1
        return $this->config['responseModel'];
207
    }
208
209
    /**
210
     * Get whether or not the operation is deprecated
211
     *
212
     * @return bool
213
     */
214 1
    public function getDeprecated()
215
    {
216 1
        return $this->config['deprecated'];
217
    }
218
219
    /**
220
     * Get the URI that will be merged into the generated request
221
     *
222
     * @return string
223
     */
224 1
    public function getUri()
225
    {
226 1
        return $this->config['uri'];
227
    }
228
229
    /**
230
     * Get the errors that could be encountered when executing the operation
231
     *
232
     * @return array
233
     */
234 1
    public function getErrorResponses()
235
    {
236 1
        return $this->config['errorResponses'];
237
    }
238
239
    /**
240
     * Get extra data from the operation
241
     *
242
     * @param string $name Name of the data point to retrieve or null to
243
     *     retrieve all of the extra data.
244
     *
245
     * @return mixed|null
246
     */
247 1
    public function getData($name = null)
248
    {
249 1
        if ($name === null) {
250 1
            return $this->config['data'];
251 1
        } elseif (isset($this->config['data'][$name])) {
252 1
            return $this->config['data'][$name];
253
        } else {
254 1
            return null;
255
        }
256
    }
257
258
    /**
259
     * @param $name
260
     * @param array $config
261
     * @return array
262
     */
263 1
    private function resolveExtends($name, array $config)
264
    {
265 1
        if (!$this->description->hasOperation($name)) {
266
            throw new \InvalidArgumentException('No operation named ' . $name);
267
        }
268
269
        // Merge parameters together one level deep
270 1
        $base = $this->description->getOperation($name)->toArray();
271 1
        $result = $config + $base;
272
273 1
        if (isset($base['parameters']) && isset($config['parameters'])) {
274 1
            $result['parameters'] = $config['parameters'] + $base['parameters'];
275 1
        }
276
277 1
        return $result;
278
    }
279
280
    /**
281
     * Process the description and extract the parameter config
282
     *
283
     * @return void
284
     */
285 10
    private function resolveParameters()
286
    {
287
        // Parameters need special handling when adding
288 10
        foreach ($this->config['parameters'] as $name => $param) {
289 7
            if (!is_array($param)) {
290 1
                throw new \InvalidArgumentException(
291 1
                    "Parameters must be arrays, {$this->config['name']}.$name is ".gettype($param)
292 1
                );
293
            }
294 6
            $param['name'] = $name;
295 6
            $this->parameters[$name] = new Parameter(
296 6
                $param,
297 6
                ['description' => $this->description]
298 6
            );
299 9
        }
300
301 9
        if ($this->config['additionalParameters']) {
302 1
            if (is_array($this->config['additionalParameters'])) {
303 1
                $this->additionalParameters = new Parameter(
304 1
                    $this->config['additionalParameters'],
305 1
                    ['description' => $this->description]
306 1
                );
307 1
            } else {
308
                $this->additionalParameters = $this->config['additionalParameters'];
309
            }
310 1
        }
311 9
    }
312
}
313