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

PinHelper   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 154
Duplicated Lines 9.09 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 1
cbo 1
dl 14
loc 154
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A createCommentRequest() 7 7 1
A createCommentDeleteRequest() 7 7 1
A createPinCreationRequest() 0 14 1
A createRepinRequest() 0 15 1
A createInfoRequest() 0 13 1
A createPinRequest() 0 9 1
A createSimplePinRequest() 0 5 1
A createPinRequestData() 0 9 2
A createPinIdRequest() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 View Code Duplication
    public static function createCommentRequest($pinId, $text)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
17
    {
18
        $dataJson = self::createPinIdRequest($pinId);
19
        $dataJson["options"]["text"] = $text;
20
21
        return self::createPinRequestData($dataJson);
22
    }
23
24
    /**
25
     * Create Pinterest API request form commenting pin
26
     *
27
     * @param int $pinId
28
     * @param int $commentId
29
     * @return array
30
     */
31 View Code Duplication
    public static function createCommentDeleteRequest($pinId, $commentId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
32
    {
33
        $dataJson = self::createPinIdRequest($pinId);
34
        $dataJson["options"]["comment_id"] = $commentId;
35
36
        return self::createPinRequestData($dataJson);
37
    }
38
39
    /**
40
     * Creates Pinterest API request for Pin creation
41
     *
42
     * @param string $description
43
     * @param string $imageUrl
44
     * @param int    $boardId
45
     * @return array
46
     */
47
    public static function createPinCreationRequest($imageUrl, $boardId, $description = "")
48
    {
49
        $dataJson = [
50
            "options" => [
51
                "method"      => "scraped",
52
                "description" => $description,
53
                "link"        => $imageUrl,
54
                "image_url"   => $imageUrl,
55
                "board_id"    => $boardId,
56
            ],
57
        ];
58
59
        return self::createPinRequestData($dataJson, "/pin/create/bookmarklet/?url=" . urlencode($imageUrl));
60
    }
61
62
63
    /**
64
     * Creates Pinterest API request for Pin repin
65
     *
66
     * @param string $description
67
     * @param int    $repinId
68
     * @param int    $boardId
69
     * @return array
70
     */
71
    public static function createRepinRequest($repinId, $boardId, $description)
72
    {
73
        $dataJson = [
74
            "options" => [
75
                "board_id"    => $boardId,
76
                "description" => stripslashes($description),
77
                "link"        => stripslashes($repinId),
78
                "is_video"    => null,
79
                "pin_id"      => $repinId,
80
            ],
81
            "context" => [],
82
        ];
83
84
        return self::createPinRequestData($dataJson);
85
    }
86
87
88
    /**
89
     * Creates Pinterest API request to get Pin info
90
     *
91
     * @param int $pinId
92
     * @return array
93
     */
94
    public static function createInfoRequest($pinId)
95
    {
96
        $dataJson = [
97
            "options" => [
98
                "field_set_key"                  => "detailed",
99
                "id"                             => $pinId,
100
                "pin_id"                         => $pinId,
101
                "allow_stale"                    => true,
102
            ],
103
        ];
104
105
        return self::createPinRequestData($dataJson);
106
    }
107
108
    /**
109
     * Creates common pin request data by PinId
110
     *
111
     * @param int    $pinId
112
     * @param string $template
113
     * @return array
114
     */
115
    public static function createPinRequest($pinId, $template = 'id')
116
    {
117
        return [
118
            "options" => [
119
                "$template" => $pinId,
120
            ],
121
            "context" => [],
122
        ];
123
    }
124
125
    /**
126
     * Creates simple Pin request by PinId (used by delete and like requests)
127
     *
128
     * @param int $pinId
129
     * @return array
130
     */
131
    public static function createSimplePinRequest($pinId)
132
    {
133
        $dataJson = self::createPinRequest($pinId);
134
        return self::createPinRequestData($dataJson);
135
    }
136
137
    /**
138
     * @param string|null $sourceUrl
139
     * @param array       $data
140
     * @return array
141
     */
142
    public static function createPinRequestData($data, $sourceUrl = null)
143
    {
144
        if ($sourceUrl === null) {
145
            reset($data);
146
            $sourceUrl = "/pin/" . end($data["options"]) . "/";
147
        }
148
149
        return Request::createRequestData($data, $sourceUrl);
150
    }
151
152
    /**
153
     * @param $pinId
154
     * @return array
155
     */
156
    public static function createPinIdRequest($pinId)
157
    {
158
        return self::createPinRequest($pinId, 'pin_id');
159
    }
160
}
161