Completed
Push — master ( d0d267...c63bf4 )
by Sergey
06:03 queued 03:35
created

Pins::unLike()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
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);
230
    }
231
232
    /**
233
     * @codeCoverageIgnore
234
     * Delete pins from board.
235
     *
236
     * @param string|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|string $userIds
251
     * @param array|string $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 $userIds defined by parameter $userIds on line 254 can also be of type string; however, seregazhuk\PinterestBot\...ages::callSendMessage() does only seem to accept array|integer, 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
     * Send pin with messages.
263
     *
264
     * @param int $pinId
265
     * @param string $text
266
     * @param array|string $userIds
267
     * @return bool
268
     */
269
    public function sendWithMessage($pinId, $text, $userIds)
270
    {
271
        return $this->send($pinId, $text, $userIds, []);
272
    }
273
274
    /**
275
     * Send pin with emails.
276
     *
277
     * @param int $pinId
278
     * @param string $text
279
     * @param array|string $emails
280
     * @return bool
281
     */
282
    public function sendWithEmail($pinId, $text, $emails)
283
    {
284
        return $this->send($pinId, $text, [], $emails);
285
    }
286
287
    /**
288
     * @codeCoverageIgnore
289
     * Move pins to board
290
     *
291
     * @param string|array $pinIds
292
     * @param int $boardId
293
     * @return bool|Response
294
     */
295
    public function move($pinIds, $boardId)
296
    {
297
        return $this->bulkEdit($pinIds, $boardId, UrlBuilder::RESOURCE_BULK_MOVE);
298
    }
299
    
300
    /**
301
     * @param string $pinId
302
     * @param array $crop
303
     * @return array|bool
304
     */
305
    public function visualSimilar($pinId, array $crop = [])
306
    {
307
        $data = [
308
            'pin_id' => $pinId,
309
            'crop' => $crop ? : [
310
                "x" => 0.16,
311
                "y" => 0.16,
312
                "w" => 0.66,
313
                "h" => 0.66,
314
                "num_crop_actions" => 1
315
            ],
316
            'force_refresh' => true,
317
            'keep_duplicates' => false
318
        ];
319
320
        return $this->execGetRequest($data, UrlBuilder::RESOURCE_VISUAL_SIMILAR_PINS);
321
    }
322
323
    /**
324
     * Saves the pin original image to the specified path. On success
325
     * returns full path to saved image. Otherwise returns false.
326
     *
327
     * @param int $pinId
328
     * @param string $path
329
     * @return string|bool
330
     */
331
    public function saveOriginalImage($pinId, $path)
332
    {
333
        $pinInfo = $this->info($pinId);
334
        if(!isset($pinInfo['images']['orig']['url'])) return false;
335
336
        $originalUrl = $pinInfo['images']['orig']['url'];
337
        $destination = $path . DIRECTORY_SEPARATOR . basename($originalUrl);
338
339
        file_put_contents($destination, file_get_contents($originalUrl));
340
341
        return $destination;
342
    }
343
    
344
    /**
345
     * Calls Pinterest API to like or unlike Pin by ID.
346
     *
347
     * @param string $pinId
348
     * @param string $resourceUrl
349
     *
350
     * @return bool
351
     */
352
    protected function likePinMethodCall($pinId, $resourceUrl)
353
    {
354
        return $this->execPostRequest(['pin_id' => $pinId], $resourceUrl);
355
    }
356
357
    /**
358
     * @param string $pinId
359
     * @return int|null
360
     */
361
    protected function getAggregatedPinId($pinId)
362
    {
363
        $pinInfo = $this->info($pinId);
364
365
        return isset($pinInfo['aggregated_pin_data']['id']) ?
366
            $pinInfo['aggregated_pin_data']['id'] :
367
            null;
368
    }
369
370
    /**
371
     * @param mixed $params
372
     * @return array
373
     */
374
    protected function getFeedRequestData($params = [])
375
    {
376
        return ['domain' => $params['source']];
377
    }
378
379
    /**
380
     * @param string|array $pinIds
381
     * @param int $boardId
382
     * @param string $editUrl
383
     * @return bool
384
     */
385
    protected function bulkEdit($pinIds, $boardId, $editUrl)
386
    {
387
        $pinIds = is_array($pinIds) ? $pinIds : [$pinIds];
388
389
        $data = [
390
            'board_id' => (string)$boardId,
391
            'pin_ids'  => $pinIds,
392
        ];
393
394
        return $this->execPostRequest($data, $editUrl);
395
    }
396
}
397