Completed
Branch master (23259a)
by Sergey
04:17 queued 48s
created

Boards::info()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 0
loc 9
rs 9.6666
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;
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') ?
111
            mb_strtolower($nameWithRemovedSpaces) :
112
            strtolower($nameWithRemovedSpaces);
113
    }
114
115
    /**
116
     * Get pins from board by boardId.
117
     *
118
     * @param int $boardId
119
     * @param int $limit
120
     *
121
     * @return Pagination
122
     */
123
    public function pins($boardId, $limit = Pagination::DEFAULT_LIMIT)
124
    {
125
        return $this->paginate(
126
            UrlBuilder::RESOURCE_GET_BOARD_FEED,
127
            ['board_id' => $boardId],
128
            $limit
129
        );
130
    }
131
132
    /**
133
     * Update board info. Gets boardId and an associative array as params. Available keys of the array are:
134
     * 'name', 'category', 'description', 'privacy'.
135
     *
136
     * - 'privacy' can be 'public' or 'secret'. 'public' by default.
137
     * - 'category' is 'other' by default.
138
     *
139
     * @param $boardId
140
     * @param $attributes
141
     * @return mixed
142
     */
143
    public function update($boardId, $attributes)
144
    {
145
        if (isset($attributes['name'])) {
146
            $attributes['name'] = $this->formatBoardName($attributes['name']);
147
        }
148
149
        $requestOptions = array_merge(
150
            [
151
                'board_id' => $boardId,
152
                'category' => 'other',
153
            ], $attributes
154
        );
155
156
        return $this->post(UrlBuilder::RESOURCE_UPDATE_BOARD, $requestOptions);
157
    }
158
159
    /**
160
     * Create a new board.
161
     *
162
     * @param string $name
163
     * @param string $description
164
     * @param string $privacy Can be 'public' or 'secret'. 'public' by default.
165
     *
166
     * @return bool
167
     */
168
    public function create($name, $description, $privacy = self::BOARD_PRIVACY_PUBLIC)
169
    {
170
        $requestOptions = [
171
            'name'        => $name,
172
            'description' => $description,
173
            'privacy'     => $privacy,
174
        ];
175
176
        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...
177
    }
178
179
    /**
180
     * Create a new board.
181
     *
182
     * @param string $name
183
     * @param string $description
184
     *
185
     * @return bool
186
     */
187
    public function createPrivate($name, $description)
188
    {
189
        return $this->create($name, $description, self::BOARD_PRIVACY_PRIVATE);
190
    }
191
192
    /**
193
     * Returns title suggestions for pin.
194
     *
195
     * @param string $pinId
196
     * @return array|bool
197
     */
198
    public function titleSuggestionsFor($pinId)
199
    {
200
        return $this->get(UrlBuilder::RESOURCE_TITLE_SUGGESTIONS, ['pin_id' => $pinId]);
201
    }
202
}
203