Completed
Pull Request — master (#220)
by Sergey
03:53
created

Boards::forUser()   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 1
1
<?php
2
3
namespace seregazhuk\PinterestBot\Api\Providers;
4
5
use Generator;
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
        'delete',
25
        'create',
26
        'follow',
27
        'unFollow',
28
        'send',
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
    /**
41
     * Get boards for user by username.
42
     *
43
     * @param string $username
44
     *
45
     * @return array|bool
46
     */
47
    public function forUser($username)
48
    {
49
        return $this->execGetRequest(['username' => $username], UrlBuilder::RESOURCE_GET_BOARDS);
50
    }
51
52
    /**
53
     * Get info about user's board.
54
     *
55
     * @param string $username
56
     * @param string $board
57
     *
58
     * @return array|bool
59
     */
60
    public function info($username, $board)
61
    {
62
        $requestOptions = [
63
            'username'      => $username,
64
            'slug'          => $board,
65
            'field_set_key' => 'detailed',
66
        ];
67
68
        return $this->execGetRequest($requestOptions, UrlBuilder::RESOURCE_GET_BOARD);
69
    }
70
71
    /**
72
     * Get pins from board by boardId.
73
     *
74
     * @param int $boardId
75
     * @param int $limit
76
     *
77
     * @return Generator
78
     */
79
    public function pins($boardId, $limit = 0)
80
    {
81
        return $this->getPaginatedResponse(['boardId' => $boardId], $limit, 'getPinsFromBoard');
82
    }
83
84
    /**
85
     * @param int   $boardId
86
     * @param array $bookmarks
87
     *
88
     * @return PaginatedResponse
89
     */
90
    public function getPinsFromBoard($boardId, $bookmarks = [])
91
    {
92
        return $this->execGetRequestWithPagination(
93
            ['board_id' => $boardId],
94
            UrlBuilder::RESOURCE_GET_BOARD_FEED,
95
            $bookmarks
96
        );
97
    }
98
99
    /**
100
     * Update board info. Gets boardId and an associative array as params. Available keys of the array are:
101
     * 'category', 'description', 'privacy'.
102
     *
103
     * - 'privacy' can be 'public' or 'secret'. 'public' by default.
104
     * - 'category' is 'other' by default.
105
     *
106
     * @param $boardId
107
     * @param $attributes
108
     * @return mixed
109
     */
110
    public function update($boardId, $attributes)
111
    {
112
        $requestOptions = array_merge(
113
            [
114
                'board_id' => $boardId,
115
                'category' => 'other',
116
            ], $attributes
117
        );
118
119
        return $this->execPostRequest($requestOptions, UrlBuilder::RESOURCE_UPDATE_BOARD);
120
    }
121
122
    /**
123
     * Create a new board.
124
     *
125
     * @param string $name
126
     * @param string $description
127
     * @param string $privacy     Can be 'public' or 'secret'. 'public' by default.
128
     *
129
     * @return bool
130
     */
131
    public function create($name, $description, $privacy = self::BOARD_PRIVACY_PUBLIC)
132
    {
133
        $requestOptions = [
134
            'name'        => $name,
135
            'description' => $description,
136
            'privacy'     => $privacy,
137
        ];
138
139
        return $this->execPostRequest($requestOptions, UrlBuilder::RESOURCE_CREATE_BOARD);
140
    }
141
142
    /**
143
     * Create a new board.
144
     *
145
     * @param string $name
146
     * @param string $description
147
     *
148
     * @return bool
149
     */
150
    public function createPrivate($name, $description)
151
    {
152
       return $this->create($name, $description, self::BOARD_PRIVACY_PRIVATE);
153
    }
154
155
    /**
156
     * Returns title suggestions for pin.
157
     *
158
     * @param string $pinId
159
     * @return array|bool
160
     */
161
    public function titleSuggestionsFor($pinId)
162
    {
163
        return $this->execGetRequest(['pin_id' => $pinId], UrlBuilder::RESOURCE_TITLE_SUGGESTIONS);
164
    }
165
166
    /**
167
     * Send board with message or by email.
168
     *
169
     * @param int $boardId
170
     * @param string $text
171
     * @param array|string $userIds
172
     * @param array|string $emails
173
     * @return bool
174
     */
175
    public function send($boardId, $text, $userIds, $emails)
176
    {
177
        $messageData = $this->buildMessageData($text, $boardId);
178
179
        return $this->callSendMessage($userIds, $emails, $messageData);
0 ignored issues
show
Bug introduced by
It seems like $userIds defined by parameter $userIds on line 175 can also be of type string; however, seregazhuk\PinterestBot\...ages::callSendMessage() does only seem to accept array|integer, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
180
    }
181
182
    /**
183
     * Send board with messages.
184
     *
185
     * @param int $boardId
186
     * @param string $text
187
     * @param array|string $userIds
188
     * @return bool
189
     */
190
    public function sendWithMessage($boardId, $text, $userIds)
191
    {
192
        return $this->send($boardId, $text, $userIds, []);
193
    }
194
195
    /**
196
     * Send board with emails.
197
     *
198
     * @param int $boardId
199
     * @param string $text
200
     * @param array|string $emails
201
     * @return bool
202
     */
203
    public function sendWithEmail($boardId, $text, $emails)
204
    {
205
        return $this->send($boardId, $text, [], $emails);
206
    }
207
}
208