Completed
Push — master ( 932d72...8b424c )
by Sergey
03:15 queued 41s
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\Pagination;
9
use seregazhuk\PinterestBot\Helpers\UrlBuilder;
10
use seregazhuk\PinterestBot\Api\Traits\Searchable;
11
use seregazhuk\PinterestBot\Api\Traits\SendsMessages;
12
use seregazhuk\PinterestBot\Api\Traits\CanBeDeleted;
13
use seregazhuk\PinterestBot\Api\Traits\UploadsImages;
14
15
class Pins extends Provider
16
{
17
    use Searchable, CanBeDeleted, UploadsImages, HasFeed, 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
        'visualSimilar',
34
    ];
35
36
    protected $searchScope  = 'pins';
37
    protected $entityIdName = 'id';
38
39
    protected $messageEntityName = 'pin';
40
41
    protected $deleteUrl = UrlBuilder::RESOURCE_DELETE_PIN;
42
    
43
    /**
44
     * Likes pin with current ID.
45
     *
46
     * @param string $pinId
47
     *
48
     * @return bool
49
     */
50
    public function like($pinId)
51
    {
52
        return $this->likePinMethodCall($pinId, UrlBuilder::RESOURCE_LIKE_PIN);
53
    }
54
55
    /**
56
     * Removes your like from pin with current ID.
57
     *
58
     * @param string $pinId
59
     *
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
     *
75
     * @return array
76
     */
77
    public function create($imageUrl, $boardId, $description = '', $link = '')
78
    {
79
        // Upload image if first argument is not url
80
        if (!filter_var($imageUrl, FILTER_VALIDATE_URL)) {
81
            $imageUrl = $this->upload($imageUrl);
82
        }
83
84
        $requestOptions = [
85
            'method'      => 'scraped',
86
            'description' => $description,
87
            'link'        => empty($link) ? $imageUrl : $link,
88
            'image_url'   => $imageUrl,
89
            'board_id'    => $boardId,
90
        ];
91
92
        return $this
93
            ->execPostRequest($requestOptions, UrlBuilder::RESOURCE_CREATE_PIN, true)
94
            ->getResponseData();
95
    }
96
97
    /**
98
     * Edit pin by ID. You can move pin to a new board by setting this board id.
99
     *
100
     * @param int $pindId
101
     * @param string $description
102
     * @param string $link
103
     * @param int|null $boardId
104
     * @return bool
105
     */
106
    public function edit($pindId, $description = '', $link = '', $boardId = null)
107
    {
108
        $requestOptions = [
109
            'id'          => $pindId,
110
            'description' => $description,
111
            'link'        => $link,
112
            'board_id'    => $boardId,
113
        ];
114
115
        return $this->execPostRequest($requestOptions, UrlBuilder::RESOURCE_UPDATE_PIN);
116
    }
117
118
    /**
119
     * Moves pin to a new board
120
     *
121
     * @param int $pindId
122
     * @param int $boardId
123
     * @return bool
124
     */
125
    public function moveToBoard($pindId, $boardId)
126
    {
127
        return $this->edit($pindId, '', '', $boardId);
128
    }
129
    
130
    /**
131
     * Make a repin.
132
     *
133
     * @param int   $repinId
134
     * @param int   $boardId
135
     * @param string $description
136
     *
137
     * @return array
138
     */
139
    public function repin($repinId, $boardId, $description = '')
140
    {
141
        $requestOptions = [
142
            'board_id'    => $boardId,
143
            'description' => stripslashes($description),
144
            'link'        => stripslashes($repinId),
145
            'is_video'    => null,
146
            'pin_id'      => $repinId,
147
        ];
148
149
        return $this
150
            ->execPostRequest($requestOptions, UrlBuilder::RESOURCE_REPIN, true)
151
            ->getResponseData();
152
    }
153
154
    /**
155
     * Get information of a pin by PinID.
156
     *
157
     * @param string $pinId
158
     *
159
     * @return array|bool
160
     */
161
    public function info($pinId)
162
    {
163
        $requestOptions = [
164
            'id'            => $pinId,
165
            'field_set_key' => 'detailed',
166
        ];
167
168
        return $this->execGetRequest($requestOptions, UrlBuilder::RESOURCE_PIN_INFO);
169
    }
170
171
    /**
172
     * Get pins from a specific url. For example: https://pinterest.com/source/flickr.com/ will
173
     * return recent Pins from flickr.com
174
     *
175
     * @param string $source
176
     * @param int $limit
177
     * @return Pagination
178
     */
179
    public function fromSource($source, $limit = Pagination::DEFAULT_LIMIT)
180
    {
181
        $data = ['domain' => $source];
182
183
        return $this->getFeed($data, UrlBuilder::RESOURCE_DOMAIN_FEED, $limit);
184
    }
185
186
    /**
187
     * Get the latest pin activity with pagination.
188
     *
189
     * @param string $pinId
190
     * @param int $limit
191
     * @return Iterator|array
192
     */
193
    public function activity($pinId, $limit = Pagination::DEFAULT_LIMIT)
194
    {
195
        $aggregatedPinId = $this->getAggregatedPinId($pinId);
196
197
        if (is_null($aggregatedPinId)) return [];
198
199
        $data = ['aggregated_pin_data_id' => $aggregatedPinId];
200
201
        return $this->getFeed($data, UrlBuilder::RESOURCE_ACTIVITY, $limit);
202
    }
203
204
    /**
205
     * Get pins from user's feed
206
     *
207
     * @param int $limit
208
     * @return Pagination
209
     */
210
    public function feed($limit = Pagination::DEFAULT_LIMIT)
211
    {
212
        return $this->getFeed([], UrlBuilder::RESOURCE_USER_FEED, $limit);
213
    }
214
215
    /**
216
     * @param string $pinId
217
     * @param int $limit
218
     * @return Pagination
219
     */
220
    public function related($pinId, $limit = Pagination::DEFAULT_LIMIT)
221
    {
222
        return $this->getFeed(['pin' => $pinId], UrlBuilder::RESOURCE_RELATED_PINS, $limit);
223
    }
224
225
    /**
226
     * Copy pins to board
227
     *
228
     * @codeCoverageIgnore
229
     * @param array|string $pinIds
230
     * @param int $boardId
231
     * @return bool|Response
232
     */
233
    public function copy($pinIds, $boardId)
234
    {
235
        return $this->bulkEdit($pinIds, $boardId, UrlBuilder::RESOURCE_BULK_COPY);
236
    }
237
238
    /**
239
     * Delete pins from board.
240
     *
241
     * @codeCoverageIgnore
242
     * @param string|array $pinIds
243
     * @param int $boardId
244
     * @return bool
245
     */
246
    public function deleteFromBoard($pinIds, $boardId)
247
    {
248
        return $this->bulkEdit($pinIds, $boardId, UrlBuilder::RESOURCE_BULK_DELETE);
249
    }
250
251
    /**
252
     * @codeCoverageIgnore
253
     * Move pins to board
254
     *
255
     * @param string|array $pinIds
256
     * @param int $boardId
257
     * @return bool|Response
258
     */
259
    public function move($pinIds, $boardId)
260
    {
261
        return $this->bulkEdit($pinIds, $boardId, UrlBuilder::RESOURCE_BULK_MOVE);
262
    }
263
    
264
    /**
265
     * @param string $pinId
266
     * @param array $crop
267
     * @return array|bool
268
     */
269
    public function visualSimilar($pinId, array $crop = [])
270
    {
271
        $data = [
272
            'pin_id' => $pinId,
273
            'crop' => $crop ? : [
274
                "x" => 0.16,
275
                "y" => 0.16,
276
                "w" => 0.66,
277
                "h" => 0.66,
278
                "num_crop_actions" => 1
279
            ],
280
            'force_refresh' => true,
281
            'keep_duplicates' => false
282
        ];
283
284
        return $this->execGetRequest($data, UrlBuilder::RESOURCE_VISUAL_SIMILAR_PINS);
285
    }
286
287
    /**
288
     * Saves the pin original image to the specified path. On success
289
     * returns full path to saved image. Otherwise returns false.
290
     *
291
     * @param int $pinId
292
     * @param string $path
293
     * @return false|string
294
     */
295
    public function saveOriginalImage($pinId, $path)
296
    {
297
        $pinInfo = $this->info($pinId);
298
        if(!isset($pinInfo['images']['orig']['url'])) return false;
299
300
        $originalUrl = $pinInfo['images']['orig']['url'];
301
        $destination = $path . DIRECTORY_SEPARATOR . basename($originalUrl);
302
303
        file_put_contents($destination, file_get_contents($originalUrl));
304
305
        return $destination;
306
    }
307
    
308
    /**
309
     * Calls Pinterest API to like or unlike Pin by ID.
310
     *
311
     * @param string $pinId
312
     * @param string $resourceUrl
313
     *
314
     * @return bool
315
     */
316
    protected function likePinMethodCall($pinId, $resourceUrl)
317
    {
318
        return $this->execPostRequest(['pin_id' => $pinId], $resourceUrl);
319
    }
320
321
    /**
322
     * @param string $pinId
323
     * @return int|null
324
     */
325
    protected function getAggregatedPinId($pinId)
326
    {
327
        $pinInfo = $this->info($pinId);
328
329
        return isset($pinInfo['aggregated_pin_data']['id']) ?
330
            $pinInfo['aggregated_pin_data']['id'] :
331
            null;
332
    }
333
334
    /**
335
     * @param mixed $params
336
     * @return array
337
     */
338
    protected function getFeedRequestData($params = [])
339
    {
340
        return ['domain' => $params['source']];
341
    }
342
343
    /**
344
     * @param string|array $pinIds
345
     * @param int $boardId
346
     * @param string $editUrl
347
     * @return bool
348
     */
349
    protected function bulkEdit($pinIds, $boardId, $editUrl)
350
    {
351
        $pinIds = is_array($pinIds) ? $pinIds : [$pinIds];
352
353
        $data = [
354
            'board_id' => (string)$boardId,
355
            'pin_ids'  => $pinIds,
356
        ];
357
358
        return $this->execPostRequest($data, $editUrl);
359
    }
360
}
361