Passed
Push — master ( de71cb...6050eb )
by Carsten
02:32
created

Request::patch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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