Test Setup Failed
Push — master ( cbe16d...3a1fc4 )
by Carsten
98:18 queued 56:15
created

Request::execute()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 41
ccs 6
cts 6
cp 1
rs 8.9528
c 0
b 0
f 0
cc 5
nc 6
nop 2
crap 5
1
<?php
2
3
namespace Lenius\Economic\API;
4
5
/**
6
 * @class      Economic_Request
7
 */
8
class Request
9
{
10
    /**
11
     * Contains Economic_Client instance.
12
     */
13
    protected $client;
14
15
    /**
16
     * __construct function.
17
     *
18
     * Instantiates the object
19
     *
20 4
     * @param Client $client
21
     */
22 4
    public function __construct(Client $client)
23 4
    {
24
        $this->client = $client;
25
    }
26
27
    /**
28
     * GET function.
29
     *
30
     * Performs an API GET request
31
     *
32
     * @param string $path
33
     * @param array  $query
34
     *
35 4
     * @throws Exception
36
     *
37
     * @return Response
38 4
     */
39
    public function get($path, $query = [])
40
    {
41
        // Add query parameters to $path?
42
        if (!empty($query)) {
43
            if (strpos($path, '?') === false) {
44
                $path .= '?'.http_build_query($query, '', '&');
45
            } else {
46
                $path .= ini_get('arg_separator.output').http_build_query($query, '', '&');
47 4
            }
48
        }
49
50 4
        // Set the request params
51
        $this->setUrl($path);
52
53
        // Start the request and return the response
54
        return $this->execute('GET');
55
    }
56
57
    /**
58
     * POST function.
59
     *
60
     * Performs an API POST request
61
     *
62
     * @param string $path
63
     * @param array  $form
64
     *
65
     * @throws Exception
66
     *
67
     * @return Response
68
     */
69
    public function post($path, $form = [])
70
    {
71
        // Set the request params
72
        $this->setUrl($path);
73
74
        // Start the request and return the response
75
        return $this->execute('POST', $form);
76
    }
77
78
    /**
79
     * PUT function.
80
     *
81
     * Performs an API PUT request
82
     *
83
     * @param string $path
84
     * @param array  $form
85
     *
86
     * @throws Exception
87
     *
88
     * @return Response
89
     */
90
    public function put($path, $form = [])
91
    {
92
        // Set the request params
93
        $this->setUrl($path);
94
95
        // Start the request and return the response
96
        return $this->execute('PUT', $form);
97
    }
98
99
    /**
100
     * PATCH function.
101
     *
102
     * Performs an API PATCH request
103
     *
104
     * @param string $path
105
     * @param array  $form
106
     *
107
     * @throws Exception
108
     *
109
     * @return Response
110
     */
111
    public function patch($path, $form = [])
112
    {
113
        // Set the request params
114
        $this->setUrl($path);
115
116
        // Start the request and return the response
117
        return $this->execute('PATCH', $form);
118
    }
119
120
    /**
121
     * DELETE function.
122
     *
123
     * Performs an API DELETE request
124 4
     *
125
     * @param string $path
126 4
     * @param array  $form
127 4
     *
128
     * @throws Exception
129
     *
130
     * @return Response
131
     */
132
    public function delete($path, $form = [])
133
    {
134
        // Set the request params
135
        $this->setUrl($path);
136
137 4
        // Start the request and return the response
138
        return $this->execute('DELETE', $form);
139
    }
140 4
141
    /**
142
     * setUrl function.
143 4
     *
144
     * Takes an API request string and appends it to the API url
145
     *
146
     * @param string $path
147
     *
148
     * @return void
149 4
     */
150 4
    protected function setUrl($path)
151 4
    {
152
        curl_setopt($this->client->ch, CURLOPT_URL, Constants::API_URL.trim($path, '/'));
153
    }
154 4
155
    /**
156 4
     * @param string $request_type
157
     * @param array  $form
158
     *
159
     * @throws Exception
160
     *
161
     * @return Response
162
     */
163 4
    protected function execute($request_type, $form = [])
164 4
    {
165 4
        // Set the HTTP request type
166 4
        curl_setopt($this->client->ch, CURLOPT_CUSTOMREQUEST, $request_type);
167
168
        // If additional data is delivered, we will send it along with the API request
169 4
        if (is_array($form) && !empty($form)) {
170
            $post = json_encode($form);
171
            curl_setopt($this->client->ch, CURLOPT_POSTFIELDS, $post);
172 4
        }
173
174
        // Store received headers in temporary memory file, remember sent headers
175
        if (!$fh_header = fopen('php://temp', 'w+')) {
176
            throw new Exception('Fail to create tmp');
177
        }
178
179
        curl_setopt($this->client->ch, CURLOPT_WRITEHEADER, $fh_header);
180
        curl_setopt($this->client->ch, CURLINFO_HEADER_OUT, true);
181
182
        // Execute the request
183
        $response_data = curl_exec($this->client->ch);
184
185
        if (curl_errno($this->client->ch) !== 0) {
186
            // An error occurred
187
            fclose($fh_header);
188
189
            throw new Exception(curl_error($this->client->ch), curl_errno($this->client->ch));
190
        }
191
192
        // Grab the headers
193
        $sent_headers = curl_getinfo($this->client->ch, CURLINFO_HEADER_OUT);
194
        rewind($fh_header);
195
        $received_headers = stream_get_contents($fh_header);
196
        fclose($fh_header);
197
198
        // Retrieve the HTTP response code
199
        $response_code = (int) curl_getinfo($this->client->ch, CURLINFO_HTTP_CODE);
200
201
        // Return the response object.
202
        return new Response($response_code, $sent_headers, $received_headers, $response_data);
203
    }
204
}
205