Completed
Push — master ( 54cad5...54d76e )
by Sergey
05:02 queued 02:30
created

Boards::formatBoardName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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