| Conditions | 25 |
| Paths | 864 |
| Total Lines | 94 |
| Code Lines | 58 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 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 |
||
| 95 | protected function call($_path, $_method='GET', array $_payload=[], &$headers=[], $if_match=null, $return_dn=false, $retry=1) |
||
| 96 | { |
||
| 97 | $curl = curl_init(); |
||
| 98 | |||
| 99 | // fix error like: Request argument "policies" is not a "dict" (PHP encodes empty arrays as array, not object) |
||
| 100 | if (array_key_exists('policies', $_payload) && empty($_payload['policies'])) |
||
| 101 | { |
||
| 102 | $_payload['policies'] = new \stdClass(); // force "policies": {} |
||
| 103 | } |
||
| 104 | if (is_array($_payload['properties']) && array_key_exists('umcProperty', $_payload['properties']) && empty($_payload['properties']['umcProperty'])) |
||
| 105 | { |
||
| 106 | $_payload['properties']['umcProperty'] = new \stdClass(); // force "umcProperty": {} |
||
| 107 | } |
||
| 108 | |||
| 109 | $curlOpts = [ |
||
| 110 | CURLOPT_URL => 'https://'.$this->host.($_path[0] !== '/' ? self::PREFIX : '').$_path, |
||
| 111 | CURLOPT_USERPWD => $this->user.':'.$this->config['ldap_root_pw'], |
||
| 112 | //CURLOPT_SSL_VERIFYHOST => 2, // 0: to disable certificate check |
||
| 113 | CURLOPT_HTTPHEADER => [ |
||
| 114 | 'Accept: application/json', |
||
| 115 | ], |
||
| 116 | CURLOPT_CUSTOMREQUEST => $_method, |
||
| 117 | CURLOPT_RETURNTRANSFER => 1, |
||
| 118 | //CURLOPT_FOLLOWLOCATION => 1, |
||
| 119 | CURLOPT_TIMEOUT => 30, // setting a timeout of 30 seconds, as recommended by Univention |
||
| 120 | CURLOPT_VERBOSE => 1, |
||
| 121 | CURLOPT_HEADER => 1, |
||
| 122 | ]; |
||
| 123 | if (isset($if_match)) |
||
| 124 | { |
||
| 125 | $curlOpts[CURLOPT_HTTPHEADER][] = 'If-Match: '.$if_match; |
||
| 126 | } |
||
| 127 | switch($_method) |
||
| 128 | { |
||
| 129 | case 'PUT': |
||
| 130 | case 'POST': |
||
| 131 | $curlOpts[CURLOPT_HTTPHEADER][] = 'Content-Type: application/json'; |
||
| 132 | $curlOpts[CURLOPT_POSTFIELDS] = json_encode($_payload, JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE); |
||
| 133 | break; |
||
| 134 | |||
| 135 | case 'GET': |
||
| 136 | default: |
||
| 137 | if ($_payload) |
||
| 138 | { |
||
| 139 | $curlOpts[CURLOPT_URL] .= '?'. http_build_query($_payload); |
||
| 140 | } |
||
| 141 | break; |
||
| 142 | } |
||
| 143 | curl_setopt_array($curl, $curlOpts); |
||
| 144 | $response = curl_exec($curl); |
||
| 145 | |||
| 146 | $header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE); |
||
| 147 | $headers = self::getHeaders(substr($response, 0, $header_size)); |
||
| 148 | $body = substr($response, $header_size); |
||
| 149 | |||
| 150 | $path = urldecode($_path); // for nicer error-messages |
||
| 151 | if ($response === false || $body !== '' && !($json = json_decode($body, true)) && json_last_error()) |
||
| 152 | { |
||
| 153 | $info = curl_getinfo($curl); |
||
| 154 | curl_close($curl); |
||
| 155 | if ($retry > 0) |
||
| 156 | { |
||
| 157 | error_log(__METHOD__."($path, $_method, ...) failed, retrying in 100ms, returned $body, headers=".json_encode($headers, JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE).", curl_getinfo()=".json_encode($info, JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE)); |
||
| 158 | usleep(100000); |
||
| 159 | return $this->call($_path, $_method, $_payload, $headers, $if_match, $return_dn, --$retry); |
||
| 160 | } |
||
| 161 | error_log(__METHOD__."($path, $_method, ...) returned $body, headers=".json_encode($headers, JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE).", curl_getinfo()=".json_encode($info, JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE)); |
||
| 162 | error_log(__METHOD__."($path, $_method, ".json_encode($_payload, JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE).")"); |
||
| 163 | throw new UdmCantConnect("Error contacting Univention UDM REST Api ($path)".($response !== false ? ': '.json_last_error() : '')); |
||
| 164 | } |
||
| 165 | curl_close($curl); |
||
| 166 | // error in json or non 20x http status |
||
| 167 | if (!empty($json['error']) || !preg_match('|^HTTP/[0-9.]+ 20|', $headers[0])) |
||
| 168 | { |
||
| 169 | error_log(__METHOD__."($path, $_method, ...) returned $response, headers=".json_encode($headers, JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE)); |
||
| 170 | error_log(__METHOD__."($path, $_method, ".json_encode($_payload, JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE).")"); |
||
| 171 | throw new UdmError("UDM REST Api ($path): ".(empty($json['error']['message']) ? $headers[0] : $json['error']['message']), $json['error']['code']); |
||
| 172 | } |
||
| 173 | if (self::DEBUG) |
||
| 174 | { |
||
| 175 | error_log(__METHOD__."($path, $_method, ...) returned $response, headers=".json_encode($headers, JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE)); |
||
| 176 | error_log(__METHOD__."($path, $_method, ".json_encode($_payload, JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE).")"); |
||
| 177 | } |
||
| 178 | |||
| 179 | if ($return_dn) |
||
| 180 | { |
||
| 181 | $matches = null; |
||
| 182 | if (!isset($headers['location']) || !preg_match('|/([^/]+)$|', $headers['location'], $matches)) |
||
| 183 | { |
||
| 184 | throw new UdmMissingLocation("UDM REST Api ($path) did not return Location header!"); |
||
| 185 | } |
||
| 186 | return urldecode($matches[1]); |
||
| 187 | } |
||
| 188 | return $json; |
||
| 189 | } |
||
| 393 | } |