Conditions | 13 |
Paths | 386 |
Total Lines | 123 |
Code Lines | 56 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 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 |
||
75 | public function send(Request $request, Hook\IChunkHook $chunkHook = NULL) { |
||
76 | $opts = []; |
||
77 | |||
78 | // Resets all the cURL options. The curl_reset() function is available only since PHP 5.5. |
||
79 | if (function_exists('curl_reset')) |
||
80 | curl_reset($this->handle); |
||
81 | |||
82 | // Sets the methods and its related options. |
||
83 | switch ($request->getMethod()) { |
||
84 | |||
85 | // GET method. |
||
86 | case Request::GET_METHOD: |
||
87 | $opts[CURLOPT_HTTPGET] = TRUE; |
||
88 | break; |
||
89 | |||
90 | // POST method. |
||
91 | case Request::POST_METHOD: |
||
92 | $opts[CURLOPT_POST] = TRUE; |
||
93 | |||
94 | // The full data to post in a HTTP "POST" operation. To post a file, prepend a filename with @ and use the full |
||
95 | // path. This can either be passed as a urlencoded string like 'para1=val1¶2=val2&...' or as an array with |
||
96 | // the field name as key and field data as value. If value is an array, the Content-Type header will be set to |
||
97 | // multipart/form-data. |
||
98 | $opts[CURLOPT_POSTFIELDS] = $request->getBody(); |
||
99 | break; |
||
100 | |||
101 | // PUT method. |
||
102 | case Request::PUT_METHOD: |
||
103 | $opts[CURLOPT_PUT] = TRUE; |
||
104 | |||
105 | // Often a request contains data in the form of a JSON object. Since cURL is just able to read data from a file, |
||
106 | // but we can't create a temporary file because it's a too much expensive operation, the code below uses a faster |
||
107 | // and efficient memory stream. |
||
108 | if ($request->hasBody()) { |
||
109 | if ($fd = fopen("php://memory", "r+")) { // Try to create a temporary file in memory. |
||
110 | fputs($fd, $request->getBody()); // Writes the message body. |
||
111 | rewind($fd); // Sets the pointer to the beginning of the file stream. |
||
112 | |||
113 | $opts[CURLOPT_INFILE] = $fd; |
||
114 | $opts[CURLOPT_INFILESIZE] = $request->getBodyLength(); |
||
115 | } |
||
116 | else |
||
117 | throw new \RuntimeException("Cannot create the stream."); |
||
118 | } |
||
119 | |||
120 | break; |
||
121 | |||
122 | // DELETE method. |
||
123 | case Request::DELETE_METHOD: |
||
124 | $opts[CURLOPT_CUSTOMREQUEST] = Request::DELETE_METHOD; |
||
125 | break; |
||
126 | |||
127 | // COPY or any other custom method. |
||
128 | default: |
||
129 | $opts[CURLOPT_CUSTOMREQUEST] = $request->getMethod(); |
||
130 | |||
131 | } // switch |
||
132 | |||
133 | // Sets the request Uniform Resource Locator. |
||
134 | $opts[CURLOPT_URL] = $this->scheme.$this->host.":".$this->port.$request->getPath().$request->getQueryString(); |
||
135 | |||
136 | // Includes the header in the output. We need this because our Response object will parse them. |
||
137 | // NOTE: we don't include header anymore, because we use the option CURLOPT_HEADERFUNCTION. |
||
138 | //$opts[CURLOPT_HEADER] = TRUE; |
||
139 | |||
140 | // Returns the transfer as a string of the return value of curl_exec() instead of outputting it out directly. |
||
141 | $opts[CURLOPT_RETURNTRANSFER] = TRUE; |
||
142 | |||
143 | // Sets the protocol version to be used. cURL constants have different values. |
||
144 | $opts[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_1_1; |
||
145 | |||
146 | // Sets basic authentication. |
||
147 | if (!empty($this->userName)) { |
||
148 | $opts[CURLOPT_HTTPAUTH] = CURLAUTH_BASIC; |
||
149 | $opts[CURLOPT_USERPWD] = $this->userName.":".$this->password; |
||
150 | } |
||
151 | |||
152 | // Sets the previous options. |
||
153 | curl_setopt_array($this->handle, $opts); |
||
154 | |||
155 | // This fix a known cURL bug: see http://the-stickman.com/web-development/php-and-curl-disabling-100-continue-header/ |
||
156 | // cURL sets the Expect header field automatically, ignoring the fact that a client may not need it for the specific |
||
157 | // request. |
||
158 | if (!$request->hasHeaderField(Request::EXPECT_HF)) |
||
159 | curl_setopt($this->handle, CURLOPT_HTTPHEADER, array("Expect:")); |
||
160 | |||
161 | // Sets the request header. |
||
162 | // Due to a stupid bug, using curl_setopt_array(), cURL doesn't override the Content-Type header field. So we must |
||
163 | // set the header using, instead, curl_stopt() |
||
164 | // $opts[CURLOPT_HTTPHEADER] = $request->getHeaderAsArray(); |
||
165 | curl_setopt($this->handle, CURLOPT_HTTPHEADER, $request->getHeaderAsArray()); |
||
166 | |||
167 | // Here we use this option because we might have a response without body. This may happen because we are supporting |
||
168 | // chunk responses, and sometimes we want trigger an hook function to let the user perform operations on coming |
||
169 | // chunks. |
||
170 | $header = ""; |
||
171 | curl_setopt($this->handle, CURLOPT_HEADERFUNCTION, |
||
172 | function($unused, $buffer) use (&$header) { |
||
173 | $header .= $buffer; |
||
174 | return strlen($buffer); |
||
175 | }); |
||
176 | |||
177 | // When the hook function is provided, we set the CURLOPT_WRITEFUNCTION so cURL will call the hook function for each |
||
178 | // response chunk read. |
||
179 | if (isset($chunkHook)) { |
||
180 | curl_setopt($this->handle, CURLOPT_WRITEFUNCTION, |
||
181 | function($unused, $buffer) use ($chunkHook) { |
||
182 | $chunkHook->process($buffer); |
||
183 | return strlen($buffer); |
||
184 | }); |
||
185 | } |
||
186 | |||
187 | if ($this->timeout) |
||
188 | curl_setopt($this->handle, CURLOPT_TIMEOUT, $this->timeout); |
||
189 | |||
190 | if ($result = curl_exec($this->handle)) { |
||
191 | $response = new Response($header); |
||
192 | $response->setBody($result); |
||
|
|||
193 | return $response; |
||
194 | } |
||
195 | else { |
||
196 | $error = curl_error($this->handle); |
||
197 | throw new \RuntimeException($error); |
||
198 | } |
||
201 | } |