Test Setup Failed
Push — master ( 553e74...c78a38 )
by Carsten
06:57
created

Request   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 182
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 52.17%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 3
dl 0
loc 182
ccs 24
cts 46
cp 0.5217
rs 10
c 0
b 0
f 0

7 Methods

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