Completed
Push — master ( d7b107...959971 )
by Sergey
04:12 queued 01:51
created

Pins::copy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
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\Api\Response;
7
use seregazhuk\PinterestBot\Api\Traits\HasFeed;
8
use seregazhuk\PinterestBot\Helpers\UrlBuilder;
9
use seregazhuk\PinterestBot\Api\Traits\Searchable;
10
use seregazhuk\PinterestBot\Api\Traits\SendsMessages;
11
use seregazhuk\PinterestBot\Api\Traits\CanBeDeleted;
12
use seregazhuk\PinterestBot\Api\Traits\UploadsImages;
13
14
class Pins extends Provider
15
{
16
    use Searchable, CanBeDeleted, UploadsImages, HasFeed, SendsMessages;
17
18
    protected $loginRequiredFor = [
19
        'like',
20
        'unLike',
21
        'create',
22
        'repin',
23
        'copy',
24
        'move',
25
        'delete',
26
        'activity',
27
        'feed',
28
        'send',
29
        'visualSimilar',
30
    ];
31
32
    protected $searchScope  = 'pins';
33
    protected $entityIdName = 'id';
34
35
    protected $deleteUrl = UrlBuilder::RESOURCE_DELETE_PIN;
36
    
37
    /**
38
     * Likes pin with current ID.
39
     *
40
     * @param string $pinId
41
     *
42
     * @return bool
43
     */
44
    public function like($pinId)
45
    {
46
        return $this->likePinMethodCall($pinId, UrlBuilder::RESOURCE_LIKE_PIN);
47
    }
48
49
    /**
50
     * Removes your like from pin with current ID.
51
     *
52
     * @param string $pinId
53
     *
54
     * @return bool
55
     */
56
    public function unLike($pinId)
57
    {
58
        return $this->likePinMethodCall($pinId, UrlBuilder::RESOURCE_UNLIKE_PIN);
59
    }
60
61
    /**
62
     * Create a pin. Returns created pin info.
63
     *
64
     * @param string $imageUrl
65
     * @param int    $boardId
66
     * @param string $description
67
     * @param string $link
68
     *
69
     * @return array
70
     */
71
    public function create($imageUrl, $boardId, $description = '', $link = '')
72
    {
73
        // Upload image if first argument is not url
74
        if (!filter_var($imageUrl, FILTER_VALIDATE_URL)) {
75
            $imageUrl = $this->upload($imageUrl);
76
        }
77
78
        $requestOptions = [
79
            'method'      => 'scraped',
80
            'description' => $description,
81
            'link'        => empty($link) ? $imageUrl : $link,
82
            'image_url'   => $imageUrl,
83
            'board_id'    => $boardId,
84
        ];
85
86
        return $this
87
            ->execPostRequest($requestOptions, UrlBuilder::RESOURCE_CREATE_PIN, true)
88
            ->getResponseData();
89
    }
90
91
    /**
92
     * Edit pin by ID. You can move pin to a new board by setting this board id.
93
     *
94
     * @param int $pindId
95
     * @param string $description
96
     * @param string $link
97
     * @param int|null $boardId
98
     * @return bool
99
     */
100
    public function edit($pindId, $description = '', $link = '', $boardId = null)
101
    {
102
        $requestOptions = [
103
            'id'          => $pindId,
104
            'description' => $description,
105
            'link'        => $link,
106
            'board_id'    => $boardId,
107
        ];
108
109
        return $this->execPostRequest($requestOptions, UrlBuilder::RESOURCE_UPDATE_PIN);
110
    }
111
112
    /**
113
     * Moves pin to a new board
114
     *
115
     * @param int $pindId
116
     * @param int $boardId
117
     * @return bool
118
     */
119
    public function moveToBoard($pindId, $boardId)
120
    {
121
        return $this->edit($pindId, '', '', $boardId);
122
    }
123
    
124
    /**
125
     * Make a repin.
126
     *
127
     * @param int   $repinId
128
     * @param int   $boardId
129
     * @param string $description
130
     *
131
     * @return array
132
     */
133
    public function repin($repinId, $boardId, $description = '')
134
    {
135
        $requestOptions = [
136
            'board_id'    => $boardId,
137
            'description' => stripslashes($description),
138
            'link'        => stripslashes($repinId),
139
            'is_video'    => null,
140
            'pin_id'      => $repinId,
141
        ];
142
143
        return $this
144
            ->execPostRequest($requestOptions, UrlBuilder::RESOURCE_REPIN, true)
145
            ->getResponseData();
146
    }
147
148
    /**
149
     * Get information of a pin by PinID.
150
     *
151
     * @param string $pinId
152
     *
153
     * @return array|bool
154
     */
155
    public function info($pinId)
156
    {
157
        $requestOptions = [
158
            'id'            => $pinId,
159
            'field_set_key' => 'detailed',
160
        ];
161
162
        return $this->execGetRequest($requestOptions, UrlBuilder::RESOURCE_PIN_INFO);
163
    }
164
165
    /**
166
     * Get pins from a specific url. For example: https://pinterest.com/source/flickr.com/ will
167
     * return recent Pins from flickr.com
168
     *
169
     * @param string $source
170
     * @param int $limit
171
     * @return Iterator
172
     */
173
    public function fromSource($source, $limit = 0)
174
    {
175
        $data = ['domain' => $source];
176
177
        return $this->getFeed($data, UrlBuilder::RESOURCE_DOMAIN_FEED, $limit);
178
    }
179
180
    /**
181
     * Get the latest pin activity with pagination.
182
     *
183
     * @param string $pinId
184
     * @param int $limit
185
     * @return Iterator|null
186
     */
187
    public function activity($pinId, $limit = 0)
188
    {
189
        $aggregatedPinId = $this->getAggregatedPinId($pinId);
190
191
        if (is_null($aggregatedPinId)) return null;
192
193
        $data = ['aggregated_pin_data_id' => $aggregatedPinId];
194
195
        return $this->getFeed($data, UrlBuilder::RESOURCE_ACTIVITY, $limit);
196
    }
197
198
    /**
199
     * Get pins from user's feed
200
     *
201
     * @param int $limit
202
     * @return Iterator
203
     */
204
    public function feed($limit = 0)
205
    {
206
        return $this->getFeed([], UrlBuilder::RESOURCE_USER_FEED, $limit);
207
    }
208
209
    /**
210
     * @param string $pinId
211
     * @param int $limit
212
     * @return mixed
213
     */
214
    public function related($pinId, $limit = 0)
215
    {
216
        return $this->getFeed(['pin' => $pinId], UrlBuilder::RESOURCE_RELATED_PINS, $limit);
217
    }
218
219
    /**
220
     * @codeCoverageIgnore
221
     * Copy pins to board
222
     *
223
     * @param array|string $pinIds
224
     * @param int $boardId
225
     * @return bool|Response
226
     */
227
    public function copy($pinIds, $boardId)
228
    {
229
        return $this->bulkEdit($pinIds, $boardId, UrlBuilder::RESOURCE_BULK_COPY);
0 ignored issues
show
Bug introduced by
It seems like $pinIds defined by parameter $pinIds on line 227 can also be of type string; however, seregazhuk\PinterestBot\...viders\Pins::bulkEdit() does only seem to accept integer|array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
230
    }
231
232
    /**
233
     * @codeCoverageIgnore
234
     * Delete pins from board.
235
     *
236
     * @param int|array $pinIds
237
     * @param int $boardId
238
     * @return bool
239
     */
240
    public function deleteFromBoard($pinIds, $boardId)
241
    {
242
        return $this->bulkEdit($pinIds, $boardId, UrlBuilder::RESOURCE_BULK_DELETE);
243
    }
244
245
    /**
246
     * Send pin with message or by email.
247
     *
248
     * @param string $pinId
249
     * @param string $text
250
     * @param array|int $userIds
251
     * @param array|int $emails
252
     * @return bool
253
     */
254
    public function send($pinId, $text, $userIds, $emails)
255
    {
256
        $messageData = $this->buildMessageData($text, $pinId);
257
258
        return $this->callSendMessage($userIds, $emails, $messageData);
0 ignored issues
show
Bug introduced by
It seems like $emails defined by parameter $emails on line 254 can also be of type integer; however, seregazhuk\PinterestBot\...ages::callSendMessage() does only seem to accept array|string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
259
    }
260
261
    /**
262
     * @codeCoverageIgnore
263
     * Move pins to board
264
     *
265
     * @param int|array $pinIds
266
     * @param int $boardId
267
     * @return bool|Response
268
     */
269
    public function move($pinIds, $boardId)
270
    {
271
        return $this->bulkEdit($pinIds, $boardId, UrlBuilder::RESOURCE_BULK_MOVE);
272
    }
273
    
274
    /**
275
     * @param string $pinId
276
     * @param array $crop
277
     * @return array|bool
278
     */
279
    public function visualSimilar($pinId, array $crop = [])
280
    {
281
        $data = [
282
            'pin_id' => $pinId,
283
            'crop' => $crop ? : [
284
                "x" => 0.16,
285
                "y" => 0.16,
286
                "w" => 0.66,
287
                "h" => 0.66,
288
                "num_crop_actions" => 1
289
            ],
290
            'force_refresh' => true,
291
            'keep_duplicates' => false
292
        ];
293
294
        return $this->execGetRequest($data, UrlBuilder::RESOURCE_VISUAL_SIMILAR_PINS);
295
    }
296
    
297
    /**
298
     * Calls Pinterest API to like or unlike Pin by ID.
299
     *
300
     * @param string $pinId
301
     * @param string $resourceUrl
302
     *
303
     * @return bool
304
     */
305
    protected function likePinMethodCall($pinId, $resourceUrl)
306
    {
307
        return $this->execPostRequest(['pin_id' => $pinId], $resourceUrl);
308
    }
309
310
    /**
311
     * @param string $pinId
312
     * @return int|null
313
     */
314
    protected function getAggregatedPinId($pinId)
315
    {
316
        $pinInfo = $this->info($pinId);
317
318
        return isset($pinInfo['aggregated_pin_data']['id']) ?
319
            $pinInfo['aggregated_pin_data']['id'] :
320
            null;
321
    }
322
323
    /**
324
     * @param mixed $params
325
     * @return array
326
     */
327
    protected function getFeedRequestData($params = [])
328
    {
329
        return ['domain' => $params['source']];
330
    }
331
332
    /**
333
     * @param int|array $pinIds
334
     * @param int $boardId
335
     * @param string $editUrl
336
     * @return bool
337
     */
338
    protected function bulkEdit($pinIds, $boardId, $editUrl)
339
    {
340
        $pinIds = is_array($pinIds) ? $pinIds : [$pinIds];
341
342
        $data = [
343
            'board_id' => (string)$boardId,
344
            'pin_ids'  => $pinIds,
345
        ];
346
347
        return $this->execPostRequest($data, $editUrl);
348
    }
349
}
350