Completed
Push — master ( c6cee3...2c0312 )
by Sergey
03:00
created

Pins::bulkEdit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 3
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\CanBeDeleted;
11
use seregazhuk\PinterestBot\Api\Traits\UploadsImages;
12
13
class Pins extends Provider
14
{
15
    use Searchable, CanBeDeleted, UploadsImages, HasFeed;
16
17
    protected $loginRequiredFor = [
18
        'like',
19
        'unLike',
20
        'create',
21
        'repin',
22
        'copy',
23
        'move',
24
        'delete',
25
        'activity',
26
        'feed',
27
        'visualSimilar'
28
    ];
29
30
    protected $searchScope  = 'pins';
31
    protected $entityIdName = 'id';
32
33
    protected $deleteUrl = UrlBuilder::RESOURCE_DELETE_PIN;
34
    
35
    /**
36
     * Likes pin with current ID.
37
     *
38
     * @param int $pinId
39
     *
40
     * @return bool
41
     */
42
    public function like($pinId)
43
    {
44
        return $this->likePinMethodCall($pinId, UrlBuilder::RESOURCE_LIKE_PIN);
45
    }
46
47
    /**
48
     * Removes your like from pin with current ID.
49
     *
50
     * @param int $pinId
51
     *
52
     * @return bool
53
     */
54
    public function unLike($pinId)
55
    {
56
        return $this->likePinMethodCall($pinId, UrlBuilder::RESOURCE_UNLIKE_PIN);
57
    }
58
59
    /**
60
     * Create a pin. Returns created pin info.
61
     *
62
     * @param string $imageUrl
63
     * @param int    $boardId
64
     * @param string $description
65
     * @param string $link
66
     *
67
     * @return array
68
     */
69
    public function create($imageUrl, $boardId, $description = '', $link = '')
70
    {
71
        // Upload image if first argument is not url
72
        if (!filter_var($imageUrl, FILTER_VALIDATE_URL)) {
73
            $imageUrl = $this->upload($imageUrl);
74
        }
75
76
        $requestOptions = [
77
            'method'      => 'scraped',
78
            'description' => $description,
79
            'link'        => empty($link) ? $imageUrl : $link,
80
            'image_url'   => $imageUrl,
81
            'board_id'    => $boardId,
82
        ];
83
84
        return $this
85
            ->execPostRequest($requestOptions, UrlBuilder::RESOURCE_CREATE_PIN, true)
86
            ->getResponseData();
87
    }
88
89
    /**
90
     * Edit pin by ID. You can move pin to a new board by setting this board id.
91
     *
92
     * @param int $pindId
93
     * @param string $description
94
     * @param string $link
95
     * @param int|null $boardId
96
     * @return bool
97
     */
98
    public function edit($pindId, $description = '', $link = '', $boardId = null)
99
    {
100
        $requestOptions = [
101
            'id'          => $pindId,
102
            'description' => $description,
103
            'link'        => $link,
104
            'board_id'    => $boardId,
105
        ];
106
107
        return $this->execPostRequest($requestOptions, UrlBuilder::RESOURCE_UPDATE_PIN);
108
    }
109
110
    /**
111
     * Moves pin to a new board
112
     *
113
     * @param int $pindId
114
     * @param int $boardId
115
     * @return bool
116
     */
117
    public function moveToBoard($pindId, $boardId)
118
    {
119
        return $this->edit($pindId, '', '', $boardId);
120
    }
121
    
122
    /**
123
     * Make a repin.
124
     *
125
     * @param int   $repinId
126
     * @param int   $boardId
127
     * @param string $description
128
     *
129
     * @return array
130
     */
131
    public function repin($repinId, $boardId, $description = '')
132
    {
133
        $requestOptions = [
134
            'board_id'    => $boardId,
135
            'description' => stripslashes($description),
136
            'link'        => stripslashes($repinId),
137
            'is_video'    => null,
138
            'pin_id'      => $repinId,
139
        ];
140
141
        return $this
142
            ->execPostRequest($requestOptions, UrlBuilder::RESOURCE_REPIN, true)
143
            ->getResponseData();
144
    }
145
146
    /**
147
     * Get information of a pin by PinID.
148
     *
149
     * @param int $pinId
150
     *
151
     * @return array|bool
152
     */
153
    public function info($pinId)
154
    {
155
        $requestOptions = [
156
            'id'            => $pinId,
157
            'field_set_key' => 'detailed',
158
        ];
159
160
        return $this->execGetRequest($requestOptions, UrlBuilder::RESOURCE_PIN_INFO);
161
    }
162
163
    /**
164
     * Get pins from a specific url. For example: https://pinterest.com/source/flickr.com/ will
165
     * return recent Pins from flickr.com
166
     *
167
     * @param string $source
168
     * @param int $limit
169
     * @return Iterator
170
     */
171
    public function fromSource($source, $limit = 0)
172
    {
173
        $data = ['domain' => $source];
174
175
        return $this->getFeed($data, UrlBuilder::RESOURCE_DOMAIN_FEED, $limit);
176
    }
177
178
    /**
179
     * Get the latest pin activity with pagination.
180
     *
181
     * @param int $pinId
182
     * @param int $limit
183
     * @return Iterator|null
184
     */
185
    public function activity($pinId, $limit = 0)
186
    {
187
        $aggregatedPinId = $this->getAggregatedPinId($pinId);
188
189
        if (is_null($aggregatedPinId)) return null;
190
191
        $data = ['aggregated_pin_data_id' => $aggregatedPinId];
192
193
        return $this->getFeed($data, UrlBuilder::RESOURCE_ACTIVITY, $limit);
194
    }
195
196
    /**
197
     * Get pins from user's feed
198
     *
199
     * @param int $limit
200
     * @return Iterator
201
     */
202
    public function feed($limit = 0)
203
    {
204
        return $this->getFeed([], UrlBuilder::RESOURCE_USER_FEED, $limit);
205
    }
206
207
    /**
208
     * @param int $pinId
209
     * @param int $limit
210
     * @return mixed
211
     */
212
    public function related($pinId, $limit = 0)
213
    {
214
        return $this->getFeed(['pin' => $pinId], UrlBuilder::RESOURCE_RELATED_PINS, $limit);
215
    }
216
217
    /**
218
     * Copy pins to board
219
     *
220
     * @param array|int $pinIds
221
     * @param int $boardId
222
     * @return bool|Response
223
     */
224
    public function copy($pinIds, $boardId)
225
    {
226
        return $this->bulkEdit($pinIds, $boardId, UrlBuilder::RESOURCE_BULK_COPY);
227
    }
228
229
    /**
230
     * Delete pins from board.
231
     *
232
     * @param int|array $pinIds
233
     * @param int $boardId
234
     * @return bool
235
     */
236
    public function deleteFromBoard($pinIds, $boardId)
237
    {
238
        return $this->bulkEdit($pinIds, $boardId, UrlBuilder::RESOURCE_BULK_DELETE);
239
    }
240
241
    /**
242
     * Move pins to board
243
     *
244
     * @param int|array $pinIds
245
     * @param int $boardId
246
     * @return bool|Response
247
     */
248
    public function move($pinIds, $boardId)
249
    {
250
        return $this->bulkEdit($pinIds, $boardId, UrlBuilder::RESOURCE_BULK_MOVE);
251
    }
252
    
253
    /**
254
     * @param int $pinId
255
     * @param array $crop
256
     * @return array|bool
257
     */
258
    public function visualSimilar($pinId, array $crop = [])
259
    {
260
        $data = [
261
            'pin_id' => $pinId,
262
            'crop' => $crop ? : [
263
                "x" => 0.16,
264
                "y" => 0.16,
265
                "w" => 0.66,
266
                "h" => 0.66,
267
                "num_crop_actions" => 1
268
            ],
269
            'force_refresh' => true,
270
            'keep_duplicates' => false
271
        ];
272
273
        return $this->execGetRequest($data, UrlBuilder::RESOURCE_VISUAL_SIMILAR_PINS);
274
    }
275
    
276
    /**
277
     * Calls Pinterest API to like or unlike Pin by ID.
278
     *
279
     * @param int $pinId
280
     * @param string $resourceUrl
281
     *
282
     * @return bool
283
     */
284
    protected function likePinMethodCall($pinId, $resourceUrl)
285
    {
286
        return $this->execPostRequest(['pin_id' => $pinId], $resourceUrl);
287
    }
288
289
    /**
290
     * @param int $pinId
291
     * @return int|null
292
     */
293
    protected function getAggregatedPinId($pinId)
294
    {
295
        $pinInfo = $this->info($pinId);
296
297
        return isset($pinInfo['aggregated_pin_data']['id']) ?
298
            $pinInfo['aggregated_pin_data']['id'] :
299
            null;
300
    }
301
302
    /**
303
     * @param mixed $params
304
     * @return array
305
     */
306
    protected function getFeedRequestData($params = [])
307
    {
308
        return ['domain' => $params['source']];
309
    }
310
311
    /**
312
     * @param int|array $pinIds
313
     * @param int $boardId
314
     * @param string $editUrl
315
     * @return bool
316
     */
317
    protected function bulkEdit($pinIds, $boardId, $editUrl)
318
    {
319
        $pinIds = is_array($pinIds) ? $pinIds : [$pinIds];
320
321
        $data = [
322
            'board_id' => (string)$boardId,
323
            'pin_ids'  => $pinIds,
324
        ];
325
326
        return $this->execPostRequest($data, $editUrl);
327
    }
328
}
329