Completed
Push — master ( 98f448...de1689 )
by Lars
05:57 queued 04:05
created

Request::httpBuildQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
namespace QuickPay\API;
3
4
use QuickPay\API\Constants;
5
use QuickPay\API\Response;
6
7
/**
8
 * @class      QuickPay_Request
9
 * @since      0.1.0
10
 * @package    QuickPay
11
 * @category   Class
12
 * @author     Patrick Tolvstein, Perfect Solution ApS
13
 * @docs       http://tech.quickpay.net/api/
14
 */
15
class Request
16
{
17
    /**
18
     * Contains QuickPay_Client instance
19
     *
20
     * @access protected
21
     */
22
    protected $client;
23
24
    /**
25
     * __construct function.
26
     *
27
     * Instantiates the object
28
     *
29
     * @access public
30
     */
31 7
    public function __construct($client)
32
    {
33 7
        $this->client = $client;
34 7
    }
35
36
    /**
37
     * GET function.
38
     *
39
     * Performs an API GET request
40
     *
41
     * @access public
42
     * @param  string $path
43
     * @param  array  $query
44
     * @return Response
45
     */
46 4
    public function get($path, $query = array())
47
    {
48
        // Add query parameters to $path?
49 4
        if (!empty($query)) {
50
            if (strpos($path, '?') === false) {
51
                $path .= '?' . http_build_query($query, '', '&');
52
            } else {
53
                $path .= ini_get('arg_separator.output') . http_build_query($query, '', '&');
54
            }
55
        }
56
57
        // Set the request params
58 4
        $this->setUrl($path);
59
60
        // Start the request and return the response
61 4
        return $this->execute('GET');
62
    }
63
64
    /**
65
     * POST function.
66
     *
67
     * Performs an API POST request
68
     *
69
     * @access public
70
     * @return Response
71
     */
72 2
    public function post($path, $form = array())
73
    {
74
        // Set the request params
75 2
        $this->setUrl($path);
76
77
        // Start the request and return the response
78 2
        return $this->execute('POST', $form);
79
    }
80
81
    /**
82
     * PUT function.
83
     *
84
     * Performs an API PUT request
85
     *
86
     * @access public
87
     * @return Response
88
     */
89
    public function put($path, $form = array())
90
    {
91
        // Set the request params
92
        $this->setUrl($path);
93
94
        // Start the request and return the response
95
        return $this->execute('PUT', $form);
96
    }
97
98
    /**
99
     * PATCH function.
100
     *
101
     * Performs an API PATCH request
102
     *
103
     * @access public
104
     * @return Response
105
     */
106
    public function patch($path, $form = array())
107
    {
108
        // Set the request params
109
        $this->setUrl($path);
110
111
        // Start the request and return the response
112
        return $this->execute('PATCH', $form);
113
    }
114
115
    /**
116
     * DELETE function.
117
     *
118
     * Performs an API DELETE request
119
     *
120
     * @access public
121
     * @return Response
122
     */
123
    public function delete($path, $form = array())
124
    {
125
        // Set the request params
126
        $this->setUrl($path);
127
128
        // Start the request and return the response
129
        return $this->execute('DELETE', $form);
130
    }
131
132
    /**
133
     * setUrl function.
134
     *
135
     * Takes an API request string and appends it to the API url
136
     *
137
     * @access protected
138
     * @return void
139
     */
140 6
    protected function setUrl($params)
141
    {
142 6
        curl_setopt($this->client->ch, CURLOPT_URL, Constants::API_URL . trim($params, '/'));
143 6
    }
144
145
    /**
146
     * EXECUTE function.
147
     *
148
     * Performs the prepared API request
149
     *
150
     * @access protected
151
     * @param  string $request_type
152
     * @param  array  $form
153
     * @return Response
154
     */
155 6
    protected function execute($request_type, $form = array())
156
    {
157
        // Set the HTTP request type
158 6
        curl_setopt($this->client->ch, CURLOPT_CUSTOMREQUEST, $request_type);
159
160
        // If additional data is delivered, we will send it along with the API request
161 6
        if (is_array($form) && ! empty($form)) {
162
            curl_setopt($this->client->ch, CURLOPT_POSTFIELDS, $this->httpBuildQuery($form, '', '&'));
0 ignored issues
show
Unused Code introduced by
The call to Request::httpBuildQuery() has too many arguments starting with ''.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
163
        }
164
165
        // Store received headers in temporary memory file, remember sent headers
166 6
        $fh_header = fopen('php://temp', 'w+');
167 6
        curl_setopt($this->client->ch, CURLOPT_WRITEHEADER, $fh_header);
168 6
        curl_setopt($this->client->ch, CURLINFO_HEADER_OUT, true);
169
170
        // Execute the request
171 6
        $response_data = curl_exec($this->client->ch);
172
173 6
        if (curl_errno($this->client->ch) !== 0) {
174
            // An error occurred
175
            fclose($fh_header);
176
            throw new Exception(curl_error($this->client->ch), curl_errno($this->client->ch));
177
        }
178
179
        // Grab the headers
180 6
        $sent_headers = curl_getinfo($this->client->ch, CURLINFO_HEADER_OUT);
181 6
        rewind($fh_header);
182 6
        $received_headers = stream_get_contents($fh_header);
183 6
        fclose($fh_header);
184
185
        // Retrieve the HTTP response code
186 6
        $response_code = (int) curl_getinfo($this->client->ch, CURLINFO_HTTP_CODE);
187
188
        // Return the response object.
189 6
        return new Response($response_code, $sent_headers, $received_headers, $response_data);
190
    }
191
192
    /**
193
     * Improves http_build_query() for the QuickPay use case.
194
     *
195
     * Kept public for testing purposes.
196
     *
197
     * @param array $query
198
     * @return mixed|string
199
     */
200 1
    public function httpBuildQuery($query)
201
    {
202 1
        $query = http_build_query($query);
203 1
        $query = preg_replace('/%5B[0-9]+%5D/i', '%5B%5D', $query);
204 1
        return $query;
205
    }
206
}
207