Completed
Pull Request — master (#128)
by Sergey
03:15
created

Pins::getAggregatedPinId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace seregazhuk\PinterestBot\Api\Providers;
4
5
use Iterator;
6
use seregazhuk\PinterestBot\Api\Traits\UploadsImages;
7
use seregazhuk\PinterestBot\Helpers\UrlHelper;
8
use seregazhuk\PinterestBot\Helpers\Pagination;
9
use seregazhuk\PinterestBot\Api\Traits\Searchable;
10
use seregazhuk\PinterestBot\Api\Traits\CanBeDeleted;
11
12
class Pins extends Provider
13
{
14
    use Searchable, CanBeDeleted, UploadsImages;
15
16
    protected $loginRequiredFor = [
17
        'like',
18
        'unLike',
19
        'comment',
20
        'deleteComment',
21
        'create',
22
        'repin',
23
        'delete',
24
        'activity'
25
    ];
26
27
    protected $searchScope  = 'pins';
28
    protected $entityIdName = 'id';
29
30
    protected $deleteUrl = UrlHelper::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, UrlHelper::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, UrlHelper::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' => $pinId, 'test' => $text];
67
68
        return $this->execPostRequest($requestOptions, UrlHelper::RESOURCE_COMMENT_PIN, true);
69
    }
70
71
    /**
72
     * Delete a comment for a pin with current id.
73
     *
74
     * @param int $pinId
75
     * @param int $commentId
76
     *
77
     * @return bool
78
     */
79
    public function deleteComment($pinId, $commentId)
80
    {
81
        $requestOptions = ['pin_id' => $pinId, 'comment_id' => $commentId];
82
83
        return $this->execPostRequest($requestOptions, UrlHelper::RESOURCE_COMMENT_DELETE_PIN);
84
    }
85
86
    /**
87
     * Create a pin. Returns created pin ID.
88
     *
89
     * @param string $imageUrl
90
     * @param int    $boardId
91
     * @param string $description
92
     * @param string $link
93
     *
94
     * @return bool|int
95
     */
96
    public function create($imageUrl, $boardId, $description = '', $link = '')
97
    {
98
        // Upload image if first argument is not url
99
        if (!filter_var($imageUrl, FILTER_VALIDATE_URL)) {
100
            $imageUrl = $this->upload($imageUrl);
101
        }
102
103
        $requestOptions = [
104
            'method'      => 'scraped',
105
            'description' => $description,
106
            'link'        => empty($link) ? $imageUrl : $link,
107
            'image_url'   => $imageUrl,
108
            'board_id'    => $boardId,
109
        ];
110
111
        return $this->execPostRequest($requestOptions, UrlHelper::RESOURCE_CREATE_PIN, true);
112
    }
113
114
    /**
115
     * Edit pin by ID. You can move pin to a new board by setting this board id.
116
     *
117
     * @param int $pindId
118
     * @param string $description
119
     * @param string $link
120
     * @param int|null $boardId
121
     * @return bool
122
     */
123
    public function edit($pindId, $description = '', $link = '', $boardId = null)
124
    {
125
        $requestOptions = [
126
            'id'          => $pindId,
127
            'description' => $description,
128
            'link'        => $link,
129
            'board_id'    => $boardId,
130
        ];
131
132
        return $this->execPostRequest($requestOptions, UrlHelper::RESOURCE_UPDATE_PIN);
133
    }
134
135
    /**
136
     * Moves pin to a new board
137
     *
138
     * @param int $pindId
139
     * @param int $boardId
140
     * @return bool
141
     */
142
    public function moveToBoard($pindId, $boardId)
143
    {
144
        return $this->edit($pindId, '', '', $boardId);
145
    }
146
    
147
    /**
148
     * Make a repin.
149
     *
150
     * @param int    $repinId
151
     * @param int    $boardId
152
     * @param string $description
153
     *
154
     * @return bool|int
155
     */
156
    public function repin($repinId, $boardId, $description = '')
157
    {
158
        $requestOptions = [
159
            'board_id'    => $boardId,
160
            'description' => stripslashes($description),
161
            'link'        => stripslashes($repinId),
162
            'is_video'    => null,
163
            'pin_id'      => $repinId,
164
        ];
165
166
        return $this->execPostRequest($requestOptions, UrlHelper::RESOURCE_REPIN, true);
167
    }
168
169
    /**
170
     * Get information of a pin by PinID.
171
     *
172
     * @param int $pinId
173
     *
174
     * @return array|bool
175
     */
176
    public function info($pinId)
177
    {
178
        $requestOptions = [
179
            'id'            => $pinId,
180
            'field_set_key' => 'detailed',
181
        ];
182
183
        return $this->execGetRequest($requestOptions, UrlHelper::RESOURCE_PIN_INFO);
184
    }
185
186
    /**
187
     * Get pins from a specific url. For example: https://pinterest.com/source/flickr.com/ will return
188
     * recent Pins from flickr.com
189
     *
190
     * @param string $source
191
     * @param int $limit
192
     * @return Iterator
193
     */
194
    public function fromSource($source, $limit = 0)
195
    {
196
        $params = [
197
            'data' => ['domain' => $source],
198
            'url'  => UrlHelper::RESOURCE_DOMAIN_FEED,
199
        ];
200
201
        return (new Pagination($this))->paginateOver('getPaginatedData', $params, $limit);
202
    }
203
204
    /**
205
     * Get the latest pin activity with pagination.
206
     *
207
     * @param int $pinId
208
     * @param int $limit
209
     * @return Iterator|null
210
     */
211
    public function activity($pinId, $limit = 0)
212
    {
213
        if (!$aggregatedPinId = $this->getAggregatedPinId($pinId)) {
214
            return null;
215
        }
216
217
        $params = [
218
            'data' => ['aggregated_pin_data_id' => $aggregatedPinId],
219
            'url'  => UrlHelper::RESOURCE_ACTIVITY
220
        ];
221
222
        return (new Pagination($this))->paginateOver('getPaginatedData', $params, $limit);
223
    }
224
225
    /**
226
     * Calls Pinterest API to like or unlike Pin by ID.
227
     *
228
     * @param int $pinId
229
     * @param string $resourceUrl
230
     *
231
     * @return bool
232
     */
233
    protected function likePinMethodCall($pinId, $resourceUrl)
234
    {
235
        return $this->execPostRequest(['pin_id' => $pinId], $resourceUrl);
236
    }
237
238
    /**
239
     * @param int $pinId
240
     * @return int|null
241
     */
242
    protected function getAggregatedPinId($pinId)
243
    {
244
        $pinInfo = $this->info($pinId);
245
246
        return isset($pinInfo['aggregated_pin_data']['id']) ?
247
            $pinInfo['aggregated_pin_data']['id'] :
248
            null;
249
    }
250
}
251