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.

Request::get()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 10
loc 10
c 0
b 0
f 0
ccs 5
cts 5
cp 1
rs 9.9332
cc 2
nc 2
nop 2
crap 2
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
use DirkGroenen\Pinterest\Exceptions\CurlException;
16
17
class Request {
18
19
    /**
20
     * Host to make the calls to
21
     *
22
     * @var string
23
     */
24
    private $host = "https://api.pinterest.com/v1/";
25
26
    /**
27
     * Access token
28
     *
29
     * @var string
30
     */
31
    protected $access_token = null;
32
33
    /**
34
     * Instance of the CurlBuilder class
35
     *
36
     * @var CurlBuilder
37
     */
38
    private $curlbuilder;
39
40
    /**
41
     * Array with the headers from the last request
42
     *
43
     * @var array
44
     */
45
    private $headers;
46
47
    /**
48
     * Constructor
49
     *
50
     * @param  CurlBuilder   $curlbuilder
51
     */
52 43
    public function __construct(CurlBuilder $curlbuilder)
53
    {
54 43
        $this->curlbuilder = $curlbuilder;
55 43
    }
56
57
    /**
58
     * Set the access token
59
     *
60
     * @access public
61
     * @param  string   $token
62
     * @return void
63
     */
64 43
    public function setAccessToken($token)
65
    {
66 43
        $this->access_token = $token;
67 43
    }
68
69
    /**
70
     * Make a get request to the given endpoint
71
     *
72
     * @access public
73
     * @param  string   $endpoint
74
     * @param  array    $parameters
75
     * @return Response
76
     */
77 27 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...
78
    {
79 27
        if (!empty($parameters)) {
80 5
            $path = sprintf("%s?%s", $endpoint, http_build_query($parameters));
81
        } else {
82 22
            $path = $endpoint;
83
        }
84
85 27
        return $this->execute("GET", sprintf("%s%s", $this->host, $path));
86
    }
87
88
    /**
89
     * Make a post request to the given endpoint
90
     *
91
     * @access public
92
     * @param  string   $endpoint
93
     * @param  array    $parameters
94
     * @return Response
95
     */
96 3
    public function post($endpoint, array $parameters = array())
97
    {
98 3
        return $this->execute("POST", sprintf("%s%s", $this->host, $endpoint), $parameters);
99
    }
100
101
    /**
102
     * Make a put request to the given endpoint
103
     *
104
     * @access public
105
     * @param  string   $endpoint
106
     * @param  array    $parameters
107
     * @return Response
108
     */
109 1
    public function put($endpoint, array $parameters = array())
110
    {
111 1
        return $this->execute("PUT", sprintf("%s%s", $this->host, $endpoint), $parameters);
112
    }
113
114
    /**
115
     * Make a delete request to the given endpoint
116
     *
117
     * @access public
118
     * @param  string   $endpoint
119
     * @param  array    $parameters
120
     * @return Response
121
     */
122 6
    public function delete($endpoint, array $parameters = array())
123
    {
124 6
        return $this->execute("DELETE", sprintf("%s%s", $this->host, $endpoint) . "/", $parameters);
125
    }
126
127
    /**
128
     * Make an update request to the given endpoint
129
     *
130
     * @access public
131
     * @param  string   $endpoint
132
     * @param  array    $parameters
133
     * @param  array    $queryparameters
134
     * @return Response
135
     */
136 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...
137
    {
138 2
        if (!empty($queryparameters)) {
139
            $path = sprintf("%s?%s", $endpoint, http_build_query($queryparameters));
140
        } else {
141 2
            $path = $endpoint;
142
        }
143
144 2
        return $this->execute("PATCH", sprintf("%s%s", $this->host, $path), $parameters);
145
    }
146
147
    /**
148
     * Return the headers from the last request
149
     *
150
     * @return array
151
     */
152 2
    public function getHeaders()
153
    {
154 2
        return $this->headers;
155
    }
156
157
    /**
158
     * Execute the http request
159
     *
160
     * @access public
161
     * @param  string $method
162
     * @param  string $apiCall
163
     * @param  array $parameters
164
     * @param  array $headers
165
     * @return Response
166
     * @throws CurlException
167
     * @throws PinterestException
168
     */
169 39
    public function execute($method, $apiCall, array $parameters = array(), $headers = array())
170
    {
171
        // Check if the access token needs to be added
172 39
        if ($this->access_token != null) {
173 39
            $headers = array_merge($headers, array(
174 39
                "Authorization: Bearer " . $this->access_token,
175
            ));
176
        }
177
178
        // Force cURL to not send Expect header to workaround bug with Akamai CDN not handling
179
        // this type of requests correctly
180 39
        $headers = array_merge($headers, array(
181 39
            "Expect:",
182
        ));
183
184
        // Setup CURL
185 39
        $ch = $this->curlbuilder->create();
186
187
        // Set default options
188 39
        $ch->setOptions(array(
189 39
            CURLOPT_URL             => $apiCall,
190 39
            CURLOPT_HTTPHEADER      => $headers,
191 39
            CURLOPT_CONNECTTIMEOUT  => 20,
192 39
            CURLOPT_TIMEOUT         => 90,
193 39
            CURLOPT_RETURNTRANSFER  => true,
194 39
            CURLOPT_SSL_VERIFYPEER  => false,
195 39
            CURLOPT_SSL_VERIFYHOST  => false,
196 39
            CURLOPT_HEADER          => false,
197 39
            CURLINFO_HEADER_OUT     => true
198
        ));
199
200
        switch ($method) {
201 39
            case 'POST':
202 3
                $ch->setOptions(array(
203 3
                    CURLOPT_CUSTOMREQUEST   => "POST",
204 3
                    CURLOPT_POST            => count($parameters),
205 3
                    CURLOPT_POSTFIELDS      => $parameters
206
                ));
207
208 3
                if (!class_exists('\CURLFile') && defined('CURLOPT_SAFE_UPLOAD')) {
209
                    $ch->setOption(CURLOPT_SAFE_UPLOAD, false);
210
                }
211 3
                elseif (class_exists('\CURLFile') && defined('CURLOPT_SAFE_UPLOAD')) {
212 3
                    $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...
213
                }
214
215 3
                break;
216 36
            case 'DELETE':
217 6
                $ch->setOption(CURLOPT_CUSTOMREQUEST, "DELETE");
218 6
                break;
219 30
            case 'PATCH':
220 2
                $ch->setOptions(array(
221 2
                    CURLOPT_CUSTOMREQUEST   => "PATCH",
222 2
                    CURLOPT_POST            => count($parameters),
223 2
                    CURLOPT_POSTFIELDS      => $parameters
224
                ));
225 2
                break;
226
            default:
227 28
                $ch->setOption(CURLOPT_CUSTOMREQUEST, "GET");
228 28
                break;
229
        }
230
231
        // Execute request and catch response
232 39
        $response_data = $ch->execute();
233
234 39
        if ($response_data === false && !$ch->hasErrors()) {
235
            throw new CurlException("Error: Curl request failed");
236
        }
237 39
        else if($ch->hasErrors()) {
238
            throw new PinterestException('Error: execute() - cURL error: ' . $ch->getErrors(), $ch->getErrorNumber());
239
        }
240
241
        // Initiate the response
242 39
        $response = new Response($response_data, $ch);
0 ignored issues
show
Security Bug introduced by
It seems like $response_data defined by $ch->execute() on line 232 can also be of type false; however, DirkGroenen\Pinterest\Tr...Response::__construct() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
243
244
        // Check the response code
245 39
        if ($response->getResponseCode() >= 400) {
246 1
            throw new PinterestException('Pinterest error (code: ' . $response->getResponseCode() . ') with message: ' . $response->getMessage(), $response->getResponseCode());
247
        }
248
249
        // Get headers from last request
250 38
        $this->headers = $ch->getHeaders();
251
252
        // Close curl resource
253 38
        $ch->close();
254
255
        // Return the response
256 38
        return $response;
257
    }
258
259
}
260