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
Pull Request — master (#52)
by Rufus
02:07
created

Request   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 14
lcom 0
cbo 3
dl 0
loc 110
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
D perform() 0 94 14
1
<?php
2
3
namespace Cloudflare;
4
5
use Cloudflare\Exception\AuthenticationException;
6
use Cloudflare\Exception\UnauthorizedException;
7
8
/**
9
 * CloudFlare API wrapper
10
 *
11
 * Request
12
 * The object to perform API requests
13
 *
14
 * @author Tang Rufus <[email protected]>
15
 */
16
class Request implements RequestInterface
17
{
18
    /**
19
     * API call method for sending requests using GET, POST, PUT, DELETE OR PATCH
20
     *
21
     * @param Api         $api    The client object
22
     * @param string      $path   Path of the endpoint
23
     * @param array|null  $data   Data to be sent along with the request
24
     * @param string|null $method Type of method that should be used ('GET', 'POST', 'PUT', 'DELETE', 'PATCH')
25
     *
26
     * @throws \Cloudflare\Exception\AuthenticationException
27
     * @throws \Cloudflare\Exception\UnauthorizedException
28
     *
29
     * @return mixed
30
     */
31
    public function perform($api, $path, array $data = null, $method = null)
32
    {
33
        if (!isset($api->email, $api->auth_key) || false === filter_var($api->email, FILTER_VALIDATE_EMAIL)) {
34
            throw new AuthenticationException(
35
                'Authentication information must be provided'
36
            );
37
        }
38
39
        $data = (null === $data ? [] : $data);
40
        $method = (null === $method ? 'get' : $method);
41
42
        //Removes null entries
43
        $data = array_filter(
44
            $data,
45
            function ($val) {
46
                return null !== $val;
47
            }
48
        );
49
50
        $url = 'https://api.cloudflare.com/client/v4/'.$path;
51
52
        $default_curl_options = [
53
            CURLOPT_VERBOSE        => false,
54
            CURLOPT_FORBID_REUSE   => true,
55
            CURLOPT_RETURNTRANSFER => 1,
56
            CURLOPT_HEADER         => false,
57
            CURLOPT_TIMEOUT        => 30,
58
            CURLOPT_SSL_VERIFYPEER => true,
59
        ];
60
61
        $curl_options = $default_curl_options;
62
        if (null !== $api->curl_options && is_array($api->curl_options)) {
63
            $curl_options = array_replace($default_curl_options, $api->curl_options);
64
        }
65
66
        $user_agent = __FILE__;
67
        $headers = [
68
            "X-Auth-Email: {$api->email}",
69
            "X-Auth-Key: {$api->auth_key}",
70
            "User-Agent: {$user_agent}",
71
        ];
72
73
        $ch = curl_init();
74
        curl_setopt_array($ch, $curl_options);
75
76
        $headers[] = 'Content-type: application/json';
77
        $json_data = json_encode($data);
78
79
        if ($method === 'post') {
80
            curl_setopt($ch, CURLOPT_POST, true);
81
            curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
82
        } elseif ($method === 'put') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
83
            curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
84
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
85
        } elseif ($method === 'delete') {
86
            curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
87
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
88
        } elseif ($method === 'patch') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
89
            curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
90
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
91
        } else {
92
            $url .= '?'.http_build_query($data);
93
        }
94
95
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
96
        curl_setopt($ch, CURLOPT_URL, $url);
97
98
        $http_result = curl_exec($ch);
99
        $error = curl_error($ch);
100
        $information = curl_getinfo($ch);
101
        $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
102
103
        if (in_array($http_code, [401, 403], true)) {
104
            throw new UnauthorizedException(
105
                'You do not have permission to perform this request'
106
            );
107
        }
108
109
        $response = json_decode($http_result);
110
        if (!$response) {
111
            $response = new \stdClass();
112
            $response->success = false;
113
        }
114
115
        curl_close($ch);
116
        if ($response->success !== true) {
117
            $response->error = $error;
118
            $response->http_code = $http_code;
119
            $response->method = $method;
120
            $response->information = $information;
121
        }
122
123
        return $response;
124
    }
125
}
126