Completed
Pull Request — master (#44)
by Sergey
18:10
created

Boards::followers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace seregazhuk\PinterestBot\Api\Providers;
4
5
use Iterator;
6
use seregazhuk\PinterestBot\Api\Request;
7
use seregazhuk\PinterestBot\Helpers\UrlHelper;
8
use seregazhuk\PinterestBot\Helpers\Providers\Traits\FollowTrait;
9
use seregazhuk\PinterestBot\Helpers\Providers\Traits\SearchTrait;
10
11
class Boards extends Provider
12
{
13
    use SearchTrait, FollowTrait;
14
15
    /**
16
     * Get boards for user by username
17
     * @param string $username
18
     * @return array|bool
19
     */
20
    public function forUser($username)
21
    {
22
        $get = Request::createRequestData(['options' => ["username" => $username]]);
23
24
        return $this->boardsGetCall($get, UrlHelper::RESOURCE_GET_BOARDS);
25
    }
26
27
    /**
28
     * Get info about user's board
29
     * @param string $username
30
     * @param string $board
31
     * @return array|bool
32
     */
33
    public function info($username, $board)
34
    {
35
        $get = Request::createRequestData(
36
            [
37
                'options' => [
38
                    'username'      => $username,
39
                    'slug'          => $board,
40
                    'field_set_key' => 'detailed'
41
                ],
42
            ]
43
        );
44
45
        return $this->boardsGetCall($get, UrlHelper::RESOURCE_GET_BOARDS);
46
    }
47
48
    /**
49
     * Get pins form board by boardId
50
     * @param int $boardId
51
     * @param int $batchesLimit
52
     * @return \Iterator
53
     */
54
    public function pins($boardId, $batchesLimit = 0)
55
    {
56
        return $this->getPaginatedData(
57
            [$this, 'getPinsFromBoard'], [
58
            'boardId' => $boardId,
59
        ], $batchesLimit
60
        );
61
62
    }
63
64
    /**
65
     * @param int $boardId
66
     * @param array $bookmarks
67
     * @return array|bool
68
     */
69
    protected function getPinsFromBoard($boardId, $bookmarks = [])
70
    {
71
        $get = Request::createRequestData(
72
            ['options' => ['board_id' => $boardId]], '', $bookmarks
73
        );
74
75
        return $this->boardsGetCall($get, UrlHelper::RESOURCE_GET_BOARD_FEED, true);
76
    }
77
78
    /**
79
     * Run GET api request to boards resource
80
     * @param array $query
81
     * @param string $url
82
     * @param bool $pagination
83
     * @return array|bool
84
     */
85
    protected function boardsGetCall($query, $url, $pagination = false)
86
    {
87
        $getString = UrlHelper::buildRequestString($query);
88
        $response = $this->request->exec($url."?{$getString}");
89
        if ($pagination) {
90
            return $this->response->getPaginationData($response);
91
        }
92
93
        return $this->response->getData($response);
94
    }
95
96
    /**
97
     * Delete your board by ID
98
     *
99
     * @param int $boardId
100
     * @return array|bool
101
     */
102
    public function delete($boardId)
103
    {
104
        return $this->callPostRequest(['board_id' => $boardId], UrlHelper::RESOURCE_DELETE_BOARD, true);
105
    }
106
107
    /**
108
     * Create a new board
109
     *
110
     * @param string $name
111
     * @param string $description
112
     * @param string $privacy Can be 'public' or 'secret'. 'public by default.
113
     * @return array|bool
114
     */
115
    public function create($name, $description, $privacy = 'public')
116
    {
117
        $requestOptions = [
118
            'name'        => $name,
119
            'description' => $description,
120
            'privacy'     => $privacy,
121
        ];
122
123
        return $this->callPostRequest($requestOptions, UrlHelper::RESOURCE_CREATE_BOARD, true);
124
    }
125
126
    /**
127
     * @param $boardId
128
     * @param string $url
129
     * @param string $sourceUrl
130
     * @param array $bookmarks
131
     * @return array
132
     * @internal param string $username
133
     */
134 View Code Duplication
    public function getBoardData($boardId, $url, $sourceUrl, $bookmarks = [])
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...
135
    {
136
        $get = Request::createRequestData(['options' => ['board_id' => $boardId]], $sourceUrl, $bookmarks);
137
        $getString = UrlHelper::buildRequestString($get);
138
        $response = $this->request->exec($url . '?' . $getString);
139
140
        return $this->response->getPaginationData($response);
141
    }
142
143
    /**
144
     * @param $boardId
145
     * @param string $resourceUrl
146
     * @param string $sourceUrl
147
     * @param int $batchesLimit
148
     * @return Iterator
149
     */
150
    public function getPaginatedUserData($boardId, $resourceUrl, $sourceUrl, $batchesLimit = 0)
151
    {
152
        return $this->getPaginatedData(
153
            [$this, 'getBoardData'], [
154
            'boardId'   => $boardId,
155
            'url'       => $resourceUrl,
156
            'sourceUrl' => $sourceUrl,
157
        ], $batchesLimit
158
        );
159
    }
160
161
    /**
162
     * Get board followers
163
     *
164
     * @param $boardId
165
     * @param int $batchesLimit
166
     * @return Iterator
167
     */
168
    public function followers($boardId, $batchesLimit = 0)
169
    {
170
        return $this->getPaginatedUserData(
171
            $boardId, UrlHelper::RESOURCE_BOARD_FOLLOWERS, "", $batchesLimit
172
        );
173
    }
174
175
    /**
176
     * Search scope
177
     *
178
     * @return string
179
     */
180
    protected function getScope()
181
    {
182
        return 'boards';
183
    }
184
185
    protected function getEntityIdName()
186
    {
187
        return Request::BOARD_ENTITY_ID;
188
    }
189
190
    /**
191
     * Follow resource
192
     *
193
     * @return string
194
     */
195
    protected function getFollowUrl()
196
    {
197
        return UrlHelper::RESOURCE_FOLLOW_BOARD;
198
    }
199
200
    /**
201
     * UnFollow resource
202
     * @return string
203
     */
204
    protected function getUnfFollowUrl()
205
    {
206
        return UrlHelper::RESOURCE_UNFOLLOW_BOARD;
207
    }
208
}