GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — development ( ac02f6...17a2df )
by Dirk
03:10
created

Request::execute()   C

Complexity

Conditions 12
Paths 36

Size

Total Lines 83
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 49
CRAP Score 12.1143

Importance

Changes 20
Bugs 9 Features 10
Metric Value
c 20
b 9
f 10
dl 0
loc 83
ccs 49
cts 54
cp 0.9074
rs 5.034
cc 12
eloc 48
nc 36
nop 4
crap 12.1143

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Copyright 2015 Dirk Groenen
4
 *
5
 * (c) Dirk Groenen <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace DirkGroenen\Pinterest\Transport;
12
13
use DirkGroenen\Pinterest\Utils\CurlBuilder;
14
use DirkGroenen\Pinterest\Exceptions\PinterestException;
15
16
class Request {
17
18
    /**
19
     * Host to make the calls to
20
     *
21
     * @var string
22
     */
23
    private $host = "https://api.pinterest.com/v1/";
24
25
    /**
26
     * Access token
27
     *
28
     * @var string
29
     */
30
    protected $access_token = null;
31
32
    /**
33
     * Instance of the CurlBuilder class
34
     *
35
     * @var CurlBuilder
36
     */
37
    private $curlbuilder;
38
39
    /**
40
     * Array with the headers from the last request
41
     *
42
     * @var array
43
     */
44
    private $headers;
45
46
    /**
47
     * Constructor
48
     *
49
     * @param  CurlBuilder   $curlbuilder
50
     */
51 39
    public function __construct(CurlBuilder $curlbuilder)
52
    {
53 39
        $this->curlbuilder = $curlbuilder;
54 39
    }
55
56
    /**
57
     * Set the access token
58
     *
59
     * @access public
60
     * @param  string   $token
61
     * @return void
62
     */
63 39
    public function setAccessToken($token)
64
    {
65 39
        $this->access_token = $token;
66 39
    }
67
68
    /**
69
     * Make a get request to the given endpoint
70
     *
71
     * @access public
72
     * @param  string   $endpoint
73
     * @param  array    $parameters
74
     * @return Response
75
     */
76 25 View Code Duplication
    public function get($endpoint, array $parameters = array())
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...
77
    {
78 25
        if (!empty($parameters)) {
79 5
            $path = sprintf("%s/?%s", $endpoint, http_build_query($parameters));
80 5
        } else {
81 20
            $path = $endpoint;
82
        }
83
84 25
        return $this->execute("GET", sprintf("%s%s", $this->host, $path));
85
    }
86
87
    /**
88
     * Make a post request to the given endpoint
89
     *
90
     * @access public
91
     * @param  string   $endpoint
92
     * @param  array    $parameters
93
     * @return Response
94
     */
95 3
    public function post($endpoint, array $parameters = array())
96
    {
97 3
        return $this->execute("POST", sprintf("%s%s", $this->host, $endpoint), $parameters);
98 1
    }
99
100
    /**
101
     * Make a delete request to the given endpoint
102
     *
103
     * @access public
104
     * @param  string   $endpoint
105
     * @param  array    $parameters
106
     * @return Response
107
     */
108 5
    public function delete($endpoint, array $parameters = array())
109
    {
110 5
        return $this->execute("DELETE", sprintf("%s%s", $this->host, $endpoint) . "/", $parameters);
111
    }
112
113
    /**
114
     * Make an update request to the given endpoint
115
     *
116
     * @access public
117
     * @param  string   $endpoint
118
     * @param  array    $parameters
119
     * @param  array    $queryparameters
120
     * @return Response
121
     */
122 2 View Code Duplication
    public function update($endpoint, array $parameters = array(), array $queryparameters = array())
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...
123
    {
124 2
        if (!empty($queryparameters)) {
125
            $path = sprintf("%s/?%s", $endpoint, http_build_query($queryparameters));
126
        } else {
127 2
            $path = $endpoint;
128
        }
129
130 2
        return $this->execute("PATCH", sprintf("%s%s", $this->host, $path), $parameters);
131
    }
132
133
    /**
134
     * Return the headers from the last request
135
     *
136
     * @return array
137
     */
138 2
    public function getHeaders()
139
    {
140 2
        return $this->headers;
141
    }
142
143
    /**
144
     * Execute the http request
145
     *
146
     * @access public
147
     * @param  string   $method
148
     * @param  string   $apiCall
149
     * @param  array    $parameters
150
     * @param  array    $headers
151
     * @return Response
152
     */
153 35
    public function execute($method, $apiCall, array $parameters = array(), $headers = array())
154
    {
155
        // Check if the access token needs to be added
156 35
        if ($this->access_token != null) {
157 35
            $headers = array_merge($headers, array(
158 35
                "Authorization: Bearer " . $this->access_token,
159
                //"Content-type: application/x-www-form-urlencoded; charset=UTF-8",
160
                "Content-Type:multipart/form-data"
161 35
            ));
162 35
        }
163
164
        // Setup CURL
165 35
        $ch = $this->curlbuilder->create();
166
167
        // Set default options
168 35
        $ch->setOptions(array(
169 35
            CURLOPT_URL             => $apiCall,
170 35
            CURLOPT_HTTPHEADER      => $headers,
171 35
            CURLOPT_CONNECTTIMEOUT  => 20,
172 35
            CURLOPT_TIMEOUT         => 90,
173 35
            CURLOPT_RETURNTRANSFER  => true,
174 35
            CURLOPT_SSL_VERIFYPEER  => false,
175 35
            CURLOPT_SSL_VERIFYHOST  => false,
176 35
            CURLOPT_HEADER          => false,
177 35
            CURLINFO_HEADER_OUT     => true
178 35
        ));
179
180
        switch ($method) {
181 35
            case 'POST':
182 3
                $ch->setOptions(array(
183 3
                    CURLOPT_CUSTOMREQUEST   => "POST",
184 3
                    CURLOPT_POST            => count($parameters),
185 3
                    CURLOPT_POSTFIELDS      => ($parameters)
186 3
                ));
187
188 3
                if (!class_exists("\CURLFile") && defined('CURLOPT_SAFE_UPLOAD')) {
189
                    $ch->setOption(CURLOPT_SAFE_UPLOAD, false);
190
                }
191 3
                elseif (class_exists("\CURLFile") && defined('CURLOPT_SAFE_UPLOAD')) {
192
                    $ch->setOption(CURLOPT_SAFE_UPLOAD, true);
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a false|string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
193
                }
194
195 3
                break;
196 32
            case 'DELETE':
197 5
                $ch->setOption(CURLOPT_CUSTOMREQUEST, "DELETE");
198 5
                break;
199 27
            case 'PATCH':
200 2
                $ch->setOptions(array(
201 2
                    CURLOPT_CUSTOMREQUEST   => "PATCH",
202 2
                    CURLOPT_POST            => count($parameters),
203 2
                    CURLOPT_POSTFIELDS      => http_build_query($parameters)
204 2
                ));
205 2
                break;
206 25
            default:
207 25
                $ch->setOption(CURLOPT_CUSTOMREQUEST, "GET");
208 25
                break;
209 25
        }
210
211
        // Execute request and catch response
212 35
        $response_data = $ch->execute();
213
214
        // Check if we have a valid response
215 35
        if (!$response_data || $ch->hasErrors()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $response_data of type false|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
216
            throw new PinterestException('Error: execute() - cURL error: ' . $ch->getErrors(), $ch->getErrorNumber());
217
        }
218
219
        // Initiate the response
220 35
        $response = new Response($response_data, $ch);
221
222
        // Check the response code
223 35
        if ($response->getResponseCode() >= 400) {
224 1
            throw new PinterestException('Pinterest error (code: ' . $response->getResponseCode() . ') with message: ' . $response->getMessage(), $response->getResponseCode());
225
        }
226
227
        // Get headers from last request
228 34
        $this->headers = $ch->getHeaders();
229
230
        // Close curl resource
231 34
        $ch->close();
232
233
        // Return the response
234 34
        return $response;
235
    }
236
237
}
238