Tiqr_API_Client::callAPI()   B
last analyzed

Complexity

Conditions 8
Paths 16

Size

Total Lines 47
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 31
c 1
b 0
f 0
dl 0
loc 47
ccs 0
cts 31
cp 0
rs 8.1795
cc 8
nc 16
nop 4
crap 72
1
<?php
2
3
class Tiqr_API_Client
4
{
5
    /**
6
     * The API Base URL
7
     *
8
     * @var string
9
     */
10
    protected $_apiBaseURL;
11
12
    /**
13
     * The API consumer key
14
     *
15
     * @var string
16
     */
17
    protected $_consumerKey;
18
19
    /**
20
     * Set API baseURL
21
     *
22
     * @param string $apiBaseURL The API end point
23
     */
24 2
    public function setBaseURL($apiBaseURL)
25
    {
26 2
        $this->_apiBaseURL = rtrim($apiBaseURL, '/') . '/';
27
    }
28
29
    /**
30
     * Set Consumer key
31
     *
32
     * @param string $consumerKey The consumer key
33
     */
34 2
    public function setConsumerKey($consumerKey)
35
    {
36 2
        $this->_consumerKey = $consumerKey;
37
    }
38
39
    /**
40
     * Calls the API
41
     *
42
     * @param string $resource	The resource URL
43
     * @param string $method	The HTTP Method (GET, POST, PUT, DELETE)
44
     * @param array  $data		Data send with request as key => value pairs
45
     *
46
     * @return Object
47
     *
48
     * @throws Exception
49
     */
50
    public function call($resource, $method = "GET", $data = array())
51
    {
52
        $headers['X-OATHService-ConsumerKey'] = $this->_consumerKey;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$headers was never initialized. Although not strictly required by PHP, it is generally a good practice to add $headers = array(); before regardless.
Loading history...
53
54
        $result = $this->callAPI($resource, $method, $data, $headers);
55
56
        if (2 == substr($result->code, 0, 1)) {
57
            return $result;
58
        } elseif (is_object($result->body)) {
59
            throw new Exception($result->body->error, $result->code);
60
61
        } else {
62
            throw new Exception('', $result->code);
63
        }
64
    }
65
66
    /**
67
     * Calls the API endpoint
68
     *
69
     * @param string $resource	The resource URL
70
     * @param string $method	The HTTP Method (GET, POST, PUT, DELETE)
71
     * @param array  $data		Data send with request as key => value pairs
72
     * @param array  $headers   HTTP Headers send with request as key => value pairs
73
     *
74
     * @return Tiqr_API_Entity_APIResult
75
     */
76
    protected function callAPI($resource, $method = "GET", $data = array(), $headers = array())
77
    {
78
        $ch = curl_init();
79
        curl_setopt($ch, CURLOPT_URL, $this->_apiBaseURL . ltrim($resource, '/'));
80
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
81
82
        // Explicitly empty null values, because http_build_query will throw away
83
        // all null values, which would not let us make fields empty.
84
        if (is_array($data)) {
0 ignored issues
show
introduced by
The condition is_array($data) is always true.
Loading history...
85
            foreach ($data as $key => $value) {
86
                if ($value == null) {
87
                    $data[$key] = '';
88
                }
89
            }
90
        }
91
92
        switch ($method) {
93
            case 'POST':
94
                curl_setopt($ch, CURLOPT_POST, 1);
95
                curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
96
                break;
97
            case 'PUT':
98
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
99
                curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
100
                break;
101
            case 'DELETE':
102
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
103
                break;
104
        }
105
106
        $headerArray = array();
107
        foreach ($headers as $key => $value) {
108
            $headerArray[] = $key . ': ' . $value;
109
        }
110
111
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArray);
112
113
        $apiResult = curl_exec($ch);
114
        $resultCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
115
        curl_close($ch);
116
117
        $result = new Tiqr_API_Entity_APIResult();
118
        $result->code = $resultCode;
119
        $result->body = json_decode($apiResult);
0 ignored issues
show
Bug introduced by
It seems like $apiResult can also be of type true; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

119
        $result->body = json_decode(/** @scrutinizer ignore-type */ $apiResult);
Loading history...
120
        $result->rawBody = $apiResult;
121
122
        return $result;
123
    }
124
}
125