Completed
Push — master ( 2a28c4...4998b6 )
by dotzero
03:11
created

PostResource::increaseCount()   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 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
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.1.2
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
     * Получить мета-информацию постов (не более 30 постов за раз)
55
     *
56
     * @param int $posts_id Номера постов
57
     * @return array
58
     * @throws IncorrectUsageException
59
     */
60 2
    public function getMeta($posts_id)
61
    {
62 2
        if (!is_array($posts_id)) {
63
            $posts_id = [$posts_id];
64
        }
65
66 2
        array_map([$this, 'checkId'], $posts_id);
67
68 1
        return $this->adapter->get(sprintf('/posts/meta?ids=%s', implode(',', $posts_id)));
69
    }
70
71
    /**
72
     * Положительное голосование за пост
73
     *
74
     * Этот метод может быть предоставлен дополнительно, по запросу
75
     * https://habrahabr.ru/feedback/
76
     *
77
     * @param int $post_id Номер поста
78
     * @return array
79
     * @throws IncorrectUsageException
80
     */
81 2
    public function votePlus($post_id)
82
    {
83 2
        $this->checkId($post_id);
84
85 1
        return $this->vote($post_id, self::VOTE_PLUS);
86
    }
87
88
    /**
89
     * Отрицательное голосование за пост
90
     *
91
     * Этот метод может быть предоставлен дополнительно, по запросу
92
     * https://habrahabr.ru/feedback/
93
     *
94
     * @param int $post_id Номер поста
95
     * @return array
96
     * @throws IncorrectUsageException
97
     */
98 2
    public function voteMinus($post_id)
99
    {
100 2
        $this->checkId($post_id);
101
102 1
        return $this->vote($post_id, self::VOTE_MINUS);
103
    }
104
105
    /**
106
     * Нейтральное голосование за пост
107
     *
108
     * Этот метод может быть предоставлен дополнительно, по запросу
109
     * https://habrahabr.ru/feedback/
110
     *
111
     * @param int $post_id Номер поста
112
     * @return array
113
     * @throws IncorrectUsageException
114
     */
115 3
    public function voteNeutral($post_id)
116
    {
117 3
        $this->checkId($post_id);
118
119 2
        return $this->vote($post_id, self::VOTE_NEUTRAL);
120
    }
121
122
    /**
123
     * Добавить пост в избранное
124
     *
125
     * @param int $post_id Номер поста
126
     * @return array
127
     * @throws IncorrectUsageException
128
     */
129 1
    public function addPostToFavorite($post_id)
130
    {
131 1
        $this->checkId($post_id);
132
133 1
        return $this->adapter->put(sprintf('/post/%d/favorite', $post_id));
134
    }
135
136
    /**
137
     * Удалить пост из избранного
138
     *
139
     * @param int $post_id Номер поста
140
     * @return array
141
     * @throws IncorrectUsageException
142
     */
143 1
    public function removePostFromFavorite($post_id)
144
    {
145 1
        $this->checkId($post_id);
146
147 1
        return $this->adapter->delete(sprintf('/post/%d/favorite', $post_id));
148
    }
149
150
    /**
151
     * Увеличить счетчик просмотров поста
152
     *
153
     * @param int $post_id Номер поста
154
     * @return array
155
     * @throws IncorrectUsageException
156
     */
157 1
    public function increaseCount($post_id)
158
    {
159 1
        $this->checkId($post_id);
160
161 1
        return $this->adapter->put(sprintf('/post/%d/viewcount', $post_id));
162
    }
163
164
    /**
165
     * Голосование за пост
166
     *
167
     * @param int $post_id Номер поста
168
     * @param int $vote Флаг голосования
169
     * @return array
170
     */
171 4
    private function vote($post_id, $vote)
172
    {
173 4
        return $this->adapter->put(sprintf('/post/%d/vote', $post_id), ['vote' => $vote]);
174
    }
175
}
176