Passed
Branch master (d58387)
by Sergey
03:31
created

Boards::getBoardData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 8
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A Boards::followers() 0 6 1
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
    /**
18
     * Get boards for user by username
19
     * @param string $username
20
     * @return array|bool
21
     */
22
    public function forUser($username)
23
    {
24
        $get = Request::createRequestData(['options' => ["username" => $username]]);
25
26
        return $this->boardsGetCall($get, UrlHelper::RESOURCE_GET_BOARDS);
27
    }
28
29
    /**
30
     * Get info about user's board
31
     * @param string $username
32
     * @param string $board
33
     * @return array|bool
34
     */
35
    public function info($username, $board)
36
    {
37
        $get = Request::createRequestData(
38
            [
39
                'options' => [
40
                    'username'      => $username,
41
                    'slug'          => $board,
42
                    'field_set_key' => 'detailed'
43
                ],
44
            ]
45
        );
46
47
        return $this->boardsGetCall($get, UrlHelper::RESOURCE_GET_BOARDS);
48
    }
49
50
    /**
51
     * Get pins form board by boardId
52
     * @param int $boardId
53
     * @param int $batchesLimit
54
     * @return \Iterator
55
     */
56
    public function pins($boardId, $batchesLimit = 0)
57
    {
58
        return Pagination::getPaginatedData(
59
            [$this, 'getPinsFromBoard'], [
60
            'boardId' => $boardId,
61
        ], $batchesLimit
62
        );
63
64
    }
65
66
    /**
67
     * @param int $boardId
68
     * @param array $bookmarks
69
     * @return array|bool
70
     */
71
    public function getPinsFromBoard($boardId, $bookmarks = [])
72
    {
73
        $get = Request::createRequestData(
74
            ['options' => ['board_id' => $boardId]], '', $bookmarks
75
        );
76
77
        return $this->boardsGetCall($get, UrlHelper::RESOURCE_GET_BOARD_FEED, true);
78
    }
79
80
    /**
81
     * Run GET api request to boards resource
82
     * @param array $query
83
     * @param string $url
84
     * @param bool $pagination
85
     * @return array|bool
86
     */
87
    protected function boardsGetCall($query, $url, $pagination = false)
88
    {
89
        $getString = UrlHelper::buildRequestString($query);
90
        $response = $this->request->exec($url."?{$getString}");
91
        if ($pagination) {
92
            return $this->response->getPaginationData($response);
93
        }
94
95
        return $this->response->getData($response);
96
    }
97
98
    /**
99
     * Delete your board by ID
100
     *
101
     * @param int $boardId
102
     * @return array|bool
103
     */
104
    public function delete($boardId)
105
    {
106
        return $this->callPostRequest(['board_id' => $boardId], UrlHelper::RESOURCE_DELETE_BOARD, true);
107
    }
108
109
    /**
110
     * Create a new board
111
     *
112
     * @param string $name
113
     * @param string $description
114
     * @param string $privacy Can be 'public' or 'secret'. 'public by default.
115
     * @return array|bool
116
     */
117
    public function create($name, $description, $privacy = 'public')
118
    {
119
        $requestOptions = [
120
            'name'        => $name,
121
            'description' => $description,
122
            'privacy'     => $privacy,
123
        ];
124
125
        return $this->callPostRequest($requestOptions, UrlHelper::RESOURCE_CREATE_BOARD, true);
126
    }
127
128
    /**
129
     * Get board followers
130
     *
131
     * @param $boardId
132
     * @param int $batchesLimit
133
     * @return Iterator
134
     */
135
    public function followers($boardId, $batchesLimit = 0)
136
    {
137
        return $this->getFollowData(
138
            ['board_id' => $boardId], UrlHelper::RESOURCE_BOARD_FOLLOWERS, "", $batchesLimit
139
        );
140
    }
141
142
    /**
143
     * Search scope
144
     *
145
     * @return string
146
     */
147
    protected function getScope()
148
    {
149
        return 'boards';
150
    }
151
152
    protected function getEntityIdName()
153
    {
154
        return Request::BOARD_ENTITY_ID;
155
    }
156
157
    /**
158
     * Follow resource
159
     *
160
     * @return string
161
     */
162
    protected function getFollowUrl()
163
    {
164
        return UrlHelper::RESOURCE_FOLLOW_BOARD;
165
    }
166
167
    /**
168
     * UnFollow resource
169
     * @return string
170
     */
171
    protected function getUnfFollowUrl()
172
    {
173
        return UrlHelper::RESOURCE_UNFOLLOW_BOARD;
174
    }
175
}