Passed
Push — master ( d5d6b0...312b1e )
by Sergey
03:09
created

Pins::analytics()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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