| Conditions | 19 | 
| Paths | 137 | 
| Total Lines | 99 | 
| Code Lines | 61 | 
| 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  | 
            ||
| 92 | protected function executeRequest(RequestInterface $request)  | 
            ||
| 93 |     { | 
            ||
| 94 | $this->logger->debug(  | 
            ||
| 95 |             sprintf('HTTP Request %s %s', $request->getMethod(), $request->getUrl()), | 
            ||
| 96 |             array('headers' => $request->getHeaders(), 'body' => $request->toArray()) | 
            ||
| 97 | );  | 
            ||
| 98 | |||
| 99 |         if (!$request->validate()) { | 
            ||
| 100 |             throw new \RuntimeException('Request entity is not valid'); | 
            ||
| 101 | }  | 
            ||
| 102 | |||
| 103 | $ch = curl_init();  | 
            ||
| 104 | |||
| 105 |         if (!is_resource($ch)) { | 
            ||
| 106 |             throw new \RuntimeException('Could not get cURL resource'); | 
            ||
| 107 | }  | 
            ||
| 108 | |||
| 109 | $options = array(  | 
            ||
| 110 | CURLOPT_URL => $request->getUrl(),  | 
            ||
| 111 | CURLOPT_HTTPHEADER => $request->getHeaders(),  | 
            ||
| 112 | CURLOPT_HTTP_VERSION => $request->getProtocolVersion(),  | 
            ||
| 113 | );  | 
            ||
| 114 | |||
| 115 | $customMethods = array(  | 
            ||
| 116 | Request::METHOD_PUT,  | 
            ||
| 117 | Request::METHOD_PATCH,  | 
            ||
| 118 | Request::METHOD_DELETE,  | 
            ||
| 119 | );  | 
            ||
| 120 | |||
| 121 |         if ($request->getMethod() === Request::METHOD_POST) { | 
            ||
| 122 | $options[CURLOPT_POST] = true;  | 
            ||
| 123 | $options[CURLOPT_POSTFIELDS] = $request->toArray();  | 
            ||
| 124 |         } elseif ($request->getMethod() === Request::METHOD_GET && $request->toArray()) { | 
            ||
| 125 |             $paramChar = strpos('?', $request->getUrl()) === false ? '?' : '&'; | 
            ||
| 126 | $options[CURLOPT_URL] = $request->getUrl() . $paramChar . http_build_query($request->toArray());  | 
            ||
| 127 |         } elseif (in_array($request->getMethod(), $customMethods)) { | 
            ||
| 128 | $options[CURLOPT_CUSTOMREQUEST] = $request->getMethod();  | 
            ||
| 129 | $options[CURLOPT_POSTFIELDS] = $request->toArray();  | 
            ||
| 130 | }  | 
            ||
| 131 | |||
| 132 | if (isset($options[CURLOPT_POSTFIELDS])  | 
            ||
| 133 |             && $request->getHeader('Content-Type') == 'application/x-www-form-urlencoded' | 
            ||
| 134 |         ) { | 
            ||
| 135 | $options[CURLOPT_POSTFIELDS] = http_build_query($options[CURLOPT_POSTFIELDS]);  | 
            ||
| 136 | } elseif (isset($options[CURLOPT_POSTFIELDS])  | 
            ||
| 137 |             && $request->getHeader('Content-Type') == 'application/json' | 
            ||
| 138 |         ) { | 
            ||
| 139 | $options[CURLOPT_POSTFIELDS] = json_encode($options[CURLOPT_POSTFIELDS]);  | 
            ||
| 140 | }  | 
            ||
| 141 | |||
| 142 | curl_setopt_array($ch, $this->getRequestOptions($options));  | 
            ||
| 143 | |||
| 144 | $result = curl_exec($ch);  | 
            ||
| 145 | $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);  | 
            ||
| 146 | $errorMessage = curl_error($ch);  | 
            ||
| 147 | $errorNumber = curl_errno($ch);  | 
            ||
| 148 | |||
| 149 | curl_close($ch);  | 
            ||
| 150 | |||
| 151 | $this->logger->debug(  | 
            ||
| 152 |             sprintf('HTTP Response %s %s', $request->getMethod(), $request->getUrl()), | 
            ||
| 153 |             array('httpCode' => $httpCode, 'body' => $result, 'curlError' => $errorMessage) | 
            ||
| 154 | );  | 
            ||
| 155 | |||
| 156 |         if ($errorNumber !== 0) { | 
            ||
| 157 | throw new PayeverCommunicationException($errorMessage, $errorNumber);  | 
            ||
| 158 | }  | 
            ||
| 159 | |||
| 160 |         if ($httpCode >= 400) { | 
            ||
| 161 | $message = $result;  | 
            ||
| 162 | $data = @json_decode($result, true);  | 
            ||
| 163 | |||
| 164 |             if (isset($data['error_description'])) { | 
            ||
| 165 | $message = $data['error_description'];  | 
            ||
| 166 |             } elseif (isset($data['message'])) { | 
            ||
| 167 | $message = $data['message'];  | 
            ||
| 168 | }  | 
            ||
| 169 | |||
| 170 |             if (isset($data['error'])) { | 
            ||
| 171 |                 $message = sprintf('%s: %s', $data['error'], $message ?: 'Unknown'); | 
            ||
| 172 | }  | 
            ||
| 173 | |||
| 174 | throw new PayeverCommunicationException($message, $httpCode);  | 
            ||
| 175 | }  | 
            ||
| 176 | |||
| 177 | $response = new Response();  | 
            ||
| 178 | |||
| 179 | $response  | 
            ||
| 180 | ->setRequestEntity($request->getRequestEntity())  | 
            ||
| 181 | ->setResponseEntity($request->getResponseEntity())  | 
            ||
| 182 | ->load($result);  | 
            ||
| 183 | |||
| 184 |         if ($response->isFailed()) { | 
            ||
| 185 | throw new PayeverCommunicationException(  | 
            ||
| 186 | $response->getResponseEntity()->getErrorDescription()  | 
            ||
| 187 | );  | 
            ||
| 188 | }  | 
            ||
| 189 | |||
| 190 | return $response;  | 
            ||
| 191 | }  | 
            ||
| 215 |