Completed
Pull Request — master (#114)
by Sergey
03:18
created

Pins::moveToBoard()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
namespace seregazhuk\PinterestBot\Api\Providers;
4
5
use Iterator;
6
use seregazhuk\PinterestBot\Helpers\UrlHelper;
7
use seregazhuk\PinterestBot\Helpers\Pagination;
8
use seregazhuk\PinterestBot\Helpers\Providers\Traits\Searchable;
9
10
class Pins extends Provider
11
{
12
    use Searchable;
13
14
    protected $loginRequired = [
15
        'like',
16
        'unLike',
17
        'comment',
18
        'deleteComment',
19
        'create',
20
        'repin',
21
        'delete',
22
        'activity'
23
    ];
24
25
    /**
26
     * Likes pin with current ID.
27
     *
28
     * @param int $pinId
29
     *
30
     * @return bool
31
     */
32
    public function like($pinId)
33
    {
34
        return $this->likePinMethodCall($pinId, UrlHelper::RESOURCE_LIKE_PIN);
35
    }
36
37
    /**
38
     * Removes your like from pin with current ID.
39
     *
40
     * @param int $pinId
41
     *
42
     * @return bool
43
     */
44
    public function unLike($pinId)
45
    {
46
        return $this->likePinMethodCall($pinId, UrlHelper::RESOURCE_UNLIKE_PIN);
47
    }
48
49
    /**
50
     * Calls Pinterest API to like or unlike Pin by ID.
51
     *
52
     * @param int    $pinId
53
     * @param string $resourceUrl
54
     *
55
     * @return bool
56
     */
57
    protected function likePinMethodCall($pinId, $resourceUrl)
58
    {
59
        return $this->execPostRequest(['pin_id' => $pinId], $resourceUrl);
60
    }
61
62
    /**
63
     * Write a comment for a pin with current id.
64
     *
65
     * @param int    $pinId
66
     * @param string $text  Comment
67
     *
68
     * @return array|bool
69
     */
70
    public function comment($pinId, $text)
71
    {
72
        $requestOptions = ['pin_id' => $pinId, 'test' => $text];
73
74
        return $this->execPostRequest($requestOptions, UrlHelper::RESOURCE_COMMENT_PIN, true);
75
    }
76
77
    /**
78
     * Delete a comment for a pin with current id.
79
     *
80
     * @param int $pinId
81
     * @param int $commentId
82
     *
83
     * @return bool
84
     */
85
    public function deleteComment($pinId, $commentId)
86
    {
87
        $requestOptions = ['pin_id' => $pinId, 'comment_id' => $commentId];
88
89
        return $this->execPostRequest($requestOptions, UrlHelper::RESOURCE_COMMENT_DELETE_PIN);
90
    }
91
92
    /**
93
     * Create a pin. Returns created pin ID.
94
     *
95
     * @param string $imageUrl
96
     * @param int    $boardId
97
     * @param string $description
98
     * @param string $link
99
     *
100
     * @return bool|int
101
     */
102
    public function create($imageUrl, $boardId, $description = '', $link = '')
103
    {
104
        $requestOptions = [
105
            'method'      => 'scraped',
106
            'description' => $description,
107
            'link'        => empty($link) ? $imageUrl : $link,
108
            'image_url'   => $imageUrl,
109
            'board_id'    => $boardId,
110
        ];
111
112
        return $this->execPostRequest($requestOptions, UrlHelper::RESOURCE_CREATE_PIN, true);
113
    }
114
115
    /**
116
     * Edit pin by ID. You can move pin to a new board by setting this board id.
117
     *
118
     * @param int $pindId
119
     * @param string $description
120
     * @param string $link
121
     * @param int|null $boardId
122
     * @return mixed
123
     */
124
    public function edit($pindId, $description = '', $link = '', $boardId = null)
125
    {
126
        $requestOptions = [
127
            'id'          => $pindId,
128
            'description' => $description,
129
            'link'        => $link,
130
            'board_id'    => $boardId,
131
        ];
132
133
        return $this->execPostRequest($requestOptions, UrlHelper::RESOURCE_UPDATE_PIN, true);
134
    }
135
136
    /**
137
     * Moves pin to a new board
138
     *
139
     * @param int $pindId
140
     * @param int $boardId
141
     * @return mixed
142
     */
143
    public function moveToBoard($pindId, $boardId)
144
    {
145
        return $this->edit($pindId, '', '', $boardId);
146
    }
147
    
148
    /**
149
     * Make a repin.
150
     *
151
     * @param int    $repinId
152
     * @param int    $boardId
153
     * @param string $description
154
     *
155
     * @return bool|int
156
     */
157
    public function repin($repinId, $boardId, $description = '')
158
    {
159
        $requestOptions = [
160
            'board_id'    => $boardId,
161
            'description' => stripslashes($description),
162
            'link'        => stripslashes($repinId),
163
            'is_video'    => null,
164
            'pin_id'      => $repinId,
165
        ];
166
167
        return $this->execPostRequest($requestOptions, UrlHelper::RESOURCE_REPIN, true);
168
    }
169
170
    /**
171
     * Delete a pin.
172
     *
173
     * @param int $pinId
174
     *
175
     * @return bool
176
     */
177
    public function delete($pinId)
178
    {
179
        return $this->execPostRequest(['id' => $pinId], UrlHelper::RESOURCE_DELETE_PIN);
180
    }
181
182
    /**
183
     * Get information of a pin by PinID.
184
     *
185
     * @param int $pinId
186
     *
187
     * @return array|bool
188
     */
189
    public function info($pinId)
190
    {
191
        $requestOptions = [
192
            'id'            => $pinId,
193
            'field_set_key' => 'detailed',
194
        ];
195
196
        return $this->execGetRequest($requestOptions, UrlHelper::RESOURCE_PIN_INFO);
197
    }
198
199
    /**
200
     * Get pins from a specific url. For example: https://pinterest.com/source/flickr.com/ will return
201
     * recent Pins from flickr.com
202
     *
203
     * @param string $source
204
     * @param int $limit
205
     * @return Iterator
206
     */
207
    public function fromSource($source, $limit = 0)
208
    {
209
        $params = [
210
            'data' => ['domain' => $source],
211
            'url'  => UrlHelper::RESOURCE_DOMAIN_FEED,
212
        ];
213
214
        return (new Pagination($this))->paginate('getPaginatedData', $params, $limit);
215
    }
216
217
    /**
218
     * @param $pinId
219
     * @param int $limit
220
     * @return Iterator
221
     */
222
    public function activity($pinId, $limit = 0)
223
    {
224
        $pinInfo = $this->info($pinId);
225
        if (!isset($pinInfo['aggregated_pin_data']['id'])) {
226
            return null;
227
        }
228
229
        $aggregatedPinId = $pinInfo['aggregated_pin_data']['id'];
230
        $params = [
231
            'data' => ['aggregated_pin_data_id' => $aggregatedPinId],
232
            'url'  => UrlHelper::RESOURCE_ACTIVITY
233
        ];
234
235
        return (new Pagination($this))->paginate('getPaginatedData', $params, $limit);
236
    }
237
238
    /**
239
     * @return string
240
     */
241
    protected function getScope()
242
    {
243
        return 'pins';
244
    }
245
}
246