Completed
Pull Request — master (#258)
by Sergey
02:44
created

Boards::pins()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
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\Followable;
8
use seregazhuk\PinterestBot\Api\Traits\Searchable;
9
use seregazhuk\PinterestBot\Api\Traits\CanBeDeleted;
10
use seregazhuk\PinterestBot\Api\Traits\SendsMessages;
11
use seregazhuk\PinterestBot\Api\Providers\Core\EntityProvider;
12
13
class Boards extends EntityProvider
14
{
15
    use CanBeDeleted, Searchable, Followable, SendsMessages;
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
        $currentUserProfile = $this->resolveCurrentUsername();
70
71
        if (!$currentUserProfile) return [];
72
73
        return $this->forUser($currentUserProfile['username']);
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
            'username'      => $username,
88
            'slug'          => $board,
89
            'field_set_key' => 'detailed',
90
        ];
91
92
        return $this->get($requestOptions, UrlBuilder::RESOURCE_GET_BOARD);
93
    }
94
95
    /**
96
     * Get pins from board by boardId.
97
     *
98
     * @param int $boardId
99
     * @param int $limit
100
     *
101
     * @return Pagination
102
     */
103
    public function pins($boardId, $limit = Pagination::DEFAULT_LIMIT)
104
    {
105
        return $this->paginate(
106
            ['board_id' => $boardId],
107
            UrlBuilder::RESOURCE_GET_BOARD_FEED,
108
            $limit
109
        );
110
    }
111
112
    /**
113
     * Update board info. Gets boardId and an associative array as params. Available keys of the array are:
114
     * 'name', 'category', 'description', 'privacy'.
115
     *
116
     * - 'privacy' can be 'public' or 'secret'. 'public' by default.
117
     * - 'category' is 'other' by default.
118
     *
119
     * @param $boardId
120
     * @param $attributes
121
     * @return mixed
122
     */
123
    public function update($boardId, $attributes)
124
    {
125
        $requestOptions = array_merge(
126
            [
127
                'board_id' => $boardId,
128
                'category' => 'other',
129
            ], $attributes
130
        );
131
132
        return $this->post($requestOptions, UrlBuilder::RESOURCE_UPDATE_BOARD);
133
    }
134
135
    /**
136
     * Create a new board.
137
     *
138
     * @param string $name
139
     * @param string $description
140
     * @param string $privacy     Can be 'public' or 'secret'. 'public' by default.
141
     *
142
     * @return bool
143
     */
144
    public function create($name, $description, $privacy = self::BOARD_PRIVACY_PUBLIC)
145
    {
146
        $requestOptions = [
147
            'name'        => $name,
148
            'description' => $description,
149
            'privacy'     => $privacy,
150
        ];
151
152
        return $this->post($requestOptions, UrlBuilder::RESOURCE_CREATE_BOARD);
153
    }
154
155
    /**
156
     * Create a new board.
157
     *
158
     * @codeCoverageIgnore
159
     * @param string $name
160
     * @param string $description
161
     *
162
     * @return bool
163
     */
164
    public function createPrivate($name, $description)
165
    {
166
       return $this->create($name, $description, self::BOARD_PRIVACY_PRIVATE);
167
    }
168
169
    /**
170
     * Returns title suggestions for pin.
171
     *
172
     * @param string $pinId
173
     * @return array|bool
174
     */
175
    public function titleSuggestionsFor($pinId)
176
    {
177
        return $this->get(['pin_id' => $pinId], UrlBuilder::RESOURCE_TITLE_SUGGESTIONS);
178
    }
179
}
180