Completed
Pull Request — master (#99)
by Sergey
04:20 queued 01:47
created

Pins::getPaginatedData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 10
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 10
loc 10
rs 9.4285
cc 1
eloc 6
nc 1
nop 4
1
<?php
2
3
namespace seregazhuk\PinterestBot\Api\Providers;
4
5
use Iterator;
6
use seregazhuk\PinterestBot\Api\Request;
7
use seregazhuk\PinterestBot\Helpers\UrlHelper;
8
use seregazhuk\PinterestBot\Helpers\Pagination;
9
use seregazhuk\PinterestBot\Helpers\Providers\Traits\Searchable;
10
11
class Pins extends Provider
12
{
13
    use Searchable;
14
15
    protected $loginRequired = [
16
        'like',
17
        'unLike',
18
        'comment',
19
        'deleteComment',
20
        'create',
21
        'repin',
22
        'delete',
23
    ];
24
25
    /**
26
     * Likes pin with current ID.
27
     *
28
     * @param int $pinId
29
     *
30
     * @return bool
31
     */
32
    public function like($pinId)
33
    {
34
        return $this->likePinMethodCall($pinId, UrlHelper::RESOURCE_LIKE_PIN);
35
    }
36
37
    /**
38
     * Removes your like from pin with current ID.
39
     *
40
     * @param int $pinId
41
     *
42
     * @return bool
43
     */
44
    public function unLike($pinId)
45
    {
46
        return $this->likePinMethodCall($pinId, UrlHelper::RESOURCE_UNLIKE_PIN);
47
    }
48
49
    /**
50
     * Calls Pinterest API to like or unlike Pin by ID.
51
     *
52
     * @param int    $pinId
53
     * @param string $resourceUrl
54
     *
55
     * @return bool
56
     */
57
    protected function likePinMethodCall($pinId, $resourceUrl)
58
    {
59
        return $this->execPostRequest(['pin_id' => $pinId], $resourceUrl);
60
    }
61
62
    /**
63
     * Write a comment for a pin with current id.
64
     *
65
     * @param int    $pinId
66
     * @param string $text  Comment
67
     *
68
     * @return array|bool
69
     */
70
    public function comment($pinId, $text)
71
    {
72
        $requestOptions = ['pin_id' => $pinId, 'test' => $text];
73
74
        return $this->execPostRequest($requestOptions, UrlHelper::RESOURCE_COMMENT_PIN, true);
75
    }
76
77
    /**
78
     * Delete a comment for a pin with current id.
79
     *
80
     * @param int $pinId
81
     * @param int $commentId
82
     *
83
     * @return bool
84
     */
85
    public function deleteComment($pinId, $commentId)
86
    {
87
        $requestOptions = ['pin_id' => $pinId, 'comment_id' => $commentId];
88
89
        return $this->execPostRequest($requestOptions, UrlHelper::RESOURCE_COMMENT_DELETE_PIN);
90
    }
91
92
    /**
93
     * Create a pin. Returns created pin ID.
94
     *
95
     * @param string $imageUrl
96
     * @param int    $boardId
97
     * @param string $description
98
     * @param string $link
99
     *
100
     * @return bool|int
101
     */
102
    public function create($imageUrl, $boardId, $description = '', $link = '')
103
    {
104
        $requestOptions = [
105
            'method'      => 'scraped',
106
            'description' => $description,
107
            'link'        => empty($link) ? $imageUrl : $link,
108
            'image_url'   => $imageUrl,
109
            'board_id'    => $boardId,
110
        ];
111
112
        return $this->execPostRequest($requestOptions, UrlHelper::RESOURCE_CREATE_PIN, true);
113
    }
114
115
    /**
116
     * Edit pin by ID. You can move pin to a new board by setting this board id.
117
     *
118
     * @param int $pindId
119
     * @param string $description
120
     * @param string $link
121
     * @param int|null $boardId
122
     * @return mixed
123
     */
124
    public function edit($pindId, $description = '', $link = '', $boardId = null)
125
    {
126
        $requestOptions = [
127
            'id'          => $pindId,
128
            'description' => $description,
129
            'link'        => $link,
130
            'board_id'    => $boardId,
131
        ];
132
133
        return $this->execPostRequest($requestOptions, UrlHelper::RESOURCE_UPDATE_PIN, true);
134
    }
135
136
    /**
137
     * Moves pin to a new board
138
     *
139
     * @param int $pindId
140
     * @param int $boardId
141
     * @return mixed
142
     */
143
    public function moveToBoard($pindId, $boardId)
144
    {
145
        return $this->edit($pindId, '', '', $boardId);
146
    }
147
    
148
    /**
149
     * Make a repin.
150
     *
151
     * @param int    $repinId
152
     * @param int    $boardId
153
     * @param string $description
154
     *
155
     * @return bool|int
156
     */
157
    public function repin($repinId, $boardId, $description = '')
158
    {
159
        $requestOptions = [
160
            'board_id'    => $boardId,
161
            'description' => stripslashes($description),
162
            'link'        => stripslashes($repinId),
163
            'is_video'    => null,
164
            'pin_id'      => $repinId,
165
        ];
166
167
        return $this->execPostRequest($requestOptions, UrlHelper::RESOURCE_REPIN, true);
168
    }
169
170
    /**
171
     * Delete a pin.
172
     *
173
     * @param int $pinId
174
     *
175
     * @return bool
176
     */
177
    public function delete($pinId)
178
    {
179
        return $this->execPostRequest(['id' => $pinId], UrlHelper::RESOURCE_DELETE_PIN);
180
    }
181
182
    /**
183
     * Get information of a pin by PinID.
184
     *
185
     * @param int $pinId
186
     *
187
     * @return array|bool
188
     */
189
    public function info($pinId)
190
    {
191
        $requestOptions = [
192
            'field_set_key' => 'detailed',
193
            'id'            => $pinId,
194
            'pin_id'        => $pinId,
195
            'allow_stale'   => true,
196
        ];
197
198
        $data = ['options' => $requestOptions];
199
        $url = UrlHelper::RESOURCE_PIN_INFO.'?'.Request::createQuery($data);
200
        $response = $this->request->exec($url);
201
202
        return $this->response->checkResponse($response);
203
    }
204
205
    /**
206
     * Get pins from a specific url. For example: https://pinterest.com/source/flickr.com/ will return
207
     * recent Pins from flickr.com
208
     *
209
     * @param string $source
210
     * @param int $batchesLimit
211
     * @return Iterator
212
     */
213
    public function fromSource($source, $batchesLimit = 0)
214
    {
215
        $params = [
216
            'data'      => ['domain' => $source],
217
            'url'       => UrlHelper::RESOURCE_DOMAIN_FEED,
218
            'sourceUrl' => "/source/$source/",
219
        ];
220
221
        return (new Pagination($this))->run('getPaginatedData', $params, $batchesLimit);
222
    }
223
    
224
    /**
225
     * @return string
226
     */
227
    protected function getScope()
228
    {
229
        return 'pins';
230
    }
231
}
232