| Conditions | 12 |
| Paths | 120 |
| Total Lines | 49 |
| Code Lines | 37 |
| 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 |
||
| 51 | protected static function cUrl($url, $data = array(), DateTime $modifiedSince = null, $ajax = false) |
||
| 52 | { |
||
| 53 | $url = 'https://' . self::$domain . '.amocrm.ru/' . $url; |
||
| 54 | $isUnsorted = mb_stripos($url, 'incoming_leads') !== false; |
||
| 55 | if ($isUnsorted) { |
||
| 56 | $url .= '&login=' . self::$userLogin . '&api_key=' . self::$userAPIKey; |
||
| 57 | } else { |
||
| 58 | if (mb_strpos($url, '?') === false) { |
||
| 59 | $url .= '?'; |
||
| 60 | } |
||
| 61 | $url .= '&USER_LOGIN=' . self::$userLogin . '&USER_HASH=' . self::$userAPIKey; |
||
| 62 | } |
||
| 63 | |||
| 64 | $curl = curl_init(); |
||
| 65 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
||
| 66 | curl_setopt($curl, CURLOPT_URL, $url); |
||
| 67 | curl_setopt($curl, CURLOPT_HEADER, false); |
||
| 68 | curl_setopt($curl, CURLOPT_USERAGENT, 'DrillCoder AmoCRM_Wrap/v' . AmoCRM::VERSION); |
||
| 69 | curl_setopt($curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2); |
||
| 70 | $headers = array(); |
||
| 71 | if (count($data) > 0) { |
||
| 72 | curl_setopt($curl, CURLOPT_POST, true); |
||
| 73 | if ($ajax) { |
||
| 74 | $headers[] = 'X-Requested-With: XMLHttpRequest'; |
||
| 75 | $dataStr = $data; |
||
| 76 | } elseif ($isUnsorted) { |
||
| 77 | $dataStr = http_build_query($data); |
||
| 78 | } else { |
||
| 79 | $headers[] = 'Content-Type: application/json'; |
||
| 80 | $dataStr = json_encode($data); |
||
| 81 | } |
||
| 82 | curl_setopt($curl, CURLOPT_POSTFIELDS, $dataStr); |
||
| 83 | } |
||
| 84 | if ($modifiedSince !== null) { |
||
| 85 | $headers[] = 'IF-MODIFIED-SINCE: ' . $modifiedSince->format(DateTime::RFC1123); |
||
| 86 | } |
||
| 87 | curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); |
||
| 88 | $json = curl_exec($curl); |
||
| 89 | curl_close($curl); |
||
| 90 | |||
| 91 | $result = json_decode($json); |
||
| 92 | if (isset($result->response->error) || (isset($result->title) && $result->title === 'Error')) { |
||
| 93 | $errorCode = isset($result->status) ? (int)$result->status : (int)$result->response->error_code; |
||
| 94 | $errorMessage = isset(Config::$errors[$errorCode]) ? Config::$errors[$errorCode] : $result->response->error; |
||
| 95 | |||
| 96 | throw new AmoWrapException($errorMessage, $errorCode); |
||
| 97 | } |
||
| 98 | |||
| 99 | return $result; |
||
| 100 | } |
||
| 111 | } |