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

PinHelper   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 9
c 3
b 0
f 0
lcom 1
cbo 1
dl 0
loc 143
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A createCommentRequest() 0 6 1
A createPinCreationRequest() 0 14 1
A createRepinRequest() 0 15 1
A createInfoRequest() 0 13 1
A createPinRequest() 0 13 1
A createSimplePinRequest() 0 6 1
A createPinRequestData() 0 9 2
A createPinIdRequest() 0 4 1
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/deleting comment pin
11
     *
12
     * @param int   $pinId
13
     * @param array $data
14
     * @return array
15
     */
16
    public static function createCommentRequest($pinId, $data)
17
    {
18
        $dataJson = self::createPinIdRequest($pinId, $data);
19
20
        return self::createPinRequestData($dataJson);
21
    }
22
23
    /**
24
     * Creates Pinterest API request for Pin creation
25
     *
26
     * @param string $description
27
     * @param string $imageUrl
28
     * @param int    $boardId
29
     * @return array
30
     */
31
    public static function createPinCreationRequest($imageUrl, $boardId, $description = "")
32
    {
33
        $dataJson = [
34
            "options" => [
35
                "method"      => "scraped",
36
                "description" => $description,
37
                "link"        => $imageUrl,
38
                "image_url"   => $imageUrl,
39
                "board_id"    => $boardId,
40
            ],
41
        ];
42
43
        return self::createPinRequestData($dataJson, "/pin/create/bookmarklet/?url=".urlencode($imageUrl));
44
    }
45
46
    /**
47
     * Creates Pinterest API request for Pin repin
48
     *
49
     * @param string $description
50
     * @param int    $repinId
51
     * @param int    $boardId
52
     * @return array
53
     */
54
    public static function createRepinRequest($repinId, $boardId, $description)
55
    {
56
        $dataJson = [
57
            "options" => [
58
                "board_id"    => $boardId,
59
                "description" => stripslashes($description),
60
                "link"        => stripslashes($repinId),
61
                "is_video"    => null,
62
                "pin_id"      => $repinId,
63
            ],
64
            "context" => [],
65
        ];
66
67
        return self::createPinRequestData($dataJson);
68
    }
69
70
    /**
71
     * Creates Pinterest API request to get Pin info
72
     *
73
     * @param int $pinId
74
     * @return array
75
     */
76
    public static function createInfoRequest($pinId)
77
    {
78
        $dataJson = [
79
            "options" => [
80
                "field_set_key" => "detailed",
81
                "id"            => $pinId,
82
                "pin_id"        => $pinId,
83
                "allow_stale"   => true,
84
            ],
85
        ];
86
87
        return self::createPinRequestData($dataJson);
88
    }
89
90
    /**
91
     * Creates common pin request data by PinId
92
     *
93
     * @param int    $pinId
94
     * @param string $template
95
     * @param array  $options
96
     * @return array
97
     */
98
    public static function createPinRequest($pinId, $template = 'id', $options = array())
99
    {
100
        $options = array_merge(
101
            ["$template" => $pinId], $options
102
        );
103
104
        $result = [
105
            "options" => $options,
106
            "context" => [],
107
        ];
108
109
        return $result;
110
    }
111
112
    /**
113
     * Creates simple Pin request by PinId (used by delete and like requests)
114
     *
115
     * @param int $pinId
116
     * @return array
117
     */
118
    public static function createSimplePinRequest($pinId)
119
    {
120
        $dataJson = self::createPinRequest($pinId);
121
122
        return self::createPinRequestData($dataJson);
123
    }
124
125
    /**
126
     * @param string|null $sourceUrl
127
     * @param array       $data
128
     * @return array
129
     */
130
    public static function createPinRequestData($data, $sourceUrl = null)
131
    {
132
        if ($sourceUrl === null) {
133
            reset($data);
134
            $sourceUrl = "/pin/".end($data["options"])."/";
135
        }
136
137
        return Request::createRequestData($data, $sourceUrl);
138
    }
139
140
    /**
141
     * @param       $pinId
142
     * @param array $options
143
     * @return array
144
     */
145
    public static function createPinIdRequest($pinId, $options = array())
146
    {
147
        return self::createPinRequest($pinId, 'pin_id', $options);
148
    }
149
}
150