Completed
Push — master ( f77348...ad6f80 )
by Sergey
03:41
created

Pins::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 1
eloc 8
nc 1
nop 3
1
<?php
2
3
namespace seregazhuk\PinterestBot\Api\Providers;
4
5
use seregazhuk\PinterestBot\Api\Request;
6
use seregazhuk\PinterestBot\Helpers\UrlHelper;
7
use seregazhuk\PinterestBot\Helpers\Providers\Traits\SearchTrait;
8
9
class Pins extends Provider
10
{
11
    use SearchTrait;
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 integer $pinId
27
     * @return bool
28
     */
29
    public function like($pinId)
30
    {
31
        return $this->likePinMethodCall($pinId, UrlHelper::RESOURCE_LIKE_PIN);
32
    }
33
34
    /**
35
     * Removes your like from pin with current ID
36
     *
37
     * @param integer $pinId
38
     * @return bool
39
     */
40
    public function unLike($pinId)
41
    {
42
        return $this->likePinMethodCall($pinId, UrlHelper::RESOURCE_UNLIKE_PIN);
43
    }
44
45
    /**
46
     * Calls Pinterest API to like or unlike Pin by ID
47
     *
48
     * @param int    $pinId
49
     * @param string $resourceUrl
50
     * @return bool
51
     */
52
    protected function likePinMethodCall($pinId, $resourceUrl)
53
    {
54
        return $this->callPostRequest(['pin_id' => $pinId], $resourceUrl);
55
    }
56
57
    /**
58
     * Write a comment for a pin with current id
59
     *
60
     * @param integer $pinId
61
     * @param string  $text Comment
62
     * @return array|bool
63
     */
64
    public function comment($pinId, $text)
65
    {
66
        $requestOptions = ['pin_id' => $pinId, 'test' => $text];
67
68
        return $this->callPostRequest($requestOptions, UrlHelper::RESOURCE_COMMENT_PIN, true);
69
    }
70
71
    /**
72
     * Delete a comment for a pin with current id
73
     *
74
     * @param integer $pinId
75
     * @param integer $commentId
76
     * @return bool
77
     */
78
    public function deleteComment($pinId, $commentId)
79
    {
80
        $requestOptions = ["pin_id" => $pinId, "comment_id" => $commentId];
81
82
        return $this->callPostRequest($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
     * @return bool|int
92
     */
93
    public function create($imageUrl, $boardId, $description = "")
94
    {
95
        $requestOptions = [
96
            "method"      => "scraped",
97
            "description" => $description,
98
            "link"        => $imageUrl,
99
            "image_url"   => $imageUrl,
100
            "board_id"    => $boardId,
101
        ];
102
103
        return $this->callPostRequest($requestOptions, UrlHelper::RESOURCE_CREATE_PIN, true);
104
    }
105
106
    /**
107
     * Make a repin
108
     *
109
     * @param int    $repinId
110
     * @param int    $boardId
111
     * @param string $description
112
     * @return bool|int
113
     */
114
    public function repin($repinId, $boardId, $description = "")
115
    {
116
        $requestOptions = [
117
            "board_id"    => $boardId,
118
            "description" => stripslashes($description),
119
            "link"        => stripslashes($repinId),
120
            "is_video"    => null,
121
            "pin_id"      => $repinId,
122
        ];
123
124
        return $this->callPostRequest($requestOptions, UrlHelper::RESOURCE_REPIN, true);
125
    }
126
127
    /**
128
     * Delete a pin
129
     *
130
     * @param int $pinId
131
     * @return bool
132
     */
133
    public function delete($pinId)
134
    {
135
        return $this->callPostRequest(['id' => $pinId], UrlHelper::RESOURCE_DELETE_PIN);
136
    }
137
138
    /**
139
     * Get information of a pin by PinID
140
     *
141
     * @param int $pinId
142
     * @return array|bool
143
     */
144
    public function info($pinId)
145
    {
146
        $requestOptions = [
147
            "field_set_key" => "detailed",
148
            "id"            => $pinId,
149
            "pin_id"        => $pinId,
150
            "allow_stale"   => true
151
        ];
152
153
        $data = array("options" => $requestOptions);
154
        $url = UrlHelper::RESOURCE_PIN_INFO.'?'.Request::createQuery($data);
155
        $response = $this->request->exec($url);
156
157
        return $this->response->checkResponse($response);
158
    }
159
160
    /**
161
     * @return string
162
     */
163
    protected function getScope()
164
    {
165
        return 'pins';
166
    }
167
}
168