Passed
Branch master (f8c3de)
by Carsten
02:40 queued 01:01
created

Request::execute()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 55

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 55
ccs 0
cts 30
cp 0
rs 8.3595
c 0
b 0
f 0
cc 6
nc 7
nop 3
crap 42

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
    public function __construct(Client $client)
23
    {
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
     * @throws Exception
36
     *
37
     * @return Response
38
     */
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
            }
48
        }
49
50
        // Start the request and return the response
51
        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
    public function post($path, $form = [])
67
    {
68
        // Start the request and return the response
69
        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
    public function put($path, $form = [])
85
    {
86
        // Start the request and return the response
87
        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
    public function patch($path, $form = [])
103
    {
104
        // Start the request and return the response
105
        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
    public function delete($path, $form = [])
121
    {
122
        // Start the request and return the response
123
        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
    protected function execute($request_type, $path, $form = [])
136
    {
137
        // Store received headers in temporary memory file, remember sent headers
138
        if (!$path) {
139
            throw new \InvalidArgumentException('Path is missing');
140
        }
141
142
        // Init client
143
        $this->client->create();
144
145
        // Set the request path
146
        curl_setopt($this->client->ch, CURLOPT_URL, Constants::API_URL.trim($path, '/'));
147
148
        // Set the HTTP request type
149
        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
        if (is_array($form) && !empty($form)) {
153
            $post = json_encode($form);
154
            curl_setopt($this->client->ch, CURLOPT_POSTFIELDS, $post);
155
        }
156
157
        // Store received headers in temporary memory file, remember sent headers
158
        if (!$fh_header = fopen('php://temp', 'w+')) {
159
            throw new Exception('Fail to create tmp');
160
        }
161
162
        curl_setopt($this->client->ch, CURLOPT_WRITEHEADER, $fh_header);
163
        curl_setopt($this->client->ch, CURLINFO_HEADER_OUT, true);
164
165
        // Execute the request
166
        $response_data = curl_exec($this->client->ch);
167
168
        if (curl_errno($this->client->ch) !== 0) {
169
            // An error occurred
170
            fclose($fh_header);
171
172
            throw new Exception(curl_error($this->client->ch), curl_errno($this->client->ch));
173
        }
174
175
        // Grab the headers
176
        $sent_headers = curl_getinfo($this->client->ch, CURLINFO_HEADER_OUT);
177
        rewind($fh_header);
178
        $received_headers = stream_get_contents($fh_header);
179
        fclose($fh_header);
180
181
        // Retrieve the HTTP response code
182
        $response_code = (int) curl_getinfo($this->client->ch, CURLINFO_HTTP_CODE);
183
184
        // Shutdown client
185
        $this->client->shutdown();
186
187
        // Return the response object.
188
        return new Response($response_code, $sent_headers, $received_headers, $response_data);
189
    }
190
}
191