Completed
Push — master ( 118874...7ee575 )
by Sergey
07:34 queued 05:25
created

Pins::tried()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
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\Api\Response;
6
use seregazhuk\PinterestBot\Api\Traits\TryIt;
7
use seregazhuk\PinterestBot\Helpers\FileHelper;
8
use seregazhuk\PinterestBot\Helpers\Pagination;
9
use seregazhuk\PinterestBot\Helpers\UrlBuilder;
10
use seregazhuk\PinterestBot\Api\Traits\Searchable;
11
use seregazhuk\PinterestBot\Api\Traits\CanBeDeleted;
12
use seregazhuk\PinterestBot\Api\Traits\SendsMessages;
13
use seregazhuk\PinterestBot\Api\Providers\Core\EntityProvider;
14
15
class Pins extends EntityProvider
16
{
17
    use Searchable,
18
        CanBeDeleted,
19
        SendsMessages,
20
        TryIt;
21
22
    /**
23
     * @var array
24
     */
25
    protected $loginRequiredFor = [
26
        'like',
27
        'feed',
28
        'send',
29
        'copy',
30
        'move',
31
        'repin',
32
        'unLike',
33
        'create',
34
        'delete',
35
        'activity',
36
        'analytics',
37
        'visualSimilar',
38
        'tryIt',
39
        'editTryIt',
40
        'deleteTryIt',
41
    ];
42
43
    protected $searchScope  = 'pins';
44
    protected $entityIdName = 'id';
45
46
    protected $messageEntityName = 'pin';
47
48
    protected $deleteUrl = UrlBuilder::RESOURCE_DELETE_PIN;
49
    
50
    /**
51
     * Likes pin with current ID.
52
     * @param string $pinId
53
     * @return bool
54
     */
55
    public function like($pinId)
56
    {
57
        return $this->likePinMethodCall($pinId, UrlBuilder::RESOURCE_LIKE_PIN);
58
    }
59
60
    /**
61
     * Removes your like from pin with current ID.
62
     * @param string $pinId
63
     * @return bool
64
     */
65
    public function unLike($pinId)
66
    {
67
        return $this->likePinMethodCall($pinId, UrlBuilder::RESOURCE_UNLIKE_PIN);
68
    }
69
70
    /**
71
     * Create a pin. Returns created pin info.
72
     *
73
     * @param string $imageUrl
74
     * @param int    $boardId
75
     * @param string $description
76
     * @param string $link
77
     * @return array
78
     */
79
    public function create($imageUrl, $boardId, $description = '', $link = '')
80
    {
81
        // Upload image if first argument is not url
82
        if (!filter_var($imageUrl, FILTER_VALIDATE_URL)) {
83
            $imageUrl = $this->upload($imageUrl);
84
        }
85
86
        $requestOptions = [
87
            'method'      => 'scraped',
88
            'description' => $description,
89
            'link'        => empty($link) ? $imageUrl : $link,
90
            'image_url'   => $imageUrl,
91
            'board_id'    => $boardId,
92
        ];
93
94
        $this->post($requestOptions, UrlBuilder::RESOURCE_CREATE_PIN);
95
96
        return $this->response->getResponseData();
97
    }
98
99
    /**
100
     * Edit pin by ID. You can move pin to a new board by setting this board id.
101
     *
102
     * @param int $pindId
103
     * @param string $description
104
     * @param string $link
105
     * @param int|null $boardId
106
     * @return bool
107
     */
108
    public function edit($pindId, $description = '', $link = '', $boardId = null)
109
    {
110
        $requestOptions = [
111
            'id'          => $pindId,
112
            'description' => stripslashes($description),
113
114
        ];
115
116
        if (!is_null($boardId)) $requestOptions['board_id'] = $boardId;
117
118
        if (!empty($link)) $requestOptions['link'] = stripslashes($link);
119
120
        return $this->post($requestOptions, UrlBuilder::RESOURCE_UPDATE_PIN);
121
    }
122
123
    /**
124
     * Moves pin to a new board
125
     *
126
     * @param int $pinId
127
     * @param int $boardId
128
     * @return bool
129
     */
130
    public function moveToBoard($pinId, $boardId)
131
    {
132
        return $this->edit($pinId, '', '', $boardId);
133
    }
134
    
135
    /**
136
     * Make a repin.
137
     *
138
     * @param int   $repinId
139
     * @param int   $boardId
140
     * @param string $description
141
     * @return array
142
     */
143
    public function repin($repinId, $boardId, $description = '')
144
    {
145
        $requestOptions = [
146
            'board_id'    => $boardId,
147
            'description' => stripslashes($description),
148
            'link'        => '',
149
            'is_video'    => null,
150
            'pin_id'      => $repinId,
151
        ];
152
153
        $this->post($requestOptions, UrlBuilder::RESOURCE_REPIN);
154
155
        return $this->response->getResponseData();
156
    }
157
158
    /**
159
     * Get information of a pin by PinID.
160
     *
161
     * @param string $pinId
162
     * @return array|bool
163
     */
164
    public function info($pinId)
165
    {
166
        $requestOptions = [
167
            'id'            => $pinId,
168
            'field_set_key' => 'detailed',
169
        ];
170
171
        return $this->get($requestOptions, UrlBuilder::RESOURCE_PIN_INFO);
172
    }
173
174
    /**
175
     * Get pins from a specific url. For example: https://pinterest.com/source/flickr.com/ will
176
     * return recent Pins from flickr.com
177
     *
178
     * @param string $source
179
     * @param int $limit
180
     * @return Pagination
181
     */
182
    public function fromSource($source, $limit = Pagination::DEFAULT_LIMIT)
183
    {
184
        $data = ['domain' => $source];
185
186
        return $this->paginate($data, UrlBuilder::RESOURCE_DOMAIN_FEED, $limit);
187
    }
188
189
    /**
190
     * Get the latest pin activity with pagination.
191
     *
192
     * @param string $pinId
193
     * @param int $limit
194
     * @return Pagination
195
     */
196
    public function activity($pinId, $limit = Pagination::DEFAULT_LIMIT)
197
    {
198
        return $this->getAggregatedActivity($pinId, [], $limit);
199
    }
200
201
    /**
202
     * @param string $pinId
203
     * @param array $additionalData
204
     * @param int $limit
205
     * @return Pagination
206
     */
207
    protected function getAggregatedActivity($pinId, $additionalData = [], $limit)
208
    {
209
        $aggregatedPinId = $this->getAggregatedPinId($pinId);
210
211
        if (is_null($aggregatedPinId)) return new Pagination();
212
213
        $additionalData['aggregated_pin_data_id'] = $aggregatedPinId;
214
215
        return $this->paginate($additionalData, UrlBuilder::RESOURCE_ACTIVITY, $limit);
216
    }
217
218
    /**
219
     * Get pins from user's feed
220
     *
221
     * @param int $limit
222
     * @return Pagination
223
     */
224
    public function feed($limit = Pagination::DEFAULT_LIMIT)
225
    {
226
        return $this->paginate([], UrlBuilder::RESOURCE_USER_FEED, $limit);
227
    }
228
229
    /**
230
     * @param string $pinId
231
     * @param int $limit
232
     * @return Pagination
233
     */
234
    public function related($pinId, $limit = Pagination::DEFAULT_LIMIT)
235
    {
236
        $requestData = [
237
            'pin'      => $pinId,
238
            'add_vase' => true,
239
        ];
240
        
241
        return $this->paginate($requestData, UrlBuilder::RESOURCE_RELATED_PINS, $limit);
242
    }
243
244
    /**
245
     * Copy pins to board
246
     *
247
     * @param array|string $pinIds
248
     * @param int $boardId
249
     * @return bool|Response
250
     */
251
    public function copy($pinIds, $boardId)
252
    {
253
        return $this->bulkEdit($pinIds, $boardId, UrlBuilder::RESOURCE_BULK_COPY);
254
    }
255
256
    /**
257
     * Delete pins from board.
258
     *
259
     * @param string|array $pinIds
260
     * @param int $boardId
261
     * @return bool
262
     */
263
    public function deleteFromBoard($pinIds, $boardId)
264
    {
265
        return $this->bulkEdit($pinIds, $boardId, UrlBuilder::RESOURCE_BULK_DELETE);
266
    }
267
268
    /**
269
     * Move pins to board
270
     *
271
     * @param string|array $pinIds
272
     * @param int $boardId
273
     * @return bool|Response
274
     */
275
    public function move($pinIds, $boardId)
276
    {
277
        return $this->bulkEdit($pinIds, $boardId, UrlBuilder::RESOURCE_BULK_MOVE);
278
    }
279
    
280
    /**
281
     * @param string $pinId
282
     * @param array $crop
283
     * @return array|bool
284
     */
285
    public function visualSimilar($pinId, array $crop = [])
286
    {
287
        $data = [
288
            'pin_id' => $pinId,
289
            'crop' => $crop ? : [
290
                "x" => 0.16,
291
                "y" => 0.16,
292
                "w" => 0.66,
293
                "h" => 0.66,
294
                "num_crop_actions" => 1
295
            ],
296
            'force_refresh' => true,
297
            'keep_duplicates' => false
298
        ];
299
300
        return $this->get($data, UrlBuilder::RESOURCE_VISUAL_SIMILAR_PINS);
301
    }
302
303
    /**
304
     * Saves the pin original image to the specified path. On success
305
     * returns full path to saved image. Otherwise returns false.
306
     *
307
     * @param string $pinId
308
     * @param string $path
309
     * @return false|string
310
     */
311
    public function saveOriginalImage($pinId, $path)
312
    {
313
        $pinInfo = $this->info($pinId);
314
        if(!isset($pinInfo['images']['orig']['url'])) return false;
315
316
        $originalUrl = $pinInfo['images']['orig']['url'];
317
        $destination = $path . DIRECTORY_SEPARATOR . basename($originalUrl);
318
319
        FileHelper::saveTo($originalUrl, $destination);
320
321
        return $destination;
322
    }
323
324
    /**
325
     * @param string $query
326
     * @param int $limit
327
     * @return Pagination
328
     */
329
    public function searchInMyPins($query, $limit = Pagination::DEFAULT_LIMIT)
330
    {
331
        return $this->paginateCustom(function() use ($query) {
332
                return $this->execSearchRequest($query, 'my_pins');
333
            })->take($limit);
334
    }
335
336
    /**
337
     * Returns trending pins from http://pinterest.com/discover page. Uses topic id, that can be received
338
     * from $bot->topics->explore() method.
339
     *
340
     * @param string $topicId
341
     * @param int $limit
342
     * @return Pagination
343
     */
344
    public function explore($topicId, $limit = Pagination::DEFAULT_LIMIT)
345
    {
346
        $data = [
347
            "aux_fields" => [],
348
            "prepend"    => false,
349
            "offset"     => 180,
350
            "section_id" => $topicId,
351
        ];
352
353
        return $this->paginate($data, UrlBuilder::RESOURCE_EXPLORE_PINS, $limit);
354
    }
355
356
    /**
357
     * Get pin analytics, like numbers of clicks, views and repins
358
     * @param $pinId
359
     * @return array|bool|Response
360
     */
361
    public function analytics($pinId)
362
    {
363
        return $this->get(['pin_id' => $pinId], UrlBuilder::RESOURCE_PIN_ANALYTICS);
364
    }
365
    
366
    /**
367
     * Calls Pinterest API to like or unlike Pin by ID.
368
     *
369
     * @param string $pinId
370
     * @param string $resourceUrl
371
     * @return bool
372
     */
373
    protected function likePinMethodCall($pinId, $resourceUrl)
374
    {
375
        return $this->post(['pin_id' => $pinId], $resourceUrl);
376
    }
377
378
    /**
379
     * @param string $pinId
380
     * @return int|null
381
     */
382
    protected function getAggregatedPinId($pinId)
383
    {
384
        $pinInfo = $this->info($pinId);
385
386
        return isset($pinInfo['aggregated_pin_data']['id']) ?
387
            $pinInfo['aggregated_pin_data']['id'] :
388
            null;
389
    }
390
391
    /**
392
     * @param mixed $params
393
     * @return array
394
     */
395
    protected function getFeedRequestData($params = [])
396
    {
397
        return ['domain' => $params['source']];
398
    }
399
400
    /**
401
     * @param string|array $pinIds
402
     * @param int $boardId
403
     * @param string $editUrl
404
     * @return bool
405
     */
406
    protected function bulkEdit($pinIds, $boardId, $editUrl)
407
    {
408
        $pinIds = is_array($pinIds) ? $pinIds : [$pinIds];
409
410
        $data = [
411
            'board_id' => $boardId,
412
            'pin_ids'  => $pinIds,
413
        ];
414
415
        return $this->post($data, $editUrl);
416
    }
417
}
418