Completed
Push — master ( 605499...e561d3 )
by dotzero
02:33
created

PostResource::addPostToFavorite()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 6
Bugs 0 Features 0
Metric Value
c 6
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Habrahabr\Api\Resources;
4
5
use Habrahabr\Api\Exception\IncorrectUsageException;
6
7
/**
8
 * Class PostResource
9
 *
10
 * Ресурс работы с постами
11
 *
12
 * @package Habrahabr\Api\Resources
13
 * @version 0.0.8
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 PostResource extends AbstractResource implements ResourceInterface
23
{
24
    /**
25
     * @const Флаг голосования в плюс
26
     */
27
    const VOTE_PLUS = 1;
28
29
    /**
30
     * @const Флаг голосования нейтрально
31
     */
32
    const VOTE_NEUTRAL = 0;
33
34
    /**
35
     * @const Флаг голосования в минус
36
     */
37
    const VOTE_MINUS = -1;
38
39
    /**
40
     * Возвращает пост по номеру
41
     *
42
     * @param int $post_id Номер поста
43
     * @return array
44
     * @throws IncorrectUsageException
45
     */
46 2
    public function getPost($post_id)
47
    {
48 2
        $this->checkId($post_id);
49
50 1
        return $this->adapter->get(sprintf('/post/%d', $post_id));
51
    }
52
53
    /**
54
     * Положительное голосование за пост
55
     *
56
     * Этот метод может быть предоставлен дополнительно, по запросу
57
     * https://habrahabr.ru/feedback/
58
     *
59
     * @param int $post_id Номер поста
60
     * @return array
61
     * @throws IncorrectUsageException
62
     */
63 2
    public function votePlus($post_id)
64
    {
65 2
        $this->checkId($post_id);
66
67 1
        return $this->vote($post_id, self::VOTE_PLUS);
68
    }
69
70
    /**
71
     * Отрицательное голосование за пост
72
     *
73
     * Этот метод может быть предоставлен дополнительно, по запросу
74
     * https://habrahabr.ru/feedback/
75
     *
76
     * @param int $post_id Номер поста
77
     * @return array
78
     * @throws IncorrectUsageException
79
     */
80 2
    public function voteMinus($post_id)
81
    {
82 2
        $this->checkId($post_id);
83
84 1
        return $this->vote($post_id, self::VOTE_MINUS);
85
    }
86
87
    /**
88
     * Нейтральное голосование за пост
89
     *
90
     * Этот метод может быть предоставлен дополнительно, по запросу
91
     * https://habrahabr.ru/feedback/
92
     *
93
     * @param int $post_id Номер поста
94
     * @return array
95
     * @throws IncorrectUsageException
96
     */
97 3
    public function voteNeutral($post_id)
98
    {
99 3
        $this->checkId($post_id);
100
101 2
        return $this->vote($post_id, self::VOTE_NEUTRAL);
102
    }
103
104
    /**
105
     * Добавить пост в избранное
106
     *
107
     * @param int $post_id Номер поста
108
     * @return array
109
     * @throws IncorrectUsageException
110
     */
111 1
    public function addPostToFavorite($post_id)
112
    {
113 1
        $this->checkId($post_id);
114
115 1
        return $this->adapter->put(sprintf('/post/%d/favorite', $post_id));
116
    }
117
118
    /**
119
     * Удалить пост из избранного
120
     *
121
     * @param int $post_id Номер поста
122
     * @return array
123
     * @throws IncorrectUsageException
124
     */
125 1
    public function removePostFromFavorite($post_id)
126
    {
127 1
        $this->checkId($post_id);
128
129 1
        return $this->adapter->delete(sprintf('/post/%d/favorite', $post_id));
130
    }
131
132
    /**
133
     * Голосование за пост
134
     *
135
     * @param int $post_id Номер поста
136
     * @param int $vote Флаг голосования
137
     * @return array
138
     */
139 4
    private function vote($post_id, $vote)
140
    {
141 4
        return $this->adapter->put(sprintf('/post/%d/vote', $post_id), ['vote' => $vote]);
142
    }
143
}
144