Conditions | 28 |
Paths | > 20000 |
Total Lines | 175 |
Code Lines | 68 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
61 | public function request($method, JUri $uri, $data = null, array $headers = null, $timeout = null, $userAgent = null) |
||
62 | { |
||
63 | // Setup the cURL handle. |
||
64 | $ch = curl_init(); |
||
65 | |||
66 | $options = array(); |
||
67 | |||
68 | // Set the request method. |
||
69 | switch (strtoupper($method)) |
||
70 | { |
||
71 | case 'GET': |
||
72 | $options[CURLOPT_HTTPGET] = true; |
||
73 | break; |
||
74 | |||
75 | case 'POST': |
||
76 | $options[CURLOPT_POST] = true; |
||
77 | break; |
||
78 | |||
79 | case 'PUT': |
||
80 | default: |
||
81 | $options[CURLOPT_CUSTOMREQUEST] = strtoupper($method); |
||
82 | break; |
||
83 | } |
||
84 | |||
85 | // Don't wait for body when $method is HEAD |
||
86 | $options[CURLOPT_NOBODY] = ($method === 'HEAD'); |
||
87 | |||
88 | // Initialize the certificate store |
||
89 | $options[CURLOPT_CAINFO] = $this->options->get('curl.certpath', __DIR__ . '/cacert.pem'); |
||
90 | |||
91 | // If data exists let's encode it and make sure our Content-type header is set. |
||
92 | if (isset($data)) |
||
93 | { |
||
94 | // If the data is a scalar value simply add it to the cURL post fields. |
||
95 | if (is_scalar($data) || (isset($headers['Content-Type']) && strpos($headers['Content-Type'], 'multipart/form-data') === 0)) |
||
96 | { |
||
97 | $options[CURLOPT_POSTFIELDS] = $data; |
||
98 | } |
||
99 | |||
100 | // Otherwise we need to encode the value first. |
||
101 | else |
||
102 | { |
||
103 | $options[CURLOPT_POSTFIELDS] = http_build_query($data); |
||
104 | } |
||
105 | |||
106 | if (!isset($headers['Content-Type'])) |
||
107 | { |
||
108 | $headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8'; |
||
109 | } |
||
110 | |||
111 | // Add the relevant headers. |
||
112 | if (is_scalar($options[CURLOPT_POSTFIELDS])) |
||
113 | { |
||
114 | $headers['Content-Length'] = strlen($options[CURLOPT_POSTFIELDS]); |
||
115 | } |
||
116 | } |
||
117 | |||
118 | // Build the headers string for the request. |
||
119 | $headerArray = array(); |
||
120 | |||
121 | if (isset($headers)) |
||
122 | { |
||
123 | foreach ($headers as $key => $value) |
||
124 | { |
||
125 | $headerArray[] = $key . ': ' . $value; |
||
126 | } |
||
127 | |||
128 | // Add the headers string into the stream context options array. |
||
129 | $options[CURLOPT_HTTPHEADER] = $headerArray; |
||
130 | } |
||
131 | |||
132 | // Curl needs the accepted encoding header as option |
||
133 | if (isset($headers['Accept-Encoding'])) |
||
134 | { |
||
135 | $options[CURLOPT_ENCODING] = $headers['Accept-Encoding']; |
||
136 | } |
||
137 | |||
138 | // If an explicit timeout is given user it. |
||
139 | if (isset($timeout)) |
||
140 | { |
||
141 | $options[CURLOPT_TIMEOUT] = (int) $timeout; |
||
142 | $options[CURLOPT_CONNECTTIMEOUT] = (int) $timeout; |
||
143 | } |
||
144 | |||
145 | // If an explicit user agent is given use it. |
||
146 | if (isset($userAgent)) |
||
147 | { |
||
148 | $options[CURLOPT_USERAGENT] = $userAgent; |
||
149 | } |
||
150 | |||
151 | // Set the request URL. |
||
152 | $options[CURLOPT_URL] = (string) $uri; |
||
153 | |||
154 | // We want our headers. :-) |
||
155 | $options[CURLOPT_HEADER] = true; |
||
156 | |||
157 | // Return it... echoing it would be tacky. |
||
158 | $options[CURLOPT_RETURNTRANSFER] = true; |
||
159 | |||
160 | // Override the Expect header to prevent cURL from confusing itself in its own stupidity. |
||
161 | // Link: http://the-stickman.com/web-development/php-and-curl-disabling-100-continue-header/ |
||
162 | $options[CURLOPT_HTTPHEADER][] = 'Expect:'; |
||
163 | |||
164 | // Follow redirects if server config allows |
||
165 | if ($this->redirectsAllowed()) |
||
166 | { |
||
167 | $options[CURLOPT_FOLLOWLOCATION] = (bool) $this->options->get('follow_location', true); |
||
168 | } |
||
169 | |||
170 | // Proxy configuration |
||
171 | $config = JFactory::getConfig(); |
||
172 | |||
173 | if ($config->get('proxy_enable')) |
||
174 | { |
||
175 | $options[CURLOPT_PROXY] = $config->get('proxy_host') . ':' . $config->get('proxy_port'); |
||
176 | |||
177 | if ($user = $config->get('proxy_user')) |
||
178 | { |
||
179 | $options[CURLOPT_PROXYUSERPWD] = $user . ':' . $config->get('proxy_pass'); |
||
180 | } |
||
181 | } |
||
182 | |||
183 | // Set any custom transport options |
||
184 | foreach ($this->options->get('transport.curl', array()) as $key => $value) |
||
185 | { |
||
186 | $options[$key] = $value; |
||
187 | } |
||
188 | |||
189 | // Authentification, if needed |
||
190 | if ($this->options->get('userauth') && $this->options->get('passwordauth')) |
||
191 | { |
||
192 | $options[CURLOPT_USERPWD] = $this->options->get('userauth') . ':' . $this->options->get('passwordauth'); |
||
193 | $options[CURLOPT_HTTPAUTH] = CURLAUTH_BASIC; |
||
194 | } |
||
195 | |||
196 | // Set the cURL options. |
||
197 | curl_setopt_array($ch, $options); |
||
198 | |||
199 | // Execute the request and close the connection. |
||
200 | $content = curl_exec($ch); |
||
201 | |||
202 | // Check if the content is a string. If it is not, it must be an error. |
||
203 | if (!is_string($content)) |
||
204 | { |
||
205 | $message = curl_error($ch); |
||
206 | |||
207 | if (empty($message)) |
||
208 | { |
||
209 | // Error but nothing from cURL? Create our own |
||
210 | $message = 'No HTTP response received'; |
||
211 | } |
||
212 | |||
213 | throw new RuntimeException($message); |
||
214 | } |
||
215 | |||
216 | // Get the request information. |
||
217 | $info = curl_getinfo($ch); |
||
218 | |||
219 | // Close the connection. |
||
220 | curl_close($ch); |
||
221 | |||
222 | $response = $this->getResponse($content, $info); |
||
223 | |||
224 | // Manually follow redirects if server doesn't allow to follow location using curl |
||
225 | if ($response->code >= 301 && $response->code < 400 && isset($response->headers['Location']) && (bool) $this->options->get('follow_location', true)) |
||
226 | { |
||
227 | $redirect_uri = new JUri($response->headers['Location']); |
||
228 | if (in_array($redirect_uri->getScheme(), array('file', 'scp'))) |
||
229 | { |
||
230 | throw new RuntimeException('Curl redirect cannot be used in file or scp requests.'); |
||
231 | } |
||
232 | $response = $this->request($method, $redirect_uri, $data, $headers, $timeout, $userAgent); |
||
233 | } |
||
234 | |||
235 | return $response; |
||
236 | } |
||
367 |