Skip to content

Commit f80343f

Browse files
committed
decomposed buildResponse()
1 parent b1a7cd3 commit f80343f

File tree

1 file changed

+44
-14
lines changed

1 file changed

+44
-14
lines changed

src/Request.php

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -104,20 +104,55 @@ private function getCurlResource($forceNew = false) {
104104
*/
105105
private function buildResponse() {
106106

107-
// basic cURL options
107+
$curlOptions = $this->getBasicCurlOptions();
108+
$this->addRequestAuth($curlOptions);
109+
$this->addRequestHeaders($curlOptions);
110+
$this->addRequestMethod($curlOptions);
111+
112+
// push options into the resource
113+
$curlResource = $this->getCurlResource();
114+
if (!curl_setopt_array($curlResource, $curlOptions)) {
115+
throw new RequestException('Invalid cURL options');
116+
}
117+
118+
// create response
119+
$response = new Response($curlResource);
120+
return $response;
121+
}
122+
123+
/**
124+
* Create basic curl options
125+
*
126+
* @return array Curl options
127+
*/
128+
private function getBasicCurlOptions() {
108129
$curlOptions = $this->getOption(self::CURL_OPTIONS_KEY, array());
109130
$curlOptions[CURLOPT_HEADER] = true;
110131
$curlOptions[CURLOPT_RETURNTRANSFER] = true;
111132
$curlOptions[CURLOPT_USERAGENT] = $this->getOption(self::USER_AGENT_KEY);
112133
$curlOptions[CURLOPT_URL] = $this->getOption(self::BASE_URL_KEY);
134+
return $curlOptions;
135+
}
113136

114-
// cURL authentication
137+
/**
138+
* Add authentication to curl options
139+
*
140+
* @param array &$curlOptions
141+
*/
142+
private function addRequestAuth(&$curlOptions) {
115143
$username = $this->getOption(self::USERNAME_KEY);
116144
$password = $this->getOption(self::PASSWORD_KEY);
117145
if ((!is_null($username)) && (!is_null($password))) {
118146
$curlOptions[CURLOPT_USERPWD] = sprintf("%s:%s", $username, $password);
119147
}
148+
}
120149

150+
/**
151+
* Add headers to curl options
152+
*
153+
* @param array &$curlOptions
154+
*/
155+
private function addRequestHeaders(&$curlOptions) {
121156
// cURL HTTP headers
122157
$headers = $this->getOption(self::HEADERS_KEY, array());
123158
// Turn off the Expect header to stop HTTP 100 Continue responses.
@@ -129,8 +164,14 @@ private function buildResponse() {
129164
$curlOptions[CURLOPT_HTTPHEADER][] = sprintf("%s:%s", $key, $value);
130165
}
131166
}
167+
}
132168

133-
// method
169+
/**
170+
* Add Method to curl options
171+
*
172+
* @param array &$curlOptions
173+
*/
174+
private function addRequestMethod(&$curlOptions) {
134175
$method = strtoupper($this->getOption(self::METHOD_KEY, 'GET'));
135176
switch ($method) {
136177
case 'GET':
@@ -143,17 +184,6 @@ private function buildResponse() {
143184
$curlOptions[CURLOPT_CUSTOMREQUEST] = $method;
144185
$curlOptions[CURLOPT_POSTFIELDS] = $this->getOption(self::DATA_KEY, array());
145186
}
146-
147-
// push options into the resource
148-
$curlResource = $this->getCurlResource();
149-
if (!curl_setopt_array($curlResource, $curlOptions)) {
150-
throw new RequestException('Invalid cURL options');
151-
}
152-
153-
// create response
154-
$response = new Response($curlResource);
155-
156-
return $response;
157187
}
158188

159189
/*

0 commit comments

Comments
 (0)