Completed
Push — master ( 36381a...2be4bb )
by Sergey
50s
created

Pins::repin()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 10
nc 1
nop 3
1
<?php
2
3
namespace seregazhuk\PinterestBot\Api\Providers;
4
5
use Iterator;
6
use seregazhuk\PinterestBot\Helpers\UrlBuilder;
7
use seregazhuk\PinterestBot\Api\Traits\Searchable;
8
use seregazhuk\PinterestBot\Api\Traits\CanBeDeleted;
9
use seregazhuk\PinterestBot\Api\Traits\UploadsImages;
10
11
class Pins extends Provider
12
{
13
    use Searchable, CanBeDeleted, UploadsImages;
14
15
    protected $loginRequiredFor = [
16
        'like',
17
        'unLike',
18
        'comment',
19
        'deleteComment',
20
        'create',
21
        'repin',
22
        'delete',
23
        'activity',
24
        'userFeed'
25
    ];
26
27
    protected $searchScope  = 'pins';
28
    protected $entityIdName = 'id';
29
30
    protected $deleteUrl = UrlBuilder::RESOURCE_DELETE_PIN;
31
    
32
    /**
33
     * Likes pin with current ID.
34
     *
35
     * @param int $pinId
36
     *
37
     * @return bool
38
     */
39
    public function like($pinId)
40
    {
41
        return $this->likePinMethodCall($pinId, UrlBuilder::RESOURCE_LIKE_PIN);
42
    }
43
44
    /**
45
     * Removes your like from pin with current ID.
46
     *
47
     * @param int $pinId
48
     *
49
     * @return bool
50
     */
51
    public function unLike($pinId)
52
    {
53
        return $this->likePinMethodCall($pinId, UrlBuilder::RESOURCE_UNLIKE_PIN);
54
    }
55
56
    /**
57
     * Write a comment for a pin with current id.
58
     *
59
     * @param int    $pinId
60
     * @param string $text  Comment
61
     *
62
     * @return array|bool
63
     */
64
    public function comment($pinId, $text)
65
    {
66
        $requestOptions = ['pin_id' => (string)$pinId, 'text' => $text];
67
68
        return $this
69
            ->execPostRequest($requestOptions, UrlBuilder::RESOURCE_COMMENT_PIN, true)
70
            ->isOk();
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 View Code Duplication
    public function fromSource($source, $limit = 0)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
201
    {
202
        $params = [
203
            'data' => ['domain' => $source],
204
            'url'  => UrlBuilder::RESOURCE_DOMAIN_FEED,
205
        ];
206
207
        return $this->getPaginatedResponse($params, $limit);
208
    }
209
210
    /**
211
     * Get the latest pin activity with pagination.
212
     *
213
     * @param int $pinId
214
     * @param int $limit
215
     * @return Iterator|null
216
     */
217
    public function activity($pinId, $limit = 0)
218
    {
219
        if (!$aggregatedPinId = $this->getAggregatedPinId($pinId)) {
220
            return null;
221
        }
222
223
        $params = [
224
            'data' => ['aggregated_pin_data_id' => $aggregatedPinId],
225
            'url'  => UrlBuilder::RESOURCE_ACTIVITY
226
        ];
227
228
        return $this->getPaginatedResponse($params, $limit);
229
    }
230
231
    /**
232
     * Get pins from user's feed
233
     *
234
     * @param int $limit
235
     * @return Iterator
236
     */
237 View Code Duplication
    public function userFeed($limit = 0)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
238
    {
239
        $params = [
240
            'data' => [],
241
            'url'  => UrlBuilder::RESOURCE_USER_FEED
242
        ];
243
244
        return $this->getPaginatedResponse($params, $limit);
245
    }
246
247
    /**
248
     * @param int $pinId
249
     * @param int $limit
250
     * @return mixed
251
     */
252 View Code Duplication
    public function getRelatedPins($pinId, $limit = 0)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
253
    {
254
        $params = [
255
            'data' => ['pin' => $pinId],
256
            'url'  => UrlBuilder::RESOURCE_RELATED_PINS
257
        ];
258
259
        return $this->getPaginatedResponse($params, $limit);
260
    }
261
262
    /**
263
     * Calls Pinterest API to like or unlike Pin by ID.
264
     *
265
     * @param int $pinId
266
     * @param string $resourceUrl
267
     *
268
     * @return bool
269
     */
270
    protected function likePinMethodCall($pinId, $resourceUrl)
271
    {
272
        return $this->execPostRequest(['pin_id' => $pinId], $resourceUrl);
273
    }
274
275
    /**
276
     * @param int $pinId
277
     * @return int|null
278
     */
279
    protected function getAggregatedPinId($pinId)
280
    {
281
        $pinInfo = $this->info($pinId);
282
283
        return isset($pinInfo['aggregated_pin_data']['id']) ?
284
            $pinInfo['aggregated_pin_data']['id'] :
285
            null;
286
    }
287
}
288