| Conditions | 28 |
| Paths | > 20000 |
| Total Lines | 126 |
| Code Lines | 73 |
| 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 |
||
| 44 | public static function getResponse(array $parameters) |
||
| 45 | { |
||
| 46 | $parameters['server'] = parse_url($parameters['url']); |
||
| 47 | |||
| 48 | if (!isset($parameters['server']['port'])) { |
||
| 49 | $parameters['server']['port'] = ($parameters['server']['scheme'] == 'https') ? 443 : 80; |
||
| 50 | } |
||
| 51 | |||
| 52 | if (!isset($parameters['server']['path'])) { |
||
| 53 | $parameters['server']['path'] = '/'; |
||
| 54 | } |
||
| 55 | |||
| 56 | if (isset($parameters['server']['user']) && isset($parameters['server']['pass'])) { |
||
| 57 | $parameters['headers'][] = 'Authorization: Basic ' . base64_encode($parameters['server']['user'] . ':' . $parameters['server']['pass']); |
||
| 58 | } |
||
| 59 | |||
| 60 | unset($parameters['url']); |
||
| 61 | |||
| 62 | if (!isset($parameters['headers']) || !is_array($parameters['headers'])) { |
||
| 63 | $parameters['headers'] = []; |
||
| 64 | } |
||
| 65 | |||
| 66 | if (!isset($parameters['method'])) { |
||
| 67 | if (isset($parameters['parameters'])) { |
||
| 68 | $parameters['method'] = 'post'; |
||
| 69 | } else { |
||
| 70 | $parameters['method'] = 'get'; |
||
| 71 | } |
||
| 72 | } |
||
| 73 | |||
| 74 | $curl = curl_init($parameters['server']['scheme'] . '://' . $parameters['server']['host'] . $parameters['server']['path'] . (isset($parameters['server']['query']) ? '?' . $parameters['server']['query'] : '')); |
||
| 75 | |||
| 76 | $curl_options = [ |
||
| 77 | CURLOPT_PORT => $parameters['server']['port'], |
||
| 78 | CURLOPT_HEADER => true, |
||
| 79 | CURLOPT_RETURNTRANSFER => true, |
||
| 80 | CURLOPT_FORBID_REUSE => true, |
||
| 81 | CURLOPT_FRESH_CONNECT => true, |
||
| 82 | CURLOPT_ENCODING => '', // disable gzip |
||
| 83 | CURLOPT_FOLLOWLOCATION => false // does not work with open_basedir so a workaround is implemented below |
||
| 84 | ]; |
||
| 85 | |||
| 86 | if (!empty($parameters['headers'])) { |
||
| 87 | $curl_options[CURLOPT_HTTPHEADER] = $parameters['headers']; |
||
| 88 | } |
||
| 89 | |||
| 90 | if ($parameters['server']['scheme'] == 'https') { |
||
| 91 | if (!isset($parameters['verify_ssl']) || ($parameters['verify_ssl'] === true)) { |
||
| 92 | $curl_options[CURLOPT_SSL_VERIFYPEER] = true; |
||
| 93 | $curl_options[CURLOPT_SSL_VERIFYHOST] = 2; |
||
| 94 | } else { |
||
| 95 | $curl_options[CURLOPT_SSL_VERIFYPEER] = false; |
||
| 96 | $curl_options[CURLOPT_SSL_VERIFYHOST] = false; |
||
| 97 | } |
||
| 98 | |||
| 99 | if (!isset($parameters['cafile'])) { |
||
| 100 | $parameters['cafile'] = OSCOM::getConfig('dir_root', 'Shop') . 'includes/cacert.pem'; |
||
| 101 | } |
||
| 102 | |||
| 103 | if (is_file($parameters['cafile'])) { |
||
| 104 | $curl_options[CURLOPT_CAINFO] = $parameters['cafile']; |
||
| 105 | } |
||
| 106 | |||
| 107 | if (isset($parameters['certificate'])) { |
||
| 108 | $curl_options[CURLOPT_SSLCERT] = $parameters['certificate']; |
||
| 109 | } |
||
| 110 | } |
||
| 111 | |||
| 112 | if ($parameters['method'] == 'post') { |
||
| 113 | if (!isset($parameters['parameters'])) { |
||
| 114 | $parameters['parameters'] = ''; |
||
| 115 | } |
||
| 116 | |||
| 117 | $curl_options[CURLOPT_POST] = true; |
||
| 118 | $curl_options[CURLOPT_POSTFIELDS] = $parameters['parameters']; |
||
| 119 | } |
||
| 120 | |||
| 121 | if (isset($parameters['proxy']) && !empty($parameters['proxy'])) { |
||
| 122 | $curl_options[CURLOPT_HTTPPROXYTUNNEL] = true; |
||
| 123 | $curl_options[CURLOPT_PROXY] = $parameters['proxy']; |
||
| 124 | } |
||
| 125 | |||
| 126 | curl_setopt_array($curl, $curl_options); |
||
| 127 | $result = curl_exec($curl); |
||
| 128 | |||
| 129 | if ($result === false) { |
||
| 130 | trigger_error(curl_error($curl)); |
||
| 131 | |||
| 132 | curl_close($curl); |
||
| 133 | |||
| 134 | return false; |
||
| 135 | } |
||
| 136 | |||
| 137 | $http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); |
||
| 138 | |||
| 139 | $header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE); |
||
| 140 | $headers = trim(substr($result, 0, $header_size)); |
||
| 141 | $body = substr($result, $header_size); |
||
| 142 | |||
| 143 | curl_close($curl); |
||
| 144 | |||
| 145 | if (($http_code == 301) || ($http_code == 302)) { |
||
| 146 | if (!isset($parameters['redir_counter']) || ($parameters['redir_counter'] < 6)) { |
||
| 147 | if (!isset($parameters['redir_counter'])) { |
||
| 148 | $parameters['redir_counter'] = 0; |
||
| 149 | } |
||
| 150 | |||
| 151 | $matches = []; |
||
| 152 | preg_match('/(Location:|URI:)(.*?)\n/i', $headers, $matches); |
||
| 153 | |||
| 154 | $redir_url = trim(array_pop($matches)); |
||
| 155 | |||
| 156 | $parameters['redir_counter']++; |
||
| 157 | |||
| 158 | $redir_params = [ |
||
| 159 | 'url' => $redir_url, |
||
| 160 | 'method' => $parameters['method'], |
||
| 161 | 'redir_counter', $parameters['redir_counter'] |
||
| 162 | ]; |
||
| 163 | |||
| 164 | $body = static::getResponse($redir_params); |
||
| 165 | } |
||
| 166 | } |
||
| 167 | |||
| 168 | return $body; |
||
| 169 | } |
||
| 170 | |||
| 214 |