Completed
Push — main ( 293a96...661ea7 )
by Dylan
02:41 queued 02:41
created

Curl   A

Complexity

Total Complexity 35

Size/Duplication

Total Lines 223
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 82
c 2
b 0
f 0
dl 0
loc 223
rs 9.6
wmc 35

16 Methods

Rating   Name   Duplication   Size   Complexity  
A getURL() 0 3 1
A getHeaders() 0 3 1
A removeDataParam() 0 4 2
A addHeader() 0 4 1
A addDataParam() 0 4 1
A setMethod() 0 10 2
A removeHeader() 0 4 2
A setURL() 0 4 1
A __construct() 0 5 3
A getDataParams() 0 3 1
A getMethod() 0 3 1
A isFileUpload() 0 3 1
A setIsFileUpload() 0 5 1
A cacheRequests() 0 4 1
A curl_json() 0 4 1
C curl() 0 57 15
1
<?php
2
3
namespace Lifeboat\Utils;
4
5
use Lifeboat\CurlResponse;
6
use Lifeboat\Exceptions\InvalidArgumentException;
7
use LogicException;
8
9
/**
10
 * Class Curl
11
 * @package Lifeboat
12
 */
13
class Curl {
14
15
    const ALLOWED_METHODS   = ['GET', 'POST', 'DELETE', 'PUT'];
16
    const USER_AGENT        = 'LifeboatSDK/curl-service';
17
18
    private static $_cache = [];
19
20
    private $_method = 'GET';
21
    private $_url    = '';
22
    private $_data    = [];
23
    private $_isfile   = false;
24
    private $_headers = [
25
        'Content-Type'      => 'application/x-www-form-urlencoded',
26
        'X-Requested-By'    => self::USER_AGENT
27
    ];
28
    private $_enable_cache = false;
29
30
    /**
31
     * Curl constructor.
32
     *
33
     * @param string $url
34
     * @param array $data
35
     * @param array $headers
36
     *
37
     * @throws LogicException
38
     */
39
    public function __construct(string $url, array $data = [], array $headers = [])
40
    {
41
        $this->setURL($url);
42
        foreach ($data as $name => $value)      $this->addDataParam($name, $value);
43
        foreach ($headers as $name => $value)   $this->addHeader($name, $value);
44
    }
45
46
    /**
47
     * @param string $url
48
     * @return Curl
49
     */
50
    public function setURL(string $url): Curl
51
    {
52
        $this->_url = $url;
53
        return $this;
54
    }
55
56
    /**
57
     * @return string
58
     */
59
    public function getURL(): string
60
    {
61
        return $this->_url;
62
    }
63
64
    /**
65
     * @param string $method
66
     * @return $this
67
     * @throws InvalidArgumentException If $method specified is invalid
68
     */
69
    public function setMethod(string $method = 'GET'): Curl
70
    {
71
        $method = strtoupper($method);
72
73
        if (!in_array($method, self::ALLOWED_METHODS)) {
74
            throw new InvalidArgumentException("HTTP Method '{$method}' is not allowed");
75
        }
76
77
        $this->_method = $method;
78
        return $this;
79
    }
80
81
    /**
82
     * @return string
83
     */
84
    public function getMethod(): string
85
    {
86
        return $this->_method;
87
    }
88
89
    /**
90
     * @param string $name
91
     * @param mixed $value
92
     * @return $this
93
     */
94
    public function addDataParam(string $name, $value): Curl
95
    {
96
        $this->_data[$name] = $value;
97
        return $this;
98
    }
99
100
    public function removeDataParam(string $name): Curl
101
    {
102
        if (array_key_exists($name, $this->_data)) unset($this->_data[$name]);
103
        return $this;
104
    }
105
106
    /**
107
     * @return array
108
     */
109
    public function getDataParams(): array
110
    {
111
        return $this->_data;
112
    }
113
114
    /**
115
     * @param string $name
116
     * @param string $value
117
     * @return Curl
118
     */
119
    public function addHeader(string $name, string $value): Curl
120
    {
121
        $this->_headers[$name] = $value;
122
        return $this;
123
    }
124
125
    /**
126
     * @param string $name
127
     * @return Curl
128
     */
129
    public function removeHeader(string $name): Curl
130
    {
131
        if (array_key_exists($name, $this->_headers)) unset($this->_headers[$name]);
132
        return $this;
133
    }
134
135
    /**
136
     * @return array
137
     */
138
    public function getHeaders(): array
139
    {
140
        return $this->_headers;
141
    }
142
143
    public function setIsFileUpload(bool $is_file): Curl
144
    {
145
        $this->addHeader('Content-Type', 'multipart/form-data');
146
        $this->_isfile = $is_file;
147
        return $this;
148
    }
149
150
    public function isFileUpload(): bool
151
    {
152
        return $this->_isfile;
153
    }
154
155
    /**
156
     * @param bool $switch
157
     * @return $this
158
     */
159
    public function cacheRequests(bool $switch): Curl
160
    {
161
        $this->_enable_cache = $switch;
162
        return $this;
163
    }
164
165
    /**
166
     * @return CurlResponse
167
     */
168
    public function curl(): CurlResponse
169
    {
170
        $post_data      = null;
171
        $send_headers   = [];
172
        $request_uri    = $this->getURL();
173
174
        //  Headers
175
        foreach ($this->getHeaders() as $k => $v) $send_headers[] = "{$k}: {$v}";
176
177
        // Request Data
178
        switch ($this->getMethod()) {
179
            case 'GET':
180
            case 'DELETE':
181
                foreach ($this->getDataParams() as $name => $value) $request_uri = URL::setGetVar($name, $value, $request_uri);
182
                break;
183
184
            case 'POST':
185
                $post_data = ($this->isFileUpload()) ? $this->getDataParams() : http_build_query($this->getDataParams());
186
                break;
187
            case 'PUT':
188
                $post_data = http_build_query($this->getDataParams());
189
                break;
190
        }
191
192
        if ($this->_enable_cache && $this->getMethod() === 'GET') {
193
            $cache_key = urlencode($request_uri) . implode(',', $send_headers);
194
            if (array_key_exists($cache_key, self::$_cache)) {
195
                return self::$_cache[$cache_key];
196
            }
197
        }
198
199
        $ch = curl_init();
200
        curl_setopt($ch, CURLOPT_URL, $request_uri);
201
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
202
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->getMethod());
203
        curl_setopt($ch, CURLOPT_USERAGENT, self::USER_AGENT);
204
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
205
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
206
        curl_setopt($ch, CURLOPT_ENCODING, '');
207
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
208
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
209
        curl_setopt($ch, CURLINFO_HEADER_OUT, true);
210
        curl_setopt($ch, CURLOPT_AUTOREFERER, true);
211
212
        if (!empty($send_headers))  curl_setopt($ch, CURLOPT_HTTPHEADER, $send_headers);
213
        if (!is_null($post_data))   {
214
            curl_setopt($ch, CURLOPT_POST, 1);
215
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
216
        }
217
218
        $result     = curl_exec($ch);
219
        $http_code  = curl_getinfo($ch, CURLINFO_HTTP_CODE);
220
221
        $response = new CurlResponse((int) $http_code, (string) $result);
222
        if ($this->_enable_cache && isset($cache_key)) self::$_cache[$cache_key] = $response;
223
224
        return $response;
225
    }
226
227
    /**
228
     * @see Curl::curl()
229
     *
230
     * @return CurlResponse
231
     */
232
    public function curl_json(): CurlResponse
233
    {
234
        $this->addHeader('Accept', 'application/json');
235
        return $this->curl();
236
    }
237
}
238