Completed
Push — master ( 523146...d3886c )
by Sergey
03:44
created

Boards::getScope()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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