CommentsResource::postComment()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 11
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 6
nc 1
nop 3
crap 1
1
<?php
2
3
namespace Habrahabr\Api\Resources;
4
5
use Habrahabr\Api\Exception\IncorrectUsageException;
6
7
/**
8
 * Class CommentsResource
9
 *
10
 * Ресурс работы с комментариями
11
 *
12
 * @package Habrahabr\Api\Resources
13
 * @version 0.1.5
14
 * @author thematicmedia <[email protected]>
15
 * @link https://tmtm.ru/
16
 * @link https://habrahabr.ru/
17
 * @link https://github.com/thematicmedia/habrahabr_api
18
 *
19
 * For the full copyright and license information, please view the LICENSE
20
 * file that was distributed with this source code.
21
 */
22
class CommentsResource extends AbstractResource implements ResourceInterface
23
{
24
    /**
25
     * @const Флаг голосования в плюс
26
     */
27
    const VOTE_PLUS = 1;
28
29
    /**
30
     * @const Флаг голосования в минус
31
     */
32
    const VOTE_MINUS = -1;
33
34
    /**
35
     * Возвращает список комментариев к посту по номеру
36
     *
37
     * @param int $post_id Номер поста
38
     * @return array
39
     * @throws IncorrectUsageException
40
     */
41 2
    public function getCommentsForPost($post_id)
42
    {
43 2
        $this->checkId($post_id);
44
45 1
        return $this->adapter->get(sprintf('/comments/%d', $post_id));
46
    }
47
48
    /**
49
     * Добавление комментария к посту по номеру
50
     *
51
     * @param int $post_id Номер поста
52
     * @param string $text Текст комментария
53
     * @param int $comment_id Номер комментария для ответа на комментарий
54
     * @return array
55
     * @throws IncorrectUsageException
56
     */
57 2
    public function postComment($post_id, $text, $comment_id = 0)
58
    {
59 2
        $this->checkId($post_id);
60
61
        $params = [
62 1
            'text' => $text,
63
            'parent_id' => $comment_id
64 1
        ];
65
66 1
        return $this->adapter->put(sprintf('/comments/%d', $post_id), $params);
67
    }
68
69
    /**
70
     * Положительное голосование за комментарий
71
     *
72
     * @param int $comment_id Номер комментария для голосования
73
     * @return array
74
     * @throws IncorrectUsageException
75
     */
76 2
    public function votePlus($comment_id)
77
    {
78 2
        $this->checkId($comment_id);
79
80 1
        return $this->vote($comment_id, self::VOTE_PLUS);
81
    }
82
83
    /**
84
     * Отрицательное голосование за комментарий
85
     *
86
     * @param int $comment_id Номер комментария для голосования
87
     * @return array
88
     * @throws IncorrectUsageException
89
     */
90 3
    public function voteMinus($comment_id)
91
    {
92 3
        $this->checkId($comment_id);
93
94 2
        return $this->vote($comment_id, self::VOTE_MINUS);
95
    }
96
97
    /**
98
     * Голосование за комментарий
99
     *
100
     * @param int $comment_id Номер комментария для голосования
101
     * @param int $vote Флаг голосования
102
     * @return array
103
     */
104 3
    private function vote($comment_id, $vote)
105
    {
106 3
        return $this->adapter->put(sprintf('/comments/%d/vote', $comment_id), ['vote' => $vote]);
107
    }
108
}
109