Completed
Push — master ( e9184f...773d4d )
by Sergey
08:29 queued 05:20
created

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