Passed
Push — master ( d8671c...01d1e3 )
by Alex
03:39
created

CurlWrapper::packArray()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 15
rs 9.9666
cc 4
nc 4
nop 2
1
<?php
2
namespace Mezon\CustomClient;
3
4
/**
5
 * Class CurlWrapper
6
 *
7
 * @package CustomClient
8
 * @subpackage CurlWrapper
9
 * @author Dodonov A.A.
10
 * @version v.1.0 (2019/08/07)
11
 * @copyright Copyright (c) 2019, aeon.org
12
 */
13
14
/**
15
 * Wrapper for CURL routines
16
 */
17
class CurlWrapper
18
{
19
20
    /**
21
     * Does the specified header exists
22
     *
23
     * @param array $headers
24
     *            list of headers to be analized
25
     * @param string $requiredHeader
26
     *            header to be found
27
     * @return bool true if the header was found, false otherwise
28
     */
29
    public static function isHeaderExists(array $headers, string $requiredHeader): bool
30
    {
31
        foreach ($headers as $header) {
32
            if (stripos($header, $requiredHeader) !== false) {
33
                return true;
34
            }
35
        }
36
37
        return false;
38
    }
39
40
    /**
41
     * Method packs arrays
42
     *
43
     * @param string $fieldName
44
     *            field name
45
     * @param array $data
46
     *            array data to be packed
47
     * @return array packed array data
48
     */
49
    public static function packArray(string $fieldName, array $data): array
50
    {
51
        $formData = [];
52
53
        foreach ($data as $key => $value) {
54
            if (is_scalar($value)) {
55
                $formData[] = $fieldName . '[' . $key . ']=' . urlencode($value);
56
            } elseif (is_array($value)) {
57
                $formData = array_merge($formData, self::packArray($fieldName . '[' . $key . ']', $value));
58
            } else {
59
                throw (new \Exception('Data type is not supported'));
60
            }
61
        }
62
63
        return $formData;
64
    }
65
66
    /**
67
     * Method packs data in string
68
     *
69
     * @param array $data
70
     *            data to be packed
71
     * @return string packed data
72
     */
73
    public static function packData(array $data): string
74
    {
75
        $formData = [];
76
77
        foreach ($data as $key => $value) {
78
            if (is_scalar($value)) {
79
                $formData[] = $key . '=' . urlencode($value);
80
            } elseif (is_array($value)) {
81
                $formData = array_merge($formData, self::packArray($key, $value));
82
            } else {
83
                throw (new \Exception('Data type is not supported'));
84
            }
85
        }
86
87
        return implode('&', $formData);
88
    }
89
90
    /**
91
     * Method send HTTP request
92
     *
93
     * @param string $url
94
     *            URL
95
     * @param array $headers
96
     *            Headers
97
     * @param string $method
98
     *            Request HTTP Method
99
     * @param array $data
100
     *            Request data
101
     * @return array Response body and HTTP code
102
     */
103
    public static function sendRequest(string $url, array $headers, string $method, array $data = []): array
104
    {
105
        $ch = curl_init();
106
107
        $curlConfig = [
108
            CURLOPT_URL => $url,
109
            CURLOPT_HTTPHEADER => $headers,
110
            CURLOPT_POST => ($method == 'POST'),
111
            CURLOPT_RETURNTRANSFER => true,
112
            CURLOPT_SSL_VERIFYPEER => true
113
        ];
114
115
        if ($method === 'POST') {
116
            if (self::isHeaderExists($headers, 'Content-type: application/json')) {
117
                $formData = json_encode($data);
118
            } else {
119
                $formData = self::packData($data);
120
            }
121
122
            $curlConfig[CURLOPT_POSTFIELDS] = $formData;
123
        }
124
125
        curl_setopt_array($ch, $curlConfig);
126
127
        $body = curl_exec($ch);
128
        $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
129
130
        curl_close($ch);
131
132
        return [
133
            $body,
134
            $code
135
        ];
136
    }
137
}
138