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 ( ca5817...c44fbe )
by Dirk
05:48 queued 03:00
created

Request::execute()   D

Complexity

Conditions 13
Paths 48

Size

Total Lines 85
Code Lines 50

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 50
CRAP Score 13.2076

Importance

Changes 20
Bugs 9 Features 10
Metric Value
c 20
b 9
f 10
dl 0
loc 85
ccs 50
cts 56
cp 0.8929
rs 4.9922
cc 13
eloc 50
nc 48
nop 4
crap 13.2076

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 5
        } 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 4
    public function post($endpoint, array $parameters = array())
97
    {
98 4
        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
     */
154 35
    public function execute($method, $apiCall, array $parameters = array(), $headers = array())
155
    {
156
        // Check if the access token needs to be added
157 35
        if ($this->access_token != null) {
158 35
            $headers = array_merge($headers, array(
159 35
                "Authorization: Bearer " . $this->access_token,
160
                //"Content-type: application/x-www-form-urlencoded; charset=UTF-8",
161
                "Content-Type:multipart/form-data"
162 35
            ));
163 35
        }
164
165
        // Setup CURL
166 35
        $ch = $this->curlbuilder->create();
167
168
        // Set default options
169 35
        $ch->setOptions(array(
170 35
            CURLOPT_URL             => $apiCall,
171 35
            CURLOPT_HTTPHEADER      => $headers,
172 35
            CURLOPT_CONNECTTIMEOUT  => 20,
173 35
            CURLOPT_TIMEOUT         => 90,
174 35
            CURLOPT_RETURNTRANSFER  => true,
175 35
            CURLOPT_SSL_VERIFYPEER  => false,
176 35
            CURLOPT_SSL_VERIFYHOST  => false,
177 35
            CURLOPT_HEADER          => false,
178 35
            CURLINFO_HEADER_OUT     => true
179 35
        ));
180
181
        switch ($method) {
182 35
            case 'POST':
183 3
                $ch->setOptions(array(
184 3
                    CURLOPT_CUSTOMREQUEST   => "POST",
185 3
                    CURLOPT_POST            => count($parameters),
186 3
                    CURLOPT_POSTFIELDS      => ($parameters)
187 3
                ));
188
189 3
                if (!class_exists("\CURLFile") && defined('CURLOPT_SAFE_UPLOAD')) {
190
                    $ch->setOption(CURLOPT_SAFE_UPLOAD, false);
191
                }
192 3
                elseif (class_exists("\CURLFile") && defined('CURLOPT_SAFE_UPLOAD')) {
193
                    $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...
194
                }
195
196 3
                break;
197 32
            case 'DELETE':
198 5
                $ch->setOption(CURLOPT_CUSTOMREQUEST, "DELETE");
199 5
                break;
200 27
            case 'PATCH':
201 2
                $ch->setOptions(array(
202 2
                    CURLOPT_CUSTOMREQUEST   => "PATCH",
203 2
                    CURLOPT_POST            => count($parameters),
204 2
                    CURLOPT_POSTFIELDS      => http_build_query($parameters)
205 2
                ));
206 2
                break;
207 25
            default:
208 25
                $ch->setOption(CURLOPT_CUSTOMREQUEST, "GET");
209 25
                break;
210 25
        }
211
212
        // Execute request and catch response
213 35
        $response_data = $ch->execute();
214
215 35
        if ($response_data === false && !$ch->hasErrors()) {
216
            throw new CurlException("Error: Curl request failed");
217
        }
218 35
        else if($ch->hasErrors()) {
219
            throw new PinterestException('Error: execute() - cURL error: ' . $ch->getErrors(), $ch->getErrorNumber());
220
        }
221
222
        // Initiate the response
223 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 213 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...
224
225
        // Check the response code
226 35
        if ($response->getResponseCode() >= 400) {
227 1
            throw new PinterestException('Pinterest error (code: ' . $response->getResponseCode() . ') with message: ' . $response->getMessage(), $response->getResponseCode());
228
        }
229
230
        // Get headers from last request
231 34
        $this->headers = $ch->getHeaders();
232
233
        // Close curl resource
234 34
        $ch->close();
235
236
        // Return the response
237 34
        return $response;
238
    }
239
240
}
241