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:37
created

Request   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

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

1 Method

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