Conditions | 15 |
Paths | 120 |
Total Lines | 91 |
Code Lines | 56 |
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 |
||
131 | public function execute($data) |
||
132 | { |
||
133 | //Initialize the logger |
||
134 | $this->logger->info($this->httpConfig->getMethod() . ' ' . $this->httpConfig->getUrl()); |
||
135 | |||
136 | //Initialize Curl Options |
||
137 | $ch = curl_init($this->httpConfig->getUrl()); |
||
138 | $options = $this->httpConfig->getCurlOptions(); |
||
139 | if (empty($options[CURLOPT_HTTPHEADER])) { |
||
140 | unset($options[CURLOPT_HTTPHEADER]); |
||
141 | } |
||
142 | curl_setopt_array($ch, $options); |
||
143 | curl_setopt($ch, CURLOPT_URL, $this->httpConfig->getUrl()); |
||
144 | curl_setopt($ch, CURLOPT_HEADER, false); |
||
145 | curl_setopt($ch, CURLINFO_HEADER_OUT, true); |
||
146 | curl_setopt($ch, CURLOPT_HTTPHEADER, $this->getHttpHeaders()); |
||
147 | |||
148 | //Determine Curl Options based on Method |
||
149 | switch ($this->httpConfig->getMethod()) { |
||
150 | case 'POST': |
||
151 | curl_setopt($ch, CURLOPT_POST, true); |
||
152 | curl_setopt($ch, CURLOPT_POSTFIELDS, $data); |
||
153 | break; |
||
154 | case 'PUT': |
||
155 | case 'PATCH': |
||
156 | case 'DELETE': |
||
157 | curl_setopt($ch, CURLOPT_POSTFIELDS, $data); |
||
158 | break; |
||
159 | } |
||
160 | |||
161 | //Default Option if Method not of given types in switch case |
||
162 | if ($this->httpConfig->getMethod() != null) { |
||
163 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->httpConfig->getMethod()); |
||
164 | } |
||
165 | |||
166 | $this->responseHeaders = array(); |
||
167 | $this->skippedHttpStatusLine = false; |
||
168 | curl_setopt($ch, CURLOPT_HEADERFUNCTION, array($this, 'parseResponseHeaders')); |
||
169 | |||
170 | //Execute Curl Request |
||
171 | $result = curl_exec($ch); |
||
172 | //Retrieve Response Status |
||
173 | $httpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
||
174 | |||
175 | //Retry if Certificate Exception |
||
176 | if (curl_errno($ch) == 60) { |
||
177 | $this->logger->info("Invalid or no certificate authority found - Retrying using bundled CA certs file"); |
||
178 | curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem'); |
||
179 | $result = curl_exec($ch); |
||
180 | //Retrieve Response Status |
||
181 | $httpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
||
182 | } |
||
183 | |||
184 | //Throw Exception if Retries and Certificates doenst work |
||
185 | if (curl_errno($ch)) { |
||
186 | $ex = new PayPalConnectionException( |
||
187 | $this->httpConfig->getUrl(), |
||
188 | curl_error($ch), |
||
189 | curl_errno($ch) |
||
190 | ); |
||
191 | curl_close($ch); |
||
192 | throw $ex; |
||
193 | } |
||
194 | |||
195 | // Get Request and Response Headers |
||
196 | $requestHeaders = curl_getinfo($ch, CURLINFO_HEADER_OUT); |
||
197 | $this->logger->debug("Request Headers \t: " . str_replace("\r\n", ", ", $requestHeaders)); |
||
198 | $this->logger->debug(($data && $data != '' ? "Request Data\t\t: " . $data : "No Request Payload") . "\n" . str_repeat('-', 128) . "\n"); |
||
199 | $this->logger->info("Response Status \t: " . $httpStatus); |
||
200 | $this->logger->debug("Response Headers\t: " . $this->implodeArray($this->responseHeaders)); |
||
201 | |||
202 | //Close the curl request |
||
203 | curl_close($ch); |
||
204 | |||
205 | //More Exceptions based on HttpStatus Code |
||
206 | if ($httpStatus < 200 || $httpStatus >= 300) { |
||
207 | $ex = new PayPalConnectionException( |
||
208 | $this->httpConfig->getUrl(), |
||
209 | "Got Http response code $httpStatus when accessing {$this->httpConfig->getUrl()}.", |
||
210 | $httpStatus |
||
211 | ); |
||
212 | $ex->setData($result); |
||
213 | $this->logger->error("Got Http response code $httpStatus when accessing {$this->httpConfig->getUrl()}. " . $result); |
||
214 | $this->logger->debug("\n\n" . str_repeat('=', 128) . "\n"); |
||
215 | throw $ex; |
||
216 | } |
||
217 | |||
218 | $this->logger->debug(($result && $result != '' ? "Response Data \t: " . $result : "No Response Body") . "\n\n" . str_repeat('=', 128) . "\n"); |
||
219 | |||
220 | //Return result object |
||
221 | return $result; |
||
222 | } |
||
224 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.