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 — master ( fec26d...4e4046 )
by Dirk
05:35
created

Request::execute()   D

Complexity

Conditions 13
Paths 48

Size

Total Lines 89
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 46
CRAP Score 13.0387

Importance

Changes 0
Metric Value
dl 0
loc 89
ccs 46
cts 49
cp 0.9388
rs 4.9922
c 0
b 0
f 0
cc 13
eloc 51
nc 48
nop 4
crap 13.0387

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
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 39
    public function __construct(CurlBuilder $curlbuilder)
53
    {
54 39
        $this->curlbuilder = $curlbuilder;
55 39
    }
56
57
    /**
58
     * Set the access token
59
     *
60
     * @access public
61
     * @param  string   $token
62
     * @return void
63
     */
64 39
    public function setAccessToken($token)
65
    {
66 39
        $this->access_token = $token;
67 39
    }
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 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...
78
    {
79 25
        if (!empty($parameters)) {
80 5
            $path = sprintf("%s/?%s", $endpoint, http_build_query($parameters));
81
        } else {
82 20
            $path = $endpoint;
83
        }
84
85 25
        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 delete request to the given endpoint
103
     *
104
     * @access public
105
     * @param  string   $endpoint
106
     * @param  array    $parameters
107
     * @return Response
108
     */
109 5
    public function delete($endpoint, array $parameters = array())
110
    {
111 5
        return $this->execute("DELETE", sprintf("%s%s", $this->host, $endpoint) . "/", $parameters);
112
    }
113
114
    /**
115
     * Make an update request to the given endpoint
116
     *
117
     * @access public
118
     * @param  string   $endpoint
119
     * @param  array    $parameters
120
     * @param  array    $queryparameters
121
     * @return Response
122
     */
123 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...
124
    {
125 2
        if (!empty($queryparameters)) {
126
            $path = sprintf("%s/?%s", $endpoint, http_build_query($queryparameters));
127
        } else {
128 2
            $path = $endpoint;
129
        }
130
131 2
        return $this->execute("PATCH", sprintf("%s%s", $this->host, $path), $parameters);
132
    }
133
134
    /**
135
     * Return the headers from the last request
136
     *
137
     * @return array
138
     */
139 2
    public function getHeaders()
140
    {
141 2
        return $this->headers;
142
    }
143
144
    /**
145
     * Execute the http request
146
     *
147
     * @access public
148
     * @param  string $method
149
     * @param  string $apiCall
150
     * @param  array $parameters
151
     * @param  array $headers
152
     * @return Response
153
     * @throws CurlException
154
     * @throws PinterestException
155
     */
156 35
    public function execute($method, $apiCall, array $parameters = array(), $headers = array())
157
    {
158
        // Check if the access token needs to be added
159 35
        if ($this->access_token != null) {
160 35
            $headers = array_merge($headers, array(
161 35
                "Authorization: Bearer " . $this->access_token,
162
            ));
163
        }
164
165
        // Force cURL to not send Expect header to workaround bug with Akamai CDN not handling
166
        // this type of requests correctly
167 35
        $headers = array_merge($headers, array(
168 35
            "Expect:",
169
        ));
170
171
        // Setup CURL
172 35
        $ch = $this->curlbuilder->create();
173
174
        // Set default options
175 35
        $ch->setOptions(array(
176 35
            CURLOPT_URL             => $apiCall,
177 35
            CURLOPT_HTTPHEADER      => $headers,
178 35
            CURLOPT_CONNECTTIMEOUT  => 20,
179 35
            CURLOPT_TIMEOUT         => 90,
180 35
            CURLOPT_RETURNTRANSFER  => true,
181 35
            CURLOPT_SSL_VERIFYPEER  => false,
182 35
            CURLOPT_SSL_VERIFYHOST  => false,
183 35
            CURLOPT_HEADER          => false,
184 35
            CURLINFO_HEADER_OUT     => true
185
        ));
186
187
        switch ($method) {
188 35
            case 'POST':
189 3
                $ch->setOptions(array(
190 3
                    CURLOPT_CUSTOMREQUEST   => "POST",
191 3
                    CURLOPT_POST            => count($parameters),
192 3
                    CURLOPT_POSTFIELDS      => $parameters
193
                ));
194
195 3
                if (!class_exists('\CURLFile') && defined('CURLOPT_SAFE_UPLOAD')) {
196
                    $ch->setOption(CURLOPT_SAFE_UPLOAD, false);
197
                }
198 3
                elseif (class_exists('\CURLFile') && defined('CURLOPT_SAFE_UPLOAD')) {
199 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...
200
                }
201
202 3
                break;
203 32
            case 'DELETE':
204 5
                $ch->setOption(CURLOPT_CUSTOMREQUEST, "DELETE");
205 5
                break;
206 27
            case 'PATCH':
207 2
                $ch->setOptions(array(
208 2
                    CURLOPT_CUSTOMREQUEST   => "PATCH",
209 2
                    CURLOPT_POST            => count($parameters),
210 2
                    CURLOPT_POSTFIELDS      => $parameters
211
                ));
212 2
                break;
213
            default:
214 25
                $ch->setOption(CURLOPT_CUSTOMREQUEST, "GET");
215 25
                break;
216
        }
217
218
        // Execute request and catch response
219 35
        $response_data = $ch->execute();
220
221 35
        if ($response_data === false && !$ch->hasErrors()) {
222
            throw new CurlException("Error: Curl request failed");
223
        }
224 35
        else if($ch->hasErrors()) {
225
            throw new PinterestException('Error: execute() - cURL error: ' . $ch->getErrors(), $ch->getErrorNumber());
226
        }
227
228
        // Initiate the response
229 35
        $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 219 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...
230
231
        // Check the response code
232 35
        if ($response->getResponseCode() >= 400) {
233 1
            throw new PinterestException('Pinterest error (code: ' . $response->getResponseCode() . ') with message: ' . $response->getMessage(), $response->getResponseCode());
234
        }
235
236
        // Get headers from last request
237 34
        $this->headers = $ch->getHeaders();
238
239
        // Close curl resource
240 34
        $ch->close();
241
242
        // Return the response
243 34
        return $response;
244
    }
245
246
}
247