Passed
Push — master ( d5d6b0...312b1e )
by Sergey
03:09
created

Boards::invites()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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