Completed
Push — master ( cc9e5c...67ffd6 )
by Sergey
13:28 queued 10:12
created

Pins::getScope()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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