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 — 2.0 ( fb4bbc...dcdbd9 )
by Nico
29:09 queued 02:20
created

Base   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 0
dl 0
loc 139
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getApiEndpoint() 0 4 1
A setApiEndpoint() 0 5 1
A getApiKey() 0 4 1
A setApiKey() 0 5 1
C request() 0 59 10
1
<?php
2
namespace Datatrics\API\Modules;
3
4
class Base
5
{
6
    const CLIENT_VERSION = '2.0.0';
7
8
    const HTTP_GET = 'GET';
9
    const HTTP_POST = 'POST';
10
    const HTTP_PUT = 'PUT';
11
    const HTTP_DELETE = 'DELETE';
12
13
    /**
14
     * @var string
15
     */
16
    protected $api_endpoint = 'https://api.datatrics.com/2.0';
17
18
    /**
19
     * @var string
20
     */
21
    protected $api_key;
22
23
    /**
24
     * Base constructor.
25
     * @param $apikey
26
     * @param $endpoint
27
     */
28
    public function __construct($apikey, $endpoint)
29
    {
30
        $this->api_key = $apikey;
31
        $this->api_endpoint .= $endpoint;
32
    }
33
34
    /**
35
     * @return string
36
     */
37
    public function getApiEndpoint()
38
    {
39
        return $this->api_endpoint;
40
    }
41
42
    /**
43
     * @param string $api_endpoint
44
     * @return Base
45
     */
46
    public function setApiEndpoint($api_endpoint)
47
    {
48
        $this->api_endpoint = $api_endpoint;
49
        return $this;
50
    }
51
52
    /**
53
     * @return string
54
     */
55
    public function getApiKey()
56
    {
57
        return $this->api_key;
58
    }
59
60
    /**
61
     * @param string $api_key
62
     * @return Base
63
     */
64
    public function setApiKey($api_key)
65
    {
66
        $this->api_key = $api_key;
67
        return $this;
68
    }
69
70
71
72
    /**
73
     * Perform an http call. This method is used by the resource specific classes.
74
     *
75
     * @param $http_method
76
     * @param $api_method
77
     * @param $http_body
78
     *
79
     * @return string
80
     *
81
     * @throws \Exception
82
     */
83
    public function request($http_method, $api_method, $http_body = null)
84
    {
85
        if (empty($this->api_key)) {
86
            throw new \Exception('You have not set an api key. Please use setApiKey() to set the API key.');
87
        }
88
        $url = $this->api_endpoint.$api_method;
89
        $ch = curl_init($url);
90
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
91
        curl_setopt($ch, CURLOPT_ENCODING, '');
92
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
93
        $user_agent = 'Datatrics Frontend '.self::CLIENT_VERSION;
94
        $request_headers = array(
95
            'Accept: application/json',
96
            'User-Agent: '.$user_agent,
97
            'X-apikey: '.$this->api_key,
98
            'X-Client-Name: '.$user_agent,
99
            'X-Datatrics-Client-Info: '.php_uname(),
100
        );
101
        if ($http_body !== null) {
102
            $request_headers[] = 'Content-Type: application/json';
103
            if ($http_method == self::HTTP_POST) {
104
                curl_setopt($ch, CURLOPT_POST, 1);
105
            } elseif ($http_method == self::HTTP_PUT) {
106
                curl_setopt($ch, CURLOPT_PUT, 1);
107
            } else {
108
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $http_method);
109
            }
110
            curl_setopt($ch, CURLOPT_POSTFIELDS, $http_body);
111
        }
112
        curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
113
        //curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
114
        //curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
115
        $body = curl_exec($ch);
116
        if (curl_errno($ch) == CURLE_SSL_CACERT || curl_errno($ch) == CURLE_SSL_PEER_CERTIFICATE || curl_errno($ch) == 77 /* CURLE_SSL_CACERT_BADFILE (constant not defined in PHP though) */) {
117
            /*
118
             * On some servers, the list of installed certificates is outdated or not present at all (the ca-bundle.crt
119
             * is not installed). So we tell cURL which certificates we trust. Then we retry the requests.
120
             */
121
            $request_headers[] = 'X-Datatrics-Debug: used shipped root certificates';
122
            curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
123
            curl_setopt($ch, CURLOPT_CAINFO, realpath(dirname(__FILE__).'/cacert.pem'));
124
            $body = curl_exec($ch);
125
        }
126
        if (strpos(curl_error($ch), "certificate subject name 'mollie.nl' does not match target host") !== false) {
127
            /*
128
             * On some servers, the wildcard SSL certificate is not processed correctly. This happens with OpenSSL 0.9.7
129
             * from 2003.
130
             */
131
            $request_headers[] = 'X-Datatrics-Debug: old OpenSSL found';
132
            curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
133
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
134
            $body = curl_exec($ch);
135
        }
136
        if (curl_errno($ch)) {
137
            throw new \Exception('Unable to communicate with Datatrics ('.curl_errno($ch).'): '.curl_error($ch));
138
        }
139
        
140
        return json_decode($body);
141
    }
142
}
143