Passed
Push — main ( 7ac621...ceb98d )
by Dylan
02:22
created

Curl::curl()   F

Complexity

Conditions 17
Paths 300

Size

Total Lines 59
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 41
c 1
b 0
f 0
dl 0
loc 59
rs 3.1333
cc 17
nc 300
nop 1

How to fix   Long Method    Complexity   

Long Method

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:

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
     * @param bool $retry_on_401
167
     * @return CurlResponse
168
     */
169
    public function curl(bool $retry_on_401 = true): CurlResponse
170
    {
171
        $post_data      = null;
172
        $send_headers   = [];
173
        $request_uri    = $this->getURL();
174
175
        //  Headers
176
        foreach ($this->getHeaders() as $k => $v) $send_headers[] = "{$k}: {$v}";
177
178
        // Request Data
179
        switch ($this->getMethod()) {
180
            case 'GET':
181
            case 'DELETE':
182
                foreach ($this->getDataParams() as $name => $value) $request_uri = URL::setGetVar($name, $value, $request_uri);
183
                break;
184
185
            case 'POST':
186
                $post_data = ($this->isFileUpload()) ? $this->getDataParams() : http_build_query($this->getDataParams());
187
                break;
188
            case 'PUT':
189
                $post_data = http_build_query($this->getDataParams());
190
                break;
191
        }
192
193
        if ($this->_enable_cache && $this->getMethod() === 'GET') {
194
            $cache_key = urlencode($request_uri) . implode(',', $send_headers);
195
            if (array_key_exists($cache_key, self::$_cache)) {
196
                return self::$_cache[$cache_key];
197
            }
198
        }
199
200
        $ch = curl_init();
201
        curl_setopt($ch, CURLOPT_URL, $request_uri);
202
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
203
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->getMethod());
204
        curl_setopt($ch, CURLOPT_USERAGENT, self::USER_AGENT);
205
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
206
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
207
        curl_setopt($ch, CURLOPT_ENCODING, '');
208
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
209
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
210
        curl_setopt($ch, CURLINFO_HEADER_OUT, true);
211
        curl_setopt($ch, CURLOPT_AUTOREFERER, true);
212
213
        if (!empty($send_headers))  curl_setopt($ch, CURLOPT_HTTPHEADER, $send_headers);
214
        if (!is_null($post_data))   {
215
            curl_setopt($ch, CURLOPT_POST, 1);
216
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
217
        }
218
219
        $result     = curl_exec($ch);
220
        $http_code  = curl_getinfo($ch, CURLINFO_HTTP_CODE);
221
222
        if ($http_code === 401 && $retry_on_401) return $this->curl(false);
223
224
        $response = new CurlResponse((int) $http_code, (string) $result);
225
        if ($this->_enable_cache && isset($cache_key)) self::$_cache[$cache_key] = $response;
226
227
        return $response;
228
    }
229
230
    /**
231
     * @see Curl::curl()
232
     *
233
     * @return CurlResponse
234
     */
235
    public function curl_json(): CurlResponse
236
    {
237
        $this->addHeader('Accept', 'application/json');
238
        return $this->curl();
239
    }
240
}
241