Completed
Push — master ( 0f5404...9fd863 )
by Sergey
08:17 queued 05:42
created

Boards::update()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 15
rs 9.4285
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 ?: [];
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) return [];
70
71
        return $this->forUser($currentUserName);
72
    }
73
74
    /**
75
     * @return array
76
     */
77
    public function my()
78
    {
79
        return $this->forMe();
80
    }
81
82
    /**
83
     * Get info about user's board.
84
     *
85
     * @param string $username
86
     * @param string $board
87
     *
88
     * @return array|bool
89
     */
90
    public function info($username, $board)
91
    {
92
        $requestOptions = [
93
            'slug'          => $this->formatBoardName($board),
94
            'username'      => $username,
95
            'field_set_key' => 'detailed',
96
        ];
97
98
        return $this->get(UrlBuilder::RESOURCE_GET_BOARD, $requestOptions);
99
    }
100
101
    /**
102
     * @param string $board
103
     * @return string
104
     */
105
    protected function formatBoardName($board)
106
    {
107
        $nameWithRemovedSpaces = str_replace(' ', '-', $board);
108
        return function_exists('mb_strtolower') ?
109
            mb_strtolower($nameWithRemovedSpaces) :
110
            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,
125
            ['board_id' => $boardId],
126
            $limit
127
        );
128
    }
129
130
    /**
131
     * Update board info. Gets boardId and an associative array as params. Available keys of the array are:
132
     * 'name', 'category', 'description', 'privacy'.
133
     *
134
     * - 'privacy' can be 'public' or 'secret'. 'public' by default.
135
     * - 'category' is 'other' by default.
136
     *
137
     * @param $boardId
138
     * @param $attributes
139
     * @return mixed
140
     */
141
    public function update($boardId, $attributes)
142
    {
143
        if (isset($attributes['name'])) {
144
            $attributes['name'] = $this->formatBoardName($attributes['name']);
145
        }
146
147
        $requestOptions = array_merge(
148
            [
149
                'board_id' => $boardId,
150
                'category' => 'other',
151
            ], $attributes
152
        );
153
154
        return $this->post(UrlBuilder::RESOURCE_UPDATE_BOARD, $requestOptions);
155
    }
156
157
    /**
158
     * Create a new board.
159
     *
160
     * @param string $name
161
     * @param string $description
162
     * @param string $privacy Can be 'public' or 'secret'. 'public' by default.
163
     *
164
     * @return bool
165
     */
166
    public function create($name, $description, $privacy = self::BOARD_PRIVACY_PUBLIC)
167
    {
168
        $requestOptions = [
169
            'name'        => $name,
170
            'description' => $description,
171
            'privacy'     => $privacy,
172
        ];
173
174
        return $this->post(UrlBuilder::RESOURCE_CREATE_BOARD, $requestOptions);
175
    }
176
177
    /**
178
     * Create a new board.
179
     *
180
     * @param string $name
181
     * @param string $description
182
     *
183
     * @return bool
184
     */
185
    public function createPrivate($name, $description)
186
    {
187
        return $this->create($name, $description, self::BOARD_PRIVACY_PRIVATE);
188
    }
189
190
    /**
191
     * Returns title suggestions for pin.
192
     *
193
     * @param string $pinId
194
     * @return array|bool
195
     */
196
    public function titleSuggestionsFor($pinId)
197
    {
198
        return $this->get(UrlBuilder::RESOURCE_TITLE_SUGGESTIONS, ['pin_id' => $pinId]);
199
    }
200
}
201