Completed
Pull Request — master (#19)
by Sergey
03:23
created

PinHelper::createPinRequestData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 9
rs 9.6667
cc 2
eloc 5
nc 2
nop 2
1
<?php
2
3
namespace seregazhuk\PinterestBot\Helpers\Requests;
4
5
use seregazhuk\PinterestBot\Api\Request;
6
7
class PinHelper
8
{
9
    /**
10
     * Create Pinterest API request form commenting pin
11
     *
12
     * @param int    $pinId
13
     * @param string $text
14
     * @return array
15
     */
16
    public static function createCommentRequest($pinId, $text)
17
    {
18
        $dataJson = self::createPinIdRequest($pinId, ["text" => $text]);
19
20
        return self::createPinRequestData($dataJson);
21
    }
22
23
    /**
24
     * Create Pinterest API request form commenting pin
25
     *
26
     * @param int $pinId
27
     * @param int $commentId
28
     * @return array
29
     */
30
    public static function createCommentDeleteRequest($pinId, $commentId)
31
    {
32
        $dataJson = self::createPinIdRequest($pinId, ["comment_id" => $commentId]);
33
34
        return self::createPinRequestData($dataJson);
35
    }
36
37
    /**
38
     * Creates Pinterest API request for Pin creation
39
     *
40
     * @param string $description
41
     * @param string $imageUrl
42
     * @param int    $boardId
43
     * @return array
44
     */
45
    public static function createPinCreationRequest($imageUrl, $boardId, $description = "")
46
    {
47
        $dataJson = [
48
            "options" => [
49
                "method"      => "scraped",
50
                "description" => $description,
51
                "link"        => $imageUrl,
52
                "image_url"   => $imageUrl,
53
                "board_id"    => $boardId,
54
            ],
55
        ];
56
57
        return self::createPinRequestData($dataJson, "/pin/create/bookmarklet/?url=".urlencode($imageUrl));
58
    }
59
60
    /**
61
     * Creates Pinterest API request for Pin repin
62
     *
63
     * @param string $description
64
     * @param int    $repinId
65
     * @param int    $boardId
66
     * @return array
67
     */
68
    public static function createRepinRequest($repinId, $boardId, $description)
69
    {
70
        $dataJson = [
71
            "options" => [
72
                "board_id"    => $boardId,
73
                "description" => stripslashes($description),
74
                "link"        => stripslashes($repinId),
75
                "is_video"    => null,
76
                "pin_id"      => $repinId,
77
            ],
78
            "context" => [],
79
        ];
80
81
        return self::createPinRequestData($dataJson);
82
    }
83
84
    /**
85
     * Creates Pinterest API request to get Pin info
86
     *
87
     * @param int $pinId
88
     * @return array
89
     */
90
    public static function createInfoRequest($pinId)
91
    {
92
        $dataJson = [
93
            "options" => [
94
                "field_set_key" => "detailed",
95
                "id"            => $pinId,
96
                "pin_id"        => $pinId,
97
                "allow_stale"   => true,
98
            ],
99
        ];
100
101
        return self::createPinRequestData($dataJson);
102
    }
103
104
    /**
105
     * Creates common pin request data by PinId
106
     *
107
     * @param int    $pinId
108
     * @param string $template
109
     * @param array  $options
110
     * @return array
111
     */
112
    public static function createPinRequest($pinId, $template = 'id', $options = array())
113
    {
114
        $options = array_merge(
115
            ["$template" => $pinId],
116
            $options
117
        );
118
119
        $result = [
120
            "options" => $options,
121
            "context" => [],
122
        ];
123
124
        return $result;
125
    }
126
127
    /**
128
     * Creates simple Pin request by PinId (used by delete and like requests)
129
     *
130
     * @param int $pinId
131
     * @return array
132
     */
133
    public static function createSimplePinRequest($pinId)
134
    {
135
        $dataJson = self::createPinRequest($pinId);
136
137
        return self::createPinRequestData($dataJson);
138
    }
139
140
    /**
141
     * @param string|null $sourceUrl
142
     * @param array       $data
143
     * @return array
144
     */
145
    public static function createPinRequestData($data, $sourceUrl = null)
146
    {
147
        if ($sourceUrl === null) {
148
            reset($data);
149
            $sourceUrl = "/pin/".end($data["options"])."/";
150
        }
151
152
        return Request::createRequestData($data, $sourceUrl);
153
    }
154
155
    /**
156
     * @param       $pinId
157
     * @param array $options
158
     * @return array
159
     */
160
    public static function createPinIdRequest($pinId, $options = array())
161
    {
162
        return self::createPinRequest($pinId, 'pin_id', $options);
163
    }
164
}
165