Completed
Pull Request — master (#209)
by Sergey
03:41
created

Pins::related()   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
        'feed',
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
     * Create a pin. Returns created pin info.
60
     *
61
     * @param string $imageUrl
62
     * @param int    $boardId
63
     * @param string $description
64
     * @param string $link
65
     *
66
     * @return array
67
     */
68
    public function create($imageUrl, $boardId, $description = '', $link = '')
69
    {
70
        // Upload image if first argument is not url
71
        if (!filter_var($imageUrl, FILTER_VALIDATE_URL)) {
72
            $imageUrl = $this->upload($imageUrl);
73
        }
74
75
        $requestOptions = [
76
            'method'      => 'scraped',
77
            'description' => $description,
78
            'link'        => empty($link) ? $imageUrl : $link,
79
            'image_url'   => $imageUrl,
80
            'board_id'    => $boardId,
81
        ];
82
83
        return $this
84
            ->execPostRequest($requestOptions, UrlBuilder::RESOURCE_CREATE_PIN, true)
85
            ->getResponseData();
86
    }
87
88
    /**
89
     * Edit pin by ID. You can move pin to a new board by setting this board id.
90
     *
91
     * @param int $pindId
92
     * @param string $description
93
     * @param string $link
94
     * @param int|null $boardId
95
     * @return bool
96
     */
97
    public function edit($pindId, $description = '', $link = '', $boardId = null)
98
    {
99
        $requestOptions = [
100
            'id'          => $pindId,
101
            'description' => $description,
102
            'link'        => $link,
103
            'board_id'    => $boardId,
104
        ];
105
106
        return $this->execPostRequest($requestOptions, UrlBuilder::RESOURCE_UPDATE_PIN);
107
    }
108
109
    /**
110
     * Moves pin to a new board
111
     *
112
     * @param int $pindId
113
     * @param int $boardId
114
     * @return bool
115
     */
116
    public function moveToBoard($pindId, $boardId)
117
    {
118
        return $this->edit($pindId, '', '', $boardId);
119
    }
120
    
121
    /**
122
     * Make a repin.
123
     *
124
     * @param int   $repinId
125
     * @param int   $boardId
126
     * @param string $description
127
     *
128
     * @return array
129
     */
130
    public function repin($repinId, $boardId, $description = '')
131
    {
132
        $requestOptions = [
133
            'board_id'    => $boardId,
134
            'description' => stripslashes($description),
135
            'link'        => stripslashes($repinId),
136
            'is_video'    => null,
137
            'pin_id'      => $repinId,
138
        ];
139
140
        return $this
141
            ->execPostRequest($requestOptions, UrlBuilder::RESOURCE_REPIN, true)
142
            ->getResponseData();
143
    }
144
145
    /**
146
     * Get information of a pin by PinID.
147
     *
148
     * @param int $pinId
149
     *
150
     * @return array|bool
151
     */
152
    public function info($pinId)
153
    {
154
        $requestOptions = [
155
            'id'            => $pinId,
156
            'field_set_key' => 'detailed',
157
        ];
158
159
        return $this->execGetRequest($requestOptions, UrlBuilder::RESOURCE_PIN_INFO);
160
    }
161
162
    /**
163
     * Get pins from a specific url. For example: https://pinterest.com/source/flickr.com/ will
164
     * return recent Pins from flickr.com
165
     *
166
     * @param string $source
167
     * @param int $limit
168
     * @return Iterator
169
     */
170
    public function fromSource($source, $limit = 0)
171
    {
172
        $data = ['domain' => $source];
173
174
        return $this->getFeed($data, UrlBuilder::RESOURCE_DOMAIN_FEED, $limit);
175
    }
176
177
    /**
178
     * Get the latest pin activity with pagination.
179
     *
180
     * @param int $pinId
181
     * @param int $limit
182
     * @return Iterator|null
183
     */
184
    public function activity($pinId, $limit = 0)
185
    {
186
        $aggregatedPinId = $this->getAggregatedPinId($pinId);
187
188
        if (is_null($aggregatedPinId)) return null;
189
190
        $data = ['aggregated_pin_data_id' => $aggregatedPinId];
191
192
        return $this->getFeed($data, UrlBuilder::RESOURCE_ACTIVITY, $limit);
193
    }
194
195
    /**
196
     * Get pins from user's feed
197
     *
198
     * @param int $limit
199
     * @return Iterator
200
     */
201
    public function feed($limit = 0)
202
    {
203
        return $this->getFeed([], UrlBuilder::RESOURCE_USER_FEED, $limit);
204
    }
205
206
    /**
207
     * @param int $pinId
208
     * @param int $limit
209
     * @return mixed
210
     */
211
    public function related($pinId, $limit = 0)
212
    {
213
        return $this->getFeed(['pin' => $pinId], UrlBuilder::RESOURCE_RELATED_PINS, $limit);
214
    }
215
216
    /**
217
     * @param int $pinId
218
     * @param array $crop
219
     * @return array|bool
220
     */
221
    public function visualSimilar($pinId, array $crop = [])
222
    {
223
        $data = [
224
            'pin_id' => $pinId,
225
            'crop' => $crop ? : [
226
                "x" => 0.16,
227
                "y" => 0.16,
228
                "w" => 0.66,
229
                "h" => 0.66,
230
                "num_crop_actions" => 1
231
            ],
232
            'force_refresh' => true,
233
            'keep_duplicates' => false
234
        ];
235
236
        return $this->execGetRequest($data, UrlBuilder::RESOURCE_VISUAL_SIMILAR_PINS);
237
    }
238
    
239
    /**
240
     * Calls Pinterest API to like or unlike Pin by ID.
241
     *
242
     * @param int $pinId
243
     * @param string $resourceUrl
244
     *
245
     * @return bool
246
     */
247
    protected function likePinMethodCall($pinId, $resourceUrl)
248
    {
249
        return $this->execPostRequest(['pin_id' => $pinId], $resourceUrl);
250
    }
251
252
    /**
253
     * @param int $pinId
254
     * @return int|null
255
     */
256
    protected function getAggregatedPinId($pinId)
257
    {
258
        $pinInfo = $this->info($pinId);
259
260
        return isset($pinInfo['aggregated_pin_data']['id']) ?
261
            $pinInfo['aggregated_pin_data']['id'] :
262
            null;
263
    }
264
265
    /**
266
     * @param mixed $params
267
     * @return array
268
     */
269
    protected function getFeedRequestData($params = [])
270
    {
271
        return ['domain' => $params['source']];
272
    }
273
}
274