Completed
Pull Request — master (#17)
by Sergey
04:53
created

PinHelper::createPinCreationRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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