Completed
Pull Request — master (#229)
by Sergey
02:32
created

Boards::sendWithMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 3
1
<?php
2
3
namespace seregazhuk\PinterestBot\Api\Providers;
4
5
use seregazhuk\PinterestBot\Helpers\Pagination;
6
use seregazhuk\PinterestBot\Helpers\UrlBuilder;
7
use seregazhuk\PinterestBot\Api\Traits\Searchable;
8
use seregazhuk\PinterestBot\Api\Traits\Followable;
9
use seregazhuk\PinterestBot\Api\Traits\CanBeDeleted;
10
use seregazhuk\PinterestBot\Api\Traits\SendsMessages;
11
use seregazhuk\PinterestBot\Api\Contracts\PaginatedResponse;
12
13
class Boards extends Provider
14
{
15
    use CanBeDeleted, Searchable, Followable, SendsMessages;
16
17
    const BOARD_PRIVACY_PUBLIC = 'public';
18
    const BOARD_PRIVACY_PRIVATE = 'secret';
19
20
    /**
21
     * @var array
22
     */
23
    protected $loginRequiredFor = [
24
        'send',
25
        'delete',
26
        'create',
27
        'follow',
28
        'unFollow',
29
    ];
30
31
    protected $searchScope  = 'boards';
32
    protected $entityIdName = 'board_id';
33
    protected $followersFor = 'board_id';
34
35
    protected $followUrl    = UrlBuilder::RESOURCE_FOLLOW_BOARD;
36
    protected $unFollowUrl  = UrlBuilder::RESOURCE_UNFOLLOW_BOARD;
37
    protected $deleteUrl    = UrlBuilder::RESOURCE_DELETE_BOARD;
38
    protected $followersUrl = UrlBuilder::RESOURCE_BOARD_FOLLOWERS;
39
40
    protected $messageEntityName = 'board';
41
42
    /**
43
     * Get boards for user by username.
44
     *
45
     * @param string $username
46
     *
47
     * @return array|bool
48
     */
49
    public function forUser($username)
50
    {
51
        return $this->execGetRequest(['username' => $username], UrlBuilder::RESOURCE_GET_BOARDS);
52
    }
53
54
    /**
55
     * Get info about user's board.
56
     *
57
     * @param string $username
58
     * @param string $board
59
     *
60
     * @return array|bool
61
     */
62
    public function info($username, $board)
63
    {
64
        $requestOptions = [
65
            'username'      => $username,
66
            'slug'          => $board,
67
            'field_set_key' => 'detailed',
68
        ];
69
70
        return $this->execGetRequest($requestOptions, UrlBuilder::RESOURCE_GET_BOARD);
71
    }
72
73
    /**
74
     * Get pins from board by boardId.
75
     *
76
     * @param int $boardId
77
     * @param int $limit
78
     *
79
     * @return Pagination
80
     */
81
    public function pins($boardId, $limit = Pagination::DEFAULT_LIMIT)
82
    {
83
        return (new Pagination($limit))
84
            ->paginateOver(function($bookmarks = []) use ($boardId) {
85
                return $this->getPinsFromBoard($boardId, $bookmarks);
86
            });
87
    }
88
89
    /**
90
     * @param int   $boardId
91
     * @param array $bookmarks
92
     *
93
     * @return PaginatedResponse
94
     */
95
    public function getPinsFromBoard($boardId, $bookmarks = [])
96
    {
97
        return $this->execGetRequestWithPagination(
98
            ['board_id' => $boardId],
99
            UrlBuilder::RESOURCE_GET_BOARD_FEED,
100
            $bookmarks
101
        );
102
    }
103
104
    /**
105
     * Update board info. Gets boardId and an associative array as params. Available keys of the array are:
106
     * 'category', 'description', 'privacy'.
107
     *
108
     * - 'privacy' can be 'public' or 'secret'. 'public' by default.
109
     * - 'category' is 'other' by default.
110
     *
111
     * @param $boardId
112
     * @param $attributes
113
     * @return mixed
114
     */
115
    public function update($boardId, $attributes)
116
    {
117
        $requestOptions = array_merge(
118
            [
119
                'board_id' => $boardId,
120
                'category' => 'other',
121
            ], $attributes
122
        );
123
124
        return $this->execPostRequest($requestOptions, UrlBuilder::RESOURCE_UPDATE_BOARD);
125
    }
126
127
    /**
128
     * Create a new board.
129
     *
130
     * @param string $name
131
     * @param string $description
132
     * @param string $privacy     Can be 'public' or 'secret'. 'public' by default.
133
     *
134
     * @return bool
135
     */
136
    public function create($name, $description, $privacy = self::BOARD_PRIVACY_PUBLIC)
137
    {
138
        $requestOptions = [
139
            'name'        => $name,
140
            'description' => $description,
141
            'privacy'     => $privacy,
142
        ];
143
144
        return $this->execPostRequest($requestOptions, UrlBuilder::RESOURCE_CREATE_BOARD);
145
    }
146
147
    /**
148
     * Create a new board.
149
     *
150
     * @codeCoverageIgnore
151
     * @param string $name
152
     * @param string $description
153
     *
154
     * @return bool
155
     */
156
    public function createPrivate($name, $description)
157
    {
158
       return $this->create($name, $description, self::BOARD_PRIVACY_PRIVATE);
159
    }
160
161
    /**
162
     * Returns title suggestions for pin.
163
     *
164
     * @param string $pinId
165
     * @return array|bool
166
     */
167
    public function titleSuggestionsFor($pinId)
168
    {
169
        return $this->execGetRequest(['pin_id' => $pinId], UrlBuilder::RESOURCE_TITLE_SUGGESTIONS);
170
    }
171
}
172