Completed
Pull Request — master (#167)
by
unknown
04:09 queued 02:30
created

Operation::getDocumentationUrl()   A

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 13
    public function __construct(array $config = [], DescriptionInterface $description = null)
53
    {
54
        static $defaults = [
55
            'name' => '',
56
            'httpMethod' => 'GET',
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 13
        ];
68
69 13
        $this->description = $description === null ? new Description([]) : $description;
70
71 13
        if (isset($config['extends'])) {
72 1
            $config = $this->resolveExtends($config['extends'], $config);
73 1
        }
74
75 13
        if (array_key_exists('httpMethod', $config) && empty($config['httpMethod'])) {
76 1
          throw new \InvalidArgumentException('httpMethod must be a non-empty string');
77
        }
78
79 12
        $this->config = $config + $defaults;
80
81
        // Account for the old style of using responseClass
82 12
        if (isset($config['responseClass'])) {
83
            $this->config['responseModel'] = $config['responseClass'];
84
        }
85
86 12
        $this->resolveParameters();
87 11
    }
88
89
    /**
90
     * @return array
91
     */
92 1
    public function toArray()
93
    {
94 1
        return $this->config;
95
    }
96
97
    /**
98
     * Get the service description that the operation belongs to
99
     *
100
     * @return Description
101
     */
102 1
    public function getServiceDescription()
103
    {
104 1
        return $this->description;
105
    }
106
107
    /**
108
     * Get the params of the operation
109
     *
110
     * @return Parameter[]
111
     */
112 1
    public function getParams()
113
    {
114 1
        return $this->parameters;
115
    }
116
117
    /**
118
     * Get additionalParameters of the operation
119
     *
120
     * @return Parameter|null
121
     */
122 1
    public function getAdditionalParameters()
123
    {
124 1
        return $this->additionalParameters;
125
    }
126
127
    /**
128
     * Check if the operation has a specific parameter by name
129
     *
130
     * @param string $name Name of the param
131
     *
132
     * @return bool
133
     */
134 2
    public function hasParam($name)
135
    {
136 2
        return isset($this->parameters[$name]);
137
    }
138
139
    /**
140
     * Get a single parameter of the operation
141
     *
142
     * @param string $name Parameter to retrieve by name
143
     *
144
     * @return Parameter|null
145
     */
146 3
    public function getParam($name)
147
    {
148 3
        return isset($this->parameters[$name])
149 3
            ? $this->parameters[$name]
150 3
            : null;
151
    }
152
153
    /**
154
     * Get the HTTP method of the operation
155
     *
156
     * @return string|null
157
     */
158 4
    public function getHttpMethod()
159
    {
160 4
        return $this->config['httpMethod'];
161
    }
162
163
    /**
164
     * Get the name of the operation
165
     *
166
     * @return string|null
167
     */
168 1
    public function getName()
169
    {
170 1
        return $this->config['name'];
171
    }
172
173
    /**
174
     * Get a short summary of what the operation does
175
     *
176
     * @return string|null
177
     */
178 2
    public function getSummary()
179
    {
180 2
        return $this->config['summary'];
181
    }
182
183
    /**
184
     * Get a longer text field to explain the behavior of the operation
185
     *
186
     * @return string|null
187
     */
188 1
    public function getNotes()
189
    {
190 1
        return $this->config['notes'];
191
    }
192
193
    /**
194
     * Get the documentation URL of the operation
195
     *
196
     * @return string|null
197
     */
198 1
    public function getDocumentationUrl()
199
    {
200 1
        return $this->config['documentationUrl'];
201
    }
202
203
    /**
204
     * Get the name of the model used for processing the response.
205
     *
206
     * @return string
207
     */
208 1
    public function getResponseModel()
209
    {
210 1
        return $this->config['responseModel'];
211
    }
212
213
    /**
214
     * Get whether or not the operation is deprecated
215
     *
216
     * @return bool
217
     */
218 1
    public function getDeprecated()
219
    {
220 1
        return $this->config['deprecated'];
221
    }
222
223
    /**
224
     * Get the URI that will be merged into the generated request
225
     *
226
     * @return string
227
     */
228 1
    public function getUri()
229
    {
230 1
        return $this->config['uri'];
231
    }
232
233
    /**
234
     * Get the errors that could be encountered when executing the operation
235
     *
236
     * @return array
237
     */
238 1
    public function getErrorResponses()
239
    {
240 1
        return $this->config['errorResponses'];
241
    }
242
243
    /**
244
     * Get extra data from the operation
245
     *
246
     * @param string $name Name of the data point to retrieve or null to
247
     *     retrieve all of the extra data.
248
     *
249
     * @return mixed|null
250
     */
251 1
    public function getData($name = null)
252
    {
253 1
        if ($name === null) {
254 1
            return $this->config['data'];
255 1
        } elseif (isset($this->config['data'][$name])) {
256 1
            return $this->config['data'][$name];
257
        } else {
258 1
            return null;
259
        }
260
    }
261
262
    /**
263
     * @param $name
264
     * @param array $config
265
     * @return array
266
     */
267 1
    private function resolveExtends($name, array $config)
268
    {
269 1
        if (!$this->description->hasOperation($name)) {
270
            throw new \InvalidArgumentException('No operation named ' . $name);
271
        }
272
273
        // Merge parameters together one level deep
274 1
        $base = $this->description->getOperation($name)->toArray();
275 1
        $result = $config + $base;
276
277 1
        if (isset($base['parameters']) && isset($config['parameters'])) {
278 1
            $result['parameters'] = $config['parameters'] + $base['parameters'];
279 1
        }
280
281 1
        return $result;
282
    }
283
284
    /**
285
     * Process the description and extract the parameter config
286
     *
287
     * @return void
288
     */
289 12
    private function resolveParameters()
290
    {
291
        // Parameters need special handling when adding
292 12
        foreach ($this->config['parameters'] as $name => $param) {
293 7
            if (!is_array($param)) {
294 1
                throw new \InvalidArgumentException(
295 1
                    "Parameters must be arrays, {$this->config['name']}.$name is ".gettype($param)
296 1
                );
297
            }
298 6
            $param['name'] = $name;
299 6
            $this->parameters[$name] = new Parameter(
300 6
                $param,
301 6
                ['description' => $this->description]
302 6
            );
303 11
        }
304
305 11
        if ($this->config['additionalParameters']) {
306 1
            if (is_array($this->config['additionalParameters'])) {
307 1
                $this->additionalParameters = new Parameter(
308 1
                    $this->config['additionalParameters'],
309 1
                    ['description' => $this->description]
310 1
                );
311 1
            } else {
312
                $this->additionalParameters = $this->config['additionalParameters'];
313
            }
314 1
        }
315 11
    }
316
}
317