Conditions | 31 |
Paths | > 20000 |
Total Lines | 182 |
Code Lines | 96 |
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 |
||
43 | public static function factory(RequestInterface $request) |
||
44 | { |
||
45 | $requestCurlOptions = $request->getCurlOptions(); |
||
46 | $mediator = new RequestMediator($request, $requestCurlOptions->get('emit_io')); |
||
47 | $tempContentLength = null; |
||
48 | $method = $request->getMethod(); |
||
49 | $bodyAsString = $requestCurlOptions->get(self::BODY_AS_STRING); |
||
50 | |||
51 | // Prepare url |
||
52 | $url = (string)$request->getUrl(); |
||
53 | if(($pos = strpos($url, '#')) !== false ){ |
||
54 | // strip fragment from url |
||
55 | $url = substr($url, 0, $pos); |
||
56 | } |
||
57 | |||
58 | // Array of default cURL options. |
||
59 | $curlOptions = array( |
||
60 | CURLOPT_URL => $url, |
||
61 | CURLOPT_CONNECTTIMEOUT => 150, |
||
62 | CURLOPT_RETURNTRANSFER => false, |
||
63 | CURLOPT_HEADER => false, |
||
64 | CURLOPT_PORT => $request->getPort(), |
||
65 | CURLOPT_HTTPHEADER => array(), |
||
66 | CURLOPT_WRITEFUNCTION => array($mediator, 'writeResponseBody'), |
||
67 | CURLOPT_HEADERFUNCTION => array($mediator, 'receiveResponseHeader'), |
||
68 | CURLOPT_HTTP_VERSION => $request->getProtocolVersion() === '1.0' |
||
69 | ? CURL_HTTP_VERSION_1_0 : CURL_HTTP_VERSION_1_1, |
||
70 | // Verifies the authenticity of the peer's certificate |
||
71 | CURLOPT_SSL_VERIFYPEER => 1, |
||
72 | // Certificate must indicate that the server is the server to which you meant to connect |
||
73 | CURLOPT_SSL_VERIFYHOST => 2 |
||
74 | ); |
||
75 | |||
76 | if (defined('CURLOPT_PROTOCOLS')) { |
||
77 | // Allow only HTTP and HTTPS protocols |
||
78 | $curlOptions[CURLOPT_PROTOCOLS] = CURLPROTO_HTTP | CURLPROTO_HTTPS; |
||
79 | } |
||
80 | |||
81 | // Add CURLOPT_ENCODING if Accept-Encoding header is provided |
||
82 | if ($acceptEncodingHeader = $request->getHeader('Accept-Encoding')) { |
||
83 | $curlOptions[CURLOPT_ENCODING] = (string) $acceptEncodingHeader; |
||
84 | // Let cURL set the Accept-Encoding header, prevents duplicate values |
||
85 | $request->removeHeader('Accept-Encoding'); |
||
86 | } |
||
87 | |||
88 | // Enable curl debug information if the 'debug' param was set |
||
89 | if ($requestCurlOptions->get('debug')) { |
||
90 | $curlOptions[CURLOPT_STDERR] = fopen('php://temp', 'r+'); |
||
91 | // @codeCoverageIgnoreStart |
||
92 | if (false === $curlOptions[CURLOPT_STDERR]) { |
||
93 | throw new RuntimeException('Unable to create a stream for CURLOPT_STDERR'); |
||
94 | } |
||
95 | // @codeCoverageIgnoreEnd |
||
96 | $curlOptions[CURLOPT_VERBOSE] = true; |
||
97 | } |
||
98 | |||
99 | // Specify settings according to the HTTP method |
||
100 | if ($method == 'GET') { |
||
101 | $curlOptions[CURLOPT_HTTPGET] = true; |
||
102 | } elseif ($method == 'HEAD') { |
||
103 | $curlOptions[CURLOPT_NOBODY] = true; |
||
104 | // HEAD requests do not use a write function |
||
105 | unset($curlOptions[CURLOPT_WRITEFUNCTION]); |
||
106 | } elseif (!($request instanceof EntityEnclosingRequest)) { |
||
107 | $curlOptions[CURLOPT_CUSTOMREQUEST] = $method; |
||
108 | } else { |
||
109 | |||
110 | $curlOptions[CURLOPT_CUSTOMREQUEST] = $method; |
||
111 | |||
112 | // Handle sending raw bodies in a request |
||
113 | if ($request->getBody()) { |
||
114 | // You can send the body as a string using curl's CURLOPT_POSTFIELDS |
||
115 | if ($bodyAsString) { |
||
116 | $curlOptions[CURLOPT_POSTFIELDS] = (string) $request->getBody(); |
||
117 | // Allow curl to add the Content-Length for us to account for the times when |
||
118 | // POST redirects are followed by GET requests |
||
119 | if ($tempContentLength = $request->getHeader('Content-Length')) { |
||
120 | $tempContentLength = (int) (string) $tempContentLength; |
||
121 | } |
||
122 | // Remove the curl generated Content-Type header if none was set manually |
||
123 | if (!$request->hasHeader('Content-Type')) { |
||
124 | $curlOptions[CURLOPT_HTTPHEADER][] = 'Content-Type:'; |
||
125 | } |
||
126 | } else { |
||
127 | $curlOptions[CURLOPT_UPLOAD] = true; |
||
128 | // Let cURL handle setting the Content-Length header |
||
129 | if ($tempContentLength = $request->getHeader('Content-Length')) { |
||
130 | $tempContentLength = (int) (string) $tempContentLength; |
||
131 | $curlOptions[CURLOPT_INFILESIZE] = $tempContentLength; |
||
132 | } |
||
133 | // Add a callback for curl to read data to send with the request only if a body was specified |
||
134 | $curlOptions[CURLOPT_READFUNCTION] = array($mediator, 'readRequestBody'); |
||
135 | // Attempt to seek to the start of the stream |
||
136 | $request->getBody()->seek(0); |
||
137 | } |
||
138 | |||
139 | } else { |
||
140 | |||
141 | // Special handling for POST specific fields and files |
||
142 | $postFields = false; |
||
143 | if (count($request->getPostFiles())) { |
||
144 | $postFields = $request->getPostFields()->useUrlEncoding(false)->urlEncode(); |
||
145 | foreach ($request->getPostFiles() as $key => $data) { |
||
146 | $prefixKeys = count($data) > 1; |
||
147 | foreach ($data as $index => $file) { |
||
148 | // Allow multiple files in the same key |
||
149 | $fieldKey = $prefixKeys ? "{$key}[{$index}]" : $key; |
||
150 | $postFields[$fieldKey] = $file->getCurlValue(); |
||
151 | } |
||
152 | } |
||
153 | } elseif (count($request->getPostFields())) { |
||
154 | $postFields = (string) $request->getPostFields()->useUrlEncoding(true); |
||
155 | } |
||
156 | |||
157 | if ($postFields !== false) { |
||
158 | if ($method == 'POST') { |
||
159 | unset($curlOptions[CURLOPT_CUSTOMREQUEST]); |
||
160 | $curlOptions[CURLOPT_POST] = true; |
||
161 | } |
||
162 | $curlOptions[CURLOPT_POSTFIELDS] = $postFields; |
||
163 | $request->removeHeader('Content-Length'); |
||
164 | } |
||
165 | } |
||
166 | |||
167 | // If the Expect header is not present, prevent curl from adding it |
||
168 | if (!$request->hasHeader('Expect')) { |
||
169 | $curlOptions[CURLOPT_HTTPHEADER][] = 'Expect:'; |
||
170 | } |
||
171 | } |
||
172 | |||
173 | // If a Content-Length header was specified but we want to allow curl to set one for us |
||
174 | if (null !== $tempContentLength) { |
||
175 | $request->removeHeader('Content-Length'); |
||
176 | } |
||
177 | |||
178 | // Set custom cURL options |
||
179 | foreach ($requestCurlOptions->toArray() as $key => $value) { |
||
180 | if (is_numeric($key)) { |
||
181 | $curlOptions[$key] = $value; |
||
182 | } |
||
183 | } |
||
184 | |||
185 | // Do not set an Accept header by default |
||
186 | if (!isset($curlOptions[CURLOPT_ENCODING])) { |
||
187 | $curlOptions[CURLOPT_HTTPHEADER][] = 'Accept:'; |
||
188 | } |
||
189 | |||
190 | // Add any custom headers to the request. Empty headers will cause curl to not send the header at all. |
||
191 | foreach ($request->getHeaderLines() as $line) { |
||
192 | $curlOptions[CURLOPT_HTTPHEADER][] = $line; |
||
193 | } |
||
194 | |||
195 | // Add the content-length header back if it was temporarily removed |
||
196 | if (null !== $tempContentLength) { |
||
197 | $request->setHeader('Content-Length', $tempContentLength); |
||
198 | } |
||
199 | |||
200 | // Apply the options to a new cURL handle. |
||
201 | $handle = curl_init(); |
||
202 | |||
203 | // Enable the progress function if the 'progress' param was set |
||
204 | if ($requestCurlOptions->get('progress')) { |
||
205 | // Wrap the function in a function that provides the curl handle to the mediator's progress function |
||
206 | // Using this rather than injecting the handle into the mediator prevents a circular reference |
||
207 | $curlOptions[CURLOPT_PROGRESSFUNCTION] = function () use ($mediator, $handle) { |
||
208 | $args = func_get_args(); |
||
209 | $args[] = $handle; |
||
210 | |||
211 | // PHP 5.5 pushed the handle onto the start of the args |
||
212 | if (is_resource($args[0])) { |
||
213 | array_shift($args); |
||
214 | } |
||
215 | |||
216 | call_user_func_array(array($mediator, 'progress'), $args); |
||
217 | }; |
||
218 | $curlOptions[CURLOPT_NOPROGRESS] = false; |
||
219 | } |
||
220 | |||
221 | curl_setopt_array($handle, $curlOptions); |
||
222 | |||
223 | return new static($handle, $curlOptions); |
||
224 | } |
||
225 | |||
465 |