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 ( a82931...4a9170 )
by Dirk
07:05 queued 04:00
created

Request::execute()   C

Complexity

Conditions 10
Paths 30

Size

Total Lines 79
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 49
CRAP Score 10.0192

Importance

Changes 19
Bugs 8 Features 10
Metric Value
c 19
b 8
f 10
dl 0
loc 79
ccs 49
cts 52
cp 0.9423
rs 5.5893
cc 10
eloc 46
nc 30
nop 4
crap 10.0192

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