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 ( db31ee...a5f74a )
by Anderson
05:41
created

CurlRequest   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 0
dl 0
loc 91
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
D request() 0 71 11
A getBaseUrl() 0 3 1
A setBaseUrl() 0 3 1
1
<?php
2
3
namespace DoctrineElastic\Http;
4
5
/**
6
 * Class ElasticRequest
7
 * @author Andsalves <[email protected]>
8
 */
9
class CurlRequest {
10
11
    protected $baseUrl = '';
12
13
    public function request($url, array $data, $method = 'POST', array $headers = null) {
14
        try {
15
            $ch = curl_init();
16
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
17
18
            if (!boolval($headers)) {
19
                $headers = ['Content-Type: application/json; charset=utf-8'];
20
            }
21
22
            switch ($method) {
23
                case 'GET':
24
                    $url .= '?';
25
26
                    foreach ($data as $key => $value) {
27
                        $url .= $key . '=' . $value . '&';
28
                    }
29
30
                    rtrim($url, '&');
31
                    break;
32
                case 'POST':
33
                    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
34
                    curl_setopt($ch, CURLOPT_POST, true);
35
                    break;
36
                case 'HEAD':
37
                    curl_setopt($ch, CURLOPT_HEADER, true);
38
                    curl_setopt($ch, CURLOPT_NOBODY, true);
39
                    break;
40
                default:
41
                    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
42
            }
43
44
            if (in_array($method, ['POST', 'PUT'])) {
45
                if (!empty($data)) {
46
                    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
47
                }
48
            }
49
50
            $url = rtrim($this->baseUrl, '/') . '/' . ltrim($url, '/');
51
            curl_setopt($ch, CURLOPT_URL, $url);
52
53
            $resultJson = curl_exec($ch);
54
55
            $content = json_decode($resultJson, true);
56
            $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
57
58
            curl_close($ch);
59
60
            if (is_null($content) && in_array($httpcode, [400, 500])) {
61
                return array(
62
                    'status' => $httpcode,
63
                    'content' => array(
64
                        'error' => ['reason' => $resultJson]
65
                    )
66
                );
67
            }
68
69
            return array(
70
                'status' => $httpcode,
71
                'content' => $content
72
            );
73
        } catch (\Exception $ex) {
74
            return array(
75
                'status' => 500,
76
                'content' => array(
77
                    'error' => ['reason' => $ex->getMessage()],
78
                    'detail' => $ex->getTraceAsString(),
79
                    'line' => $ex->getLine()
80
                )
81
            );
82
        }
83
    }
84
85
    /**
86
     * @return string
87
     */
88
    public function getBaseUrl() {
89
        return $this->baseUrl;
90
    }
91
92
    /**
93
     * @param string $baseUrl
94
     */
95
    public function setBaseUrl($baseUrl) {
96
        $this->baseUrl = $baseUrl;
97
    }
98
99
}