Completed
Push — master ( 1e0bdf...0e0b28 )
by Sergey
09:20 queued 10s
created

PinHelper::parsePinInfoResponse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 8
Ratio 100 %

Importance

Changes 6
Bugs 1 Features 1
Metric Value
c 6
b 1
f 1
dl 8
loc 8
rs 9.4286
cc 2
eloc 4
nc 2
nop 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A PinHelper::createSimplePinRequest() 0 5 1
1
<?php
2
3
namespace seregazhuk\PinterestBot\Helpers;
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
     * Checks result of PIN-methods
39
     *
40
     * @param array $res
41
     * @return bool
42
     */
43
    public static function checkMethodCallResult($res)
44
    {
45
        if ($res !== null && isset($res['resource_response'])) {
46
            return true;
47
        } else {
48
            return false;
49
        }
50
    }
51
52
    /**
53
     * Creates Pinterest API request for Pin creation
54
     *
55
     * @param string $description
56
     * @param string $imageUrl
57
     * @param int    $boardId
58
     * @return array
59
     */
60
    public static function createPinCreationRequest($imageUrl, $boardId, $description = "")
61
    {
62
        $dataJson = [
63
            "options" => [
64
                "method"      => "scraped",
65
                "description" => $description,
66
                "link"        => $imageUrl,
67
                "image_url"   => $imageUrl,
68
                "board_id"    => $boardId,
69
            ],
70
            "context" => new \stdClass(),
71
        ];
72
73
        return self::createPinRequestData($dataJson, "/pin/create/bookmarklet/?url=" . urlencode($imageUrl));
74
    }
75
76
77
    /**
78
     * Creates Pinterest API request for Pin repin
79
     *
80
     * @param string $description
81
     * @param int    $repinId
82
     * @param int    $boardId
83
     * @return array
84
     */
85
    public static function createRepinRequest($repinId, $boardId, $description)
86
    {
87
        $dataJson = [
88
            "options" => [
89
                "board_id"    => $boardId,
90
                "description" => stripslashes($description),
91
                "link"        => stripslashes($repinId),
92
                "is_video"    => null,
93
                "pin_id"      => $repinId,
94
            ],
95
            "context" => [],
96
        ];
97
98
        return self::createPinRequestData($dataJson);
99
    }
100
101
102
    /**
103
     * Creates Pinterest API request to get Pin info
104
     *
105
     * @param int $pinId
106
     * @return array
107
     */
108
    public static function createInfoRequest($pinId)
109
    {
110
        $dataJson = [
111
            "options" => [
112
                "field_set_key"                  => "detailed",
113
                "fetch_visualsearchCall_objects" => true,
114
                "id"                             => $pinId,
115
                "pin_id"                         => $pinId,
116
                "allow_stale"                    => true,
117
            ],
118
            "context" => new \StdClass(),
119
        ];
120
121
        return self::createPinRequestData($dataJson);
122
    }
123
124
    /**
125
     * Creates common pin request data by PinId
126
     *
127
     * @param int    $pinId
128
     * @param string $template
129
     * @return array
130
     */
131
    public static function createPinRequest($pinId, $template = 'id')
132
    {
133
        return [
134
            "options" => [
135
                "$template" => $pinId,
136
            ],
137
            "context" => [],
138
        ];
139
    }
140
141
    /**
142
     * Creates simple Pin request by PinId (used by delete and like requests)
143
     *
144
     * @param $pinId
145
     * @return array
146
     */
147
    public static function createSimplePinRequest($pinId)
148
    {
149
        $dataJson = self::createPinRequest($pinId);
150
        return self::createPinRequestData($dataJson);
151
    }
152
153
    /**
154
     * @param string|null $sourceUrl
155
     * @param array       $data
156
     * @return array
157
     */
158
    public static function createPinRequestData($data, $sourceUrl = null)
159
    {
160
        if ($sourceUrl === null) {
161
            reset($data);
162
            $sourceUrl = "/pin/" . end($data["options"]) . "/";
163
        }
164
165
        return self::createRequestData($data, $sourceUrl);
166
    }
167
}
168