Completed
Push — master ( f22d83...d711c3 )
by Denis
02:01
created

Curl::getError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 2
crap 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: dp
5
 * Date: 26.07.17
6
 * Time: 12:06
7
 */
8
9
namespace Lan\Ebs\Sdk\Helper;
10
11
use Exception;
12
13
class Curl
14
{
15
    /**
16
     * @param $host
17
     * @param $url
18
     * @param $method
19
     * @param $token
20
     * @param array $params
21
     * @return array|mixed
22
     * @throws Exception
23
     */
24 16
    public static function getResponse($host, $url, $method, $token, array $params)
25
    {
26 16
        $curl = curl_init();
27
28
        switch ($method) {
29 16
            case 'GET':
30 14
                if (!empty($params)) {
31 10
                    $url = sprintf("%s?%s", $url, http_build_query($params));
32 10
                };
33
34 14
                break;
35 3
            case 'POST':
36 3
            case 'PUT':
37 3
            case 'DELETE':
38 3
                if (!empty($params)) {
39 2
                    curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($params));
40 2
                }
41 3
                break;
42
            default:
43
                throw new Exception('Method ' . $method . ' unknown');
44
        }
45
46
        $headers = [
47 16
            'X-Auth-Token: ' . $token,
48 16
            'Content-Type: application/x-www-form-urlencoded; charset=utf-8',
49
            'Accept: application/json'
50 16
        ];
51
52 16
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
53 16
        curl_setopt($curl, CURLOPT_URL, $host . $url);
54 16
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
55 16
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
56
57 16
        $curlResult = curl_exec($curl);
58
59 16
        Debuger::dump($method . ' ' . $host . $url . ' ' . ($method == 'POST' || $method == 'PUT' || $method == 'DELETE' ? Curl::arrayPrettyPrint($params) : '') .  '[' . (curl_errno($curl) ? 500 : curl_getinfo($curl)['http_code']) . ']');
60
61 16
        if (curl_errno($curl)) {
62
            return Curl::getError('Curl error: ' . curl_errno($curl), 500);
63
        }
64
65 16
        $response = json_decode($curlResult, true);
66
67 16
        if (json_last_error()) {
68
            return Curl::getError('JSON error: ' . json_last_error_msg() . "\n" . $curlResult, curl_getinfo($curl)['http_code']);
69
        }
70
71 16
        if (empty($response)) {
72
            return Curl::getError('Response is empty', curl_getinfo($curl)['http_code']);
73
        }
74
75 16
        return $response;
76
    }
77
78
    private static function getError($message, $code)
79
    {
80
        return [
81
            'type' => 'none',
82
            'data' => null,
83
            'count' => 0,
84
            'status' => $code,
85
            'message' => $message
86
        ];
87
    }
88
89 3
    private static function arrayPrettyPrint(array $array) {
90 3
        return str_replace('Array (', '(', preg_replace('/\s{2,}/', ' ', preg_replace('/[\x00-\x1F\x7F ]/', ' ', print_r($array, true))));
91
    }
92
}