Completed
Pull Request — master (#55)
by
unknown
06:50
created

AbstractApi::postRaw()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 3
crap 2
1
<?php
2
3
namespace Trello\Api;
4
5
use Trello\Client;
6
use Trello\HttpClient\Message\ResponseMediator;
7
use Trello\Exception\InvalidArgumentException;
8
use Trello\Exception\BadMethodCallException;
9
use Trello\Exception\MissingArgumentException;
10
use \DateTime;
11
12
/**
13
 * Abstract class for Api classes
14
 *
15
 * @author Christian Daguerre <[email protected]>
16
 * @author Joseph Bielawski <[email protected]>
17
 */
18
abstract class AbstractApi implements ApiInterface
19
{
20
    /**
21
     * API sub namespace
22
     *
23
     * @var string
24
     */
25
    protected $path;
26
27
    /**
28
     * The client
29
     *
30
     * @var Client
31
     */
32
    protected $client;
33
34
    /**
35
     * @var array
36
     */
37
    public static $fields;
38
39
    /**
40
     * @param Client $client
41
     */
42 334
    public function __construct(Client $client)
43
    {
44 334
        $this->client = $client;
45 334
    }
46
47
    /**
48
     * Catches any undefined "get{$field}" calls, and passes them
49
     * to the getField() if the $field is in the $this->fields property
50
     *
51
     * @param string $method    called method
52
     * @param array  $arguments array of arguments passed to called method
53
     *
54
     * @return array
55
     *
56
     * @throws BadMethodCallException If the method does not start with "get"
57
     *                                or the field is not included in the $fields property
58
     */
59
    public function __call($method, $arguments)
60
    {
61
        if (isset($this->fields) && substr($method, 0, 3) === 'get') {
62
            $property = lcfirst(substr($method, 3));
63
            if (in_array($property, $this->fields) && count($arguments) === 2) {
64
                return $this->getField($arguments[0], $arguments[1]);
65
            }
66
        }
67
68
        throw new BadMethodCallException(sprintf(
69
            'There is no method named "%s" in class "%s".',
70
            $method,
71
            get_called_class()
72
        ));
73
    }
74
75
    /**
76
     * Get field names (properties)
77
     *
78
     * @return array array of fields
79
     */
80
    public function getFields()
81
    {
82
        return static::$fields;
83
    }
84
85
    /**
86
     * Get a field value by field name
87
     *
88
     * @param string $id    the board's id
89
     * @param string $field the field
90
     *
91
     * @return mixed field value
92
     *
93
     * @throws InvalidArgumentException If the field does not exist
94
     */
95 5
    public function getField($id, $field)
96
    {
97 5
        if (!in_array($field, static::$fields)) {
98 2
            throw new InvalidArgumentException(sprintf('There is no field named %s.', $field));
99
        }
100
101 3
        $response = $this->get($this->path.'/'.rawurlencode($id).'/'.rawurlencode($field));
102
103 3
        return isset($response['_value']) ? $response['_value'] : $response;
104
    }
105
106
    /**
107
     * Send a GET request with query parameters.
108
     *
109
     * @param string $path           Request path.
110
     * @param array  $parameters     GET parameters.
111
     * @param array  $requestHeaders Request Headers.
112
     *
113
     * @return \Guzzle\Http\EntityBodyInterface|mixed|string
114
     */
115
    protected function get($path, array $parameters = array(), $requestHeaders = array())
116
    {
117
        $response = $this->client->getHttpClient()->get($path, $parameters, $requestHeaders);
118
119
        return ResponseMediator::getContent($response);
120
    }
121
122
    /**
123
     * Send a HEAD request with query parameters
124
     *
125
     * @param string $path           Request path.
126
     * @param array  $parameters     HEAD parameters.
127
     * @param array  $requestHeaders Request headers.
128
     *
129
     * @return \Guzzle\Http\Message\Response
130
     */
131
    protected function head($path, array $parameters = array(), $requestHeaders = array())
132
    {
133
        $response = $this->client->getHttpClient()->request($path, null, 'HEAD', $requestHeaders, array(
134
            'query' => $parameters,
135
        ));
136
137
        return $response;
138
    }
139
140
    /**
141
     * Send a POST request with JSON-encoded parameters.
142
     *
143
     * @param string $path           Request path.
144
     * @param array  $parameters     POST parameters to be JSON encoded.
145
     * @param array  $requestHeaders Request headers.
146
     *
147
     * @return mixed
148
     */
149
    protected function post($path, array $parameters = array(), $requestHeaders = array())
150
    {
151
        return $this->postRaw(
152
            $path,
153
            $this->createParametersBody($parameters),
154
            $requestHeaders
155
        );
156
    }
157
158
    /**
159
     * Send a POST request with raw data.
160
     *
161
     * @param string $path           Request path.
162
     * @param mixed  $body           Request body.
163
     * @param array  $requestHeaders Request headers.
164
     *
165
     * @return \Guzzle\Http\EntityBodyInterface|mixed|string
166
     */
167
    protected function postRaw($path, $body, $requestHeaders = array())
168
    {
169
        $response = $this->client->getHttpClient()->post(
170
            $path,
171
            $body,
172
            $requestHeaders
173
        );
174
175
        return ResponseMediator::getContent($response);
176
    }
177
178
    /**
179
     * Send a PATCH request with JSON-encoded parameters.
180
     *
181
     * @param string $path           Request path.
182
     * @param array  $parameters     POST parameters to be JSON encoded.
183
     * @param array  $requestHeaders Request headers.
184
     *
185
     * @return mixed
186
     */
187 View Code Duplication
    protected function patch($path, array $parameters = array(), $requestHeaders = array())
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
188
    {
189
        $response = $this->client->getHttpClient()->patch(
190
            $path,
191
            $this->createParametersBody($parameters),
192
            $requestHeaders
193
        );
194
195
        return ResponseMediator::getContent($response);
196
    }
197
198
    /**
199
     * Send a PUT request with JSON-encoded parameters.
200
     *
201
     * @param string $path           Request path.
202
     * @param array  $parameters     POST parameters to be JSON encoded.
203
     * @param array  $requestHeaders Request headers.
204
     *
205
     * @return mixed
206
     */
207
    protected function put($path, array $parameters = array(), $requestHeaders = array())
208
    {
209
        foreach ($parameters as $name => $parameter) {
210
            if (is_bool($parameter)) {
211
                $parameters[$name] = $parameter ? 'true' : 'false';
212
            }
213
        }
214
215
        $response = $this->client->getHttpClient()->put(
216
            $path,
217
            $this->createParametersBody($parameters),
218
            $requestHeaders
219
        );
220
221
        return ResponseMediator::getContent($response);
222
    }
223
224
    /**
225
     * Send a DELETE request with JSON-encoded parameters.
226
     *
227
     * @param string $path           Request path.
228
     * @param array  $parameters     POST parameters to be JSON encoded.
229
     * @param array  $requestHeaders Request headers.
230
     *
231
     * @return mixed
232
     */
233 View Code Duplication
    protected function delete($path, array $parameters = array(), $requestHeaders = array())
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
234
    {
235
        $response = $this->client->getHttpClient()->delete(
236
            $path,
237
            $this->createParametersBody($parameters),
238
            $requestHeaders
239
        );
240
241
        return ResponseMediator::getContent($response);
242
    }
243
244
    /**
245
     * Prepare request parameters.
246
     *
247
     * @param array $parameters Request parameters
248
     *
249
     * @return null|string
250
     */
251
    protected function createParametersBody(array $parameters)
252
    {
253
        foreach ($parameters as $name => $parameter) {
254
            if (is_bool($parameter)) {
255
                $parameters[$name] = $parameter ? 'true' : 'false';
256
            } elseif (is_array($parameter)) {
257
                $commaSeparatedParameters = '';
258
                foreach ($parameter as $subName => $subParameter) {
259
                    if (is_bool($subParameter)) {
260
                        $subParameter = $subParameter ? 'true' : 'false';
261
                    }
262
                    if($commaSeparatedParameters != ''){
263
                        $commaSeparatedParameters .= ',';
264
                    }
265
                    $commaSeparatedParameters .= $subParameter;
266
                }
267
                $parameters[$name] = $commaSeparatedParameters;
268
            } elseif ($parameter instanceof DateTime) {
269
                $parameters[$name] = $parameter->format($parameter::ATOM);
270
            }
271
        }
272 191
273
        return $parameters;
274 191
    }
275 91
276
    protected function getPath($id = null)
277
    {
278 100
        if ($id) {
279
            return preg_replace('/\#id\#/', $id, $this->path);
280
        }
281
282
        return $this->path;
283
    }
284
285
    /**
286
     * Validate parameters array
287
     *
288
     * @param string[] $required required properties (array keys)
289 30
     * @param array $params   array to check for existence of the required keys
290
     *
291 30
     * @throws MissingArgumentException if a required parameter is missing
292 30
     */
293 18 View Code Duplication
    protected function validateRequiredParameters(array $required, array $params)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
294
    {
295 15
        foreach ($required as $param) {
296 12
            if (!isset($params[$param])) {
297
                throw new MissingArgumentException(sprintf('The "%s" parameter is required.', $param));
298
            }
299
        }
300
    }
301
302
    /**
303
     * Validate allowed parameters array
304
     * Checks whether the passed parameters are allowed
305
     *
306
     * @param string[]        $allowed allowed properties
307
     * @param array|string $params  array to check
308
     * @param string $paramName
309
     *
310 70
     * @return array array of validated parameters
311
     *
312 70
     * @throws InvalidArgumentException if a parameter is not allowed
313 61
     */
314 61
    protected function validateAllowedParameters(array $allowed, $params, $paramName)
315
    {
316 70
        if (!is_array($params)) {
317 70
            $params = array($params);
318 21
        }
319 21
320 21
        foreach ($params as $param) {
321 21
            if (!in_array($param, $allowed)) {
322
                throw new InvalidArgumentException(sprintf(
323 21
                    'The "%s" parameter may contain only values within "%s". "%s" given.',
324
                    $paramName,
325 49
                    implode(", ", $allowed),
326
                    $param
327 49
                ));
328
            }
329
        }
330
331
        return $params;
332
    }
333
334
    /**
335
     * Validate that the params array includes at least one of
336
     * the keys in a given array
337
     *
338
     * @param string[] $atLeastOneOf allowed properties
339
     * @param array $params       array to check
340
     *
341 6
     * @return boolean
342
     *
343 6
     * @throws MissingArgumentException
344 6
     */
345 3 View Code Duplication
    protected function validateAtLeastOneOf(array $atLeastOneOf, array $params)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
346
    {
347 4
        foreach ($atLeastOneOf as $param) {
348
            if (isset($params[$param])) {
349 3
                return true;
350 3
            }
351 3
        }
352 3
353
        throw new MissingArgumentException(sprintf(
354
            'You need to provide at least one of the following parameters "%s".',
355
            implode('", "', $atLeastOneOf)
356
        ));
357
    }
358
}
359