Boards::my()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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