Completed
Branch master (23259a)
by Sergey
04:17 queued 48s
created

Pins::saveOriginalImage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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