Completed
Push — master ( 356572...7d3154 )
by Sergey
03:26
created

Boards::info()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
rs 9.4286
cc 1
eloc 7
nc 1
nop 2
1
<?php
2
3
namespace seregazhuk\PinterestBot\Api\Providers;
4
5
use seregazhuk\PinterestBot\Helpers\Providers\FollowHelper;
6
use seregazhuk\PinterestBot\Helpers\Providers\SearchHelper;
7
use seregazhuk\PinterestBot\Api\Request;
8
use seregazhuk\PinterestBot\Helpers\UrlHelper;
9
10
class Boards extends Provider
11
{
12
    use SearchHelper, FollowHelper;
13
14
    /**
15
     * Get boards for user by username
16
     * @param string $username
17
     * @return array|bool
18
     */
19
    public function forUser($username)
20
    {
21
        $get = Request::createRequestData(
22
            [
23
                'options' => ["username" => $username],
24
            ]
25
        );
26
27
        return $this->boardsGetCall($get, UrlHelper::RESOURCE_GET_BOARDS);
28
    }
29
30
    /**
31
     * Get info about user's board
32
     * @param string $username
33
     * @param string $board
34
     * @return array|bool
35
     */
36
    public function info($username, $board)
37
    {
38
        $get = Request::createRequestData(
39
            [
40
                'options' => [
41
                    'username'      => $username,
42
                    'slug'          => $board,
43
                    'field_set_key' => 'detailed'
44
                ],
45
            ]
46
        );
47
48
        return $this->boardsGetCall($get, UrlHelper::RESOURCE_GET_BOARDS);
49
    }
50
51
    /**
52
     * Get pins form board by boardId
53
     * @param int $boardId
54
     * @param int $batchesLimit
55
     * @return \Iterator
56
     */
57
    public function pins($boardId, $batchesLimit = 0)
58
    {
59
        return $this->getPaginatedData(
60
            [$this, 'getPinsFromBoard'], [
61
            'boardId' => $boardId,
62
        ], $batchesLimit
63
        );
64
65
    }
66
67
    /**
68
     * @param int   $boardId
69
     * @param array $bookmarks
70
     * @return array|bool
71
     */
72
    protected function getPinsFromBoard($boardId, $bookmarks = [])
73
    {
74
        $get = Request::createRequestData(
75
            [
76
                'options' => ['board_id' => $boardId],
77
            ], '', $bookmarks
78
        );
79
80
        return $this->boardsGetCall($get, UrlHelper::RESOURCE_GET_BOARD_FEED, true);
81
    }
82
83
    /**
84
     * Run GET api request to boards resource
85
     * @param array  $query
86
     * @param string $url
87
     * @param bool   $pagination
88
     * @return array|bool
89
     */
90
    protected function boardsGetCall($query, $url, $pagination = false)
91
    {
92
        $getString = UrlHelper::buildRequestString($query);
93
        $response = $this->request->exec($url."?{$getString}");
94
95
        if ($pagination) {
96
            return $this->response->getPaginationData($response);
97
        }
98
99
        return $this->response->getData($response);
100
    }
101
102
    protected function getScope()
103
    {
104
        return 'boards';
105
    }
106
107
    protected function getEntityIdName()
108
    {
109
        return Request::BOARD_ENTITY_ID;
110
    }
111
112
    protected function getFollowUrl()
113
    {
114
        return UrlHelper::RESOURCE_FOLLOW_BOARD;
115
    }
116
117
    protected function getUnfFollowUrl()
118
    {
119
        return UrlHelper::RESOURCE_UNFOLLOW_BOARD;
120
    }
121
}