Completed
Push — master ( 86625d...aab048 )
by Sergey
13:07 queued 10:25
created

Pins::edit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

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