Completed
Push — master ( 5058c9...c36a93 )
by Sergey
05:20 queued 02:29
created

Pins::getRelatedPins()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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