Completed
Push — v2 ( f96eb8...12e20b )
by Beñat
02:07
created

AnswerApi::undoDownvote()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 11
rs 9.4286
cc 3
eloc 6
nc 3
nop 3
1
<?php
2
3
/*
4
 * This file is part of the Stack Exchange Api Client library.
5
 *
6
 * Copyright (c) 2014-2016 Beñat Espiña <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace BenatEspina\StackExchangeApiClient\Api;
13
14
use BenatEspina\StackExchangeApiClient\Authentication\Authentication;
15
use BenatEspina\StackExchangeApiClient\Http\Http;
16
use BenatEspina\StackExchangeApiClient\Model\Answer;
17
use BenatEspina\StackExchangeApiClient\Serializer\AnswerSerializer;
18
19
/**
20
 * The answer api class.
21
 *
22
 * @author Beñat Espiña <[email protected]>
23
 */
24
final class AnswerApi
25
{
26
    const URL = 'answers/';
27
    const QUERY_PARAMS = [
28
        'order'  => 'desc',
29
        'sort'   => 'activity',
30
        'site'   => 'stackoverflow',
31
        'fitler' => Http::FILTER_ALL,
32
    ];
33
34
    /**
35
     * The authentication.
36
     *
37
     * @var Authentication|null
38
     */
39
    private $authentication;
40
41
    /**
42
     * Constructor.
43
     *
44
     * @param Authentication|null $anAuthentication The authentication
45
     */
46
    public function __construct(Authentication $anAuthentication = null)
47
    {
48
        $this->authentication = $anAuthentication;
49
    }
50
51
    /**
52
     * Get all answers on the site.
53
     *
54
     * More info: http://api.stackexchange.com/docs/answers
55
     *
56
     * @param array $params    QueryString parameter(s)
57
     * @param bool  $serialize Checks if the result will be serialize or not, by default is true
58
     *
59
     * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be array|Answer? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
60
     */
61
    public function all($params = self::QUERY_PARAMS, $serialize = true)
62
    {
63
        if (null !== $this->authentication) {
64
            $params = array_merge($params, $this->authentication->toArray());
65
        }
66
        $response = Http::instance()->get(
67
            self::URL, $params
68
        );
69
70
        return $serialize === true ? AnswerSerializer::instance()->serialize($response) : $response;
71
    }
72
73
    /**
74
     * Get answers identified by a set of ids.
75
     *
76
     * More info: http://api.stackexchange.com/docs/answers-by-ids
77
     *
78
     * @param string|array $ids       Array which contains the ids delimited by semicolon, or a simple id
79
     * @param array        $params    QueryString parameter(s)
80
     * @param bool         $serialize Checks if the result will be serialize or not, by default is true
81
     *
82
     * @return array|Answer
83
     */
84
    public function getOfIds($ids, array $params = self::QUERY_PARAMS, $serialize = true)
85
    {
86
        if (null !== $this->authentication) {
87
            $params = array_merge($params, $this->authentication->toArray());
88
        }
89
        $response = Http::instance()->get(
90
            self::URL . (is_array($ids) ? implode(';', $ids) : $ids), $params
91
        );
92
93
        return $serialize === true ? AnswerSerializer::instance()->serialize($response) : $response;
94
    }
95
96
    /**
97
     * Casts an accept vote on the given answer.
98
     *
99
     * More info: https://api.stackexchange.com/docs/accept-answer
100
     *
101
     * @param string $id        The id of question
102
     * @param array  $params    QueryString parameter(s)
103
     * @param bool   $serialize Checks if the result will be serialize or not, by default is true
104
     *
105
     * @throws \Exception when the auth is null
106
     *
107
     * @return Answer
108
     */
109
    public function accept($id, array $params = self::QUERY_PARAMS, $serialize = true)
110
    {
111
        if (!$this->authentication instanceof Authentication) {
112
            throw new \Exception('Authentication is required');
113
        }
114
        $response = Http::instance()->put(
115
            self::URL . $id . '/accept', array_merge($params, $this->authentication->toArray())
116
        );
117
118
        return $serialize === true ? AnswerSerializer::instance()->serialize($response) : $response;
0 ignored issues
show
Bug Compatibility introduced by
The expression $serialize === true ? \B...$response) : $response; of type array|BenatEspina\StackE...eApiClient\Model\Answer adds the type array to the return on line 118 which is incompatible with the return type documented by BenatEspina\StackExchang...t\Api\AnswerApi::accept of type BenatEspina\StackExchangeApiClient\Model\Answer.
Loading history...
119
    }
120
121
    /**
122
     * Undoes an accept vote on the given answer.
123
     *
124
     * More info: https://api.stackexchange.com/docs/undo-accept-answer
125
     *
126
     * @param string $id        The id of question
127
     * @param array  $params    QueryString parameter(s)
128
     * @param bool   $serialize Checks if the result will be serialize or not, by default is true
129
     *
130
     * @throws \Exception when the auth is null
131
     *
132
     * @return Answer
133
     */
134
    public function undoAccept($id, array $params = self::QUERY_PARAMS, $serialize = true)
135
    {
136
        if (!$this->authentication instanceof Authentication) {
137
            throw new \Exception('Authentication is required');
138
        }
139
        $response = Http::instance()->put(
140
            self::URL . $id . '/accept/undo', array_merge($params, $this->authentication->toArray())
141
        );
142
143
        return $serialize === true ? AnswerSerializer::instance()->serialize($response) : $response;
0 ignored issues
show
Bug Compatibility introduced by
The expression $serialize === true ? \B...$response) : $response; of type array|BenatEspina\StackE...eApiClient\Model\Answer adds the type array to the return on line 143 which is incompatible with the return type documented by BenatEspina\StackExchang...i\AnswerApi::undoAccept of type BenatEspina\StackExchangeApiClient\Model\Answer.
Loading history...
144
    }
145
146
    /**
147
     * Deletes an answer.
148
     *
149
     * More info: https://api.stackexchange.com/docs/delete-answer
150
     *
151
     * @param string $id        The id of question
152
     * @param array  $params    QueryString parameter(s)
153
     * @param bool   $serialize Checks if the result will be serialize or not, by default is true
154
     *
155
     * @throws \Exception when the auth is null
156
     *
157
     * @return Answer
158
     */
159
    public function delete($id, array $params = self::QUERY_PARAMS, $serialize = true)
160
    {
161
        if (!$this->authentication instanceof Authentication) {
162
            throw new \Exception('Authentication is required');
163
        }
164
        $response = Http::instance()->delete(
165
            self::URL . $id . '/delete', array_merge($params, $this->authentication->toArray())
166
        );
167
168
        return $serialize === true ? AnswerSerializer::instance()->serialize($response) : $response;
0 ignored issues
show
Bug Compatibility introduced by
The expression $serialize === true ? \B...$response) : $response; of type array|BenatEspina\StackE...eApiClient\Model\Answer adds the type array to the return on line 168 which is incompatible with the return type documented by BenatEspina\StackExchang...t\Api\AnswerApi::delete of type BenatEspina\StackExchangeApiClient\Model\Answer.
Loading history...
169
    }
170
171
    /**
172
     * Downvotes an answer.
173
     *
174
     * More info: https://api.stackexchange.com/docs/downvote-answer
175
     *
176
     * @param string $id        The id of question
177
     * @param array  $params    QueryString parameter(s)
178
     * @param bool   $serialize Checks if the result will be serialize or not, by default is true
179
     *
180
     * @throws \Exception when the auth is null
181
     *
182
     * @return Answer
183
     */
184
    public function downvote($id, array $params = self::QUERY_PARAMS, $serialize = true)
185
    {
186
        if (!$this->authentication instanceof Authentication) {
187
            throw new \Exception('Authentication is required');
188
        }
189
        $response = Http::instance()->put(
190
            self::URL . $id . '/downvote', array_merge($params, $this->authentication->toArray())
191
        );
192
193
        return $serialize === true ? AnswerSerializer::instance()->serialize($response) : $response;
0 ignored issues
show
Bug Compatibility introduced by
The expression $serialize === true ? \B...$response) : $response; of type array|BenatEspina\StackE...eApiClient\Model\Answer adds the type array to the return on line 193 which is incompatible with the return type documented by BenatEspina\StackExchang...Api\AnswerApi::downvote of type BenatEspina\StackExchangeApiClient\Model\Answer.
Loading history...
194
    }
195
196
    /**
197
     * Undoes an downvote on an answer.
198
     *
199
     * More info: https://api.stackexchange.com/docs/undo-downvote-answer
200
     *
201
     * @param string $id        The id of question
202
     * @param array  $params    QueryString parameter(s)
203
     * @param bool   $serialize Checks if the result will be serialize or not, by default is true
204
     *
205
     * @throws \Exception when the auth is null
206
     *
207
     * @return Answer
208
     */
209
    public function undoDownvote($id, array $params = self::QUERY_PARAMS, $serialize = true)
210
    {
211
        if (!$this->authentication instanceof Authentication) {
212
            throw new \Exception('Authentication is required');
213
        }
214
        $response = Http::instance()->put(
215
            self::URL . $id . '/downvote/undo', array_merge($params, $this->authentication->toArray())
216
        );
217
218
        return $serialize === true ? AnswerSerializer::instance()->serialize($response) : $response;
0 ignored issues
show
Bug Compatibility introduced by
The expression $serialize === true ? \B...$response) : $response; of type array|BenatEspina\StackE...eApiClient\Model\Answer adds the type array to the return on line 218 which is incompatible with the return type documented by BenatEspina\StackExchang...AnswerApi::undoDownvote of type BenatEspina\StackExchangeApiClient\Model\Answer.
Loading history...
219
    }
220
221
    /**
222
     * Edit an existing answer.
223
     *
224
     * More info: https://api.stackexchange.com/docs/edit-answer
225
     *
226
     * @param string $id        The id of question
227
     * @param string $body      The body of the answer
228
     * @param array  $params    QueryString parameter(s)
229
     * @param bool   $serialize Checks if the result will be serialize or not, by default is true
230
     *
231
     * @throws \Exception when the auth is null
232
     *
233
     * @return Answer
234
     */
235 View Code Duplication
    public function update($id, $body, array $params = self::QUERY_PARAMS, $serialize = true)
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...
236
    {
237
        if (!$this->authentication instanceof Authentication) {
238
            throw new \Exception('Authentication is required');
239
        }
240
        $response = Http::instance()->put(
241
            self::URL . $id . '/edit', array_merge(['body' => $body], $params, $this->authentication->toArray())
242
        );
243
244
        return $serialize === true ? AnswerSerializer::instance()->serialize($response) : $response;
0 ignored issues
show
Bug Compatibility introduced by
The expression $serialize === true ? \B...$response) : $response; of type array|BenatEspina\StackE...eApiClient\Model\Answer adds the type array to the return on line 244 which is incompatible with the return type documented by BenatEspina\StackExchang...t\Api\AnswerApi::update of type BenatEspina\StackExchangeApiClient\Model\Answer.
Loading history...
245
    }
246
247
    /**
248
     * Upvotes an answer.
249
     *
250
     * More info: https://api.stackexchange.com/docs/upvote-answer
251
     *
252
     * @param string $id        The id of question
253
     * @param array  $params    QueryString parameter(s)
254
     * @param bool   $serialize Checks if the result will be serialize or not, by default is true
255
     *
256
     * @throws \Exception when the auth is null
257
     *
258
     * @return Answer
259
     */
260
    public function upvote($id, array $params = self::QUERY_PARAMS, $serialize = true)
261
    {
262
        if (!$this->authentication instanceof Authentication) {
263
            throw new \Exception('Authentication is required');
264
        }
265
        $response = Http::instance()->put(
266
            self::URL . $id . '/upvote', array_merge($params, $this->authentication->toArray())
267
        );
268
269
        return $serialize === true ? AnswerSerializer::instance()->serialize($response) : $response;
0 ignored issues
show
Bug Compatibility introduced by
The expression $serialize === true ? \B...$response) : $response; of type array|BenatEspina\StackE...eApiClient\Model\Answer adds the type array to the return on line 269 which is incompatible with the return type documented by BenatEspina\StackExchang...t\Api\AnswerApi::upvote of type BenatEspina\StackExchangeApiClient\Model\Answer.
Loading history...
270
    }
271
272
    /**
273
     * Undoes an upvote on an answer.
274
     *
275
     * More info: https://api.stackexchange.com/docs/undo-upvote-answer
276
     *
277
     * @param string $id        The id of question
278
     * @param array  $params    QueryString parameter(s)
279
     * @param bool   $serialize Checks if the result will be serialize or not, by default is true
280
     *
281
     * @throws \Exception when the auth is null
282
     *
283
     * @return Answer
284
     */
285
    public function undoUpvote($id, array $params = self::QUERY_PARAMS, $serialize = true)
286
    {
287
        if (!$this->authentication instanceof Authentication) {
288
            throw new \Exception('Authentication is required');
289
        }
290
        $response = Http::instance()->put(
291
            self::URL . $id . '/upvote/undo', array_merge($params, $this->authentication->toArray())
292
        );
293
294
        return $serialize === true ? AnswerSerializer::instance()->serialize($response) : $response;
0 ignored issues
show
Bug Compatibility introduced by
The expression $serialize === true ? \B...$response) : $response; of type array|BenatEspina\StackE...eApiClient\Model\Answer adds the type array to the return on line 294 which is incompatible with the return type documented by BenatEspina\StackExchang...i\AnswerApi::undoUpvote of type BenatEspina\StackExchangeApiClient\Model\Answer.
Loading history...
295
    }
296
297
    /**
298
     * Casts a flag against the answer identified by id.
299
     *
300
     * More info: https://api.stackexchange.com/docs/create-answer-flag
301
     *
302
     * @param string $id        The id of question
303
     * @param array  $params    QueryString parameter(s)
304
     * @param bool   $serialize Checks if the result will be serialize or not, by default is true
305
     *
306
     * @throws \Exception when the auth is null
307
     *
308
     * @return Answer
309
     */
310
    public function addFlag($id, array $params = self::QUERY_PARAMS, $serialize = true)
311
    {
312
        if (!$this->authentication instanceof Authentication) {
313
            throw new \Exception('Authentication is required');
314
        }
315
        $response = Http::instance()->put(
316
            self::URL . $id . '/flags/add', array_merge($params, $this->authentication->toArray())
317
        );
318
319
        return $serialize === true ? AnswerSerializer::instance()->serialize($response) : $response;
0 ignored issues
show
Bug Compatibility introduced by
The expression $serialize === true ? \B...$response) : $response; of type array|BenatEspina\StackE...eApiClient\Model\Answer adds the type array to the return on line 319 which is incompatible with the return type documented by BenatEspina\StackExchang...\Api\AnswerApi::addFlag of type BenatEspina\StackExchangeApiClient\Model\Answer.
Loading history...
320
    }
321
322
    /**
323
     * Gets the answers to a set of questions identified in id.
324
     *
325
     * More info: https://api.stackexchange.com/docs/answers-on-questions
326
     *
327
     * @param string|array $ids       Array which contains the ids delimited by semicolon, or a simple id
328
     * @param array        $params    QueryString parameter(s)
329
     * @param bool         $serialize Checks if the result will be serialize or not, by default is true
330
     *
331
     * @return array|Answer
332
     */
333
    public function getOfQuestionIds($ids, array $params = self::QUERY_PARAMS, $serialize = true)
334
    {
335
        $response = Http::instance()->get(
336
            'questions/' . (is_array($ids) ? implode(';', $ids) : $ids) . self::URL, $params
337
        );
338
339
        return $serialize === true ? AnswerSerializer::instance()->serialize($response) : $response;
340
    }
341
342
    /**
343
     * Create a new answer on the given question.
344
     *
345
     * More info: https://api.stackexchange.com/docs/create-answer
346
     *
347
     * @param string $id        The id of question
348
     * @param string $body      The body of the answer
349
     * @param array  $params    QueryString parameter(s)
350
     * @param bool   $serialize Checks if the result will be serialize or not, by default is true
351
     *
352
     * @throws \Exception when the auth is null
353
     *
354
     * @return Answer
355
     */
356 View Code Duplication
    public function addOfQuestionId($id, $body, array $params = self::QUERY_PARAMS, $serialize = true)
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...
357
    {
358
        if (!$this->authentication instanceof Authentication) {
359
            throw new \Exception('Authentication is required');
360
        }
361
        $response = Http::instance()->post(
362
            'questions/' . $id . '/' . self::URL . 'add', array_merge(
363
                ['body' => $body], $params, $this->authentication->toArray()
364
            )
365
        );
366
367
        return $serialize === true ? AnswerSerializer::instance()->serialize($response) : $response;
0 ignored issues
show
Bug Compatibility introduced by
The expression $serialize === true ? \B...$response) : $response; of type array|BenatEspina\StackE...eApiClient\Model\Answer adds the type array to the return on line 367 which is incompatible with the return type documented by BenatEspina\StackExchang...werApi::addOfQuestionId of type BenatEspina\StackExchangeApiClient\Model\Answer.
Loading history...
368
    }
369
370
    /**
371
     * Render an answer given it's body and the question it's on.
372
     *
373
     * More info: https://api.stackexchange.com/docs/render-answer
374
     *
375
     * @param string $id        The id of question
376
     * @param string $body      The body of the answer
377
     * @param array  $params    QueryString parameter(s)
378
     * @param bool   $serialize Checks if the result will be serialize or not, by default is true
379
     *
380
     * @throws \Exception when the auth is null
381
     *
382
     * @return Answer
383
     */
384
    public function render($id, $body, array $params = self::QUERY_PARAMS, $serialize = true)
385
    {
386
        $response = Http::instance()->post(
387
            'questions/' . $id . '/' . self::URL . 'render', array_merge(
388
                ['body' => $body], $params, $this->authentication->toArray()
389
            )
390
        );
391
392
        return $serialize === true ? AnswerSerializer::instance()->serialize($response) : $response;
0 ignored issues
show
Bug Compatibility introduced by
The expression $serialize === true ? \B...$response) : $response; of type array|BenatEspina\StackE...eApiClient\Model\Answer adds the type array to the return on line 392 which is incompatible with the return type documented by BenatEspina\StackExchang...t\Api\AnswerApi::render of type BenatEspina\StackExchangeApiClient\Model\Answer.
Loading history...
393
    }
394
395
    /**
396
     * Returns the answers the users in {ids} have posted.
397
     *
398
     * More info: https://api.stackexchange.com/docs/answers-on-users
399
     *
400
     * @param string|array $ids       Array which contains the ids delimited by semicolon, or a simple id
401
     * @param array        $params    QueryString parameter(s)
402
     * @param bool         $serialize Checks if the result will be serialize or not, by default is true
403
     *
404
     * @return array|Answer
405
     */
406 View Code Duplication
    public function getOfUserIds($ids, array $params = self::QUERY_PARAMS, $serialize = true)
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...
407
    {
408
        $response = Http::instance()->get(
409
            'users/' . (is_array($ids) ? implode(';', $ids) : $ids) . '/' . self::URL, $params
410
        );
411
412
        return $serialize === true ? AnswerSerializer::instance()->serialize($response) : $response;
413
    }
414
415
    /**
416
     * Returns the answers owned by the user associated with the given access_token.
417
     *
418
     * More info: https://api.stackexchange.com/docs/me-answers
419
     *
420
     * @param array $params    QueryString parameter(s)
421
     * @param bool  $serialize Checks if the result will be serialize or not, by default is true
422
     *
423
     * @throws \Exception when the auth is null
424
     *
425
     * @return array|Answer
426
     */
427
    public function myAnswers(array $params = self::QUERY_PARAMS, $serialize = true)
428
    {
429
        if (!$this->authentication instanceof Authentication) {
430
            throw new \Exception('Authentication is required');
431
        }
432
        $response = Http::instance()->get(
433
            'me/' . self::URL, array_merge($params, ['access_token' => $this->authentication->accessToken()])
434
        );
435
436
        return $serialize === true ? AnswerSerializer::instance()->serialize($response) : $response;
437
    }
438
439
    /**
440
     * Returns the top 30 answers a user has posted in response to questions with the given tags.
441
     *
442
     * More info: https://api.stackexchange.com/docs/top-user-answers-in-tags
443
     *
444
     * @param string       $userId    The user id
445
     * @param string|array $tags      Array which contains the $tags delimited by semicolon, or a simple tag
446
     * @param array        $params    QueryString parameter(s)
447
     * @param bool         $serialize Checks if the result will be serialize or not, by default is true
448
     *
449
     * @return array|Answer
450
     */
451 View Code Duplication
    public function getTopOfUserAndTags($userId, $tags, array $params = self::QUERY_PARAMS, $serialize = true)
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...
452
    {
453
        $response = Http::instance()->get(
454
            'users/' . $userId . '/tags/' . (is_array($tags) ? implode(';', $tags) : $tags) . '/top-' . self::URL, $params
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 122 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
455
        );
456
457
        return $serialize === true ? AnswerSerializer::instance()->serialize($response) : $response;
458
    }
459
460
    /**
461
     * Returns the top 30 answers the user associated with the given
462
     * access_token has posted in response to questions with the given tags.
463
     *
464
     * More info: https://api.stackexchange.com/docs/me-tags-top-answers
465
     *
466
     * @param string|array $tags      Array which contains the tags delimited by semicolon, or a simple tag
467
     * @param array        $params    QueryString parameter(s)
468
     * @param bool         $serialize Checks if the result will be serialize or not, by default is true
469
     *
470
     * @throws \Exception when the auth is null
471
     *
472
     * @return array|Answer
473
     */
474
    public function myTopAnswersOfTags($tags, array $params = self::QUERY_PARAMS, $serialize = true)
475
    {
476
        if (!$this->authentication instanceof Authentication) {
477
            throw new \Exception('Authentication is required');
478
        }
479
        $response = Http::instance()->get(
480
            'me/tags/' . (is_array($tags) ? implode(';', $tags) : $tags) . '/top-' . self::URL,
481
            array_merge($params, ['access_token' => $this->authentication->accessToken()])
482
        );
483
484
        return $serialize === true ? AnswerSerializer::instance()->serialize($response) : $response;
485
    }
486
}
487