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