CurlInitializer   A
last analyzed

Complexity

Total Complexity 28

Size/Duplication

Total Lines 298
Duplicated Lines 0 %

Test Coverage

Coverage 84.85%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 188
c 5
b 0
f 0
dl 0
loc 298
ccs 196
cts 231
cp 0.8485
rs 10
wmc 28

23 Methods

Rating   Name   Duplication   Size   Complexity  
A put() 0 3 1
A delete() 0 3 1
A oauthForBearerToken() 0 14 1
A invalidateBearerToken() 0 14 1
A postJson() 0 3 1
A post2() 0 3 1
A streaming() 0 15 1
A delete2() 0 3 1
A browsing() 0 9 1
A getOut() 0 14 2
A postOut() 0 15 1
A oauthForAccessToken() 0 13 1
A xauthForAccessToken() 0 18 1
A postMultipartOut() 0 15 1
A oauthForRequestToken() 0 15 2
A put2() 0 3 1
A postMultipart() 0 15 1
A custom2() 0 15 1
A post() 0 3 1
A get2() 0 14 2
A get() 0 14 2
A __construct() 0 4 1
A custom() 0 24 2
1
<?php
2
3
namespace mpyw\Cowitter\Components;
4
5
use mpyw\Cowitter\Helpers\RequestParamValidator;
6
use mpyw\Cowitter\Helpers\UrlNormalizer;
7
8
class CurlInitializer
9
{
10
    protected $credential;
11
    protected $options;
12
13 68
    public function __construct(Credential $credential, array $options)
14 68
    {
15 68
        $this->credential = $credential;
16 68
        $this->options = $options;
17 68
    }
18
19 12
    public function get($endpoint, array $params)
20 12
    {
21 12
        $ch = curl_init();
22 12
        list($url, $extra) = UrlNormalizer::twitterSplitUrlAndParameters($endpoint);
23 12
        $params += $extra;
24 12
        $params = RequestParamValidator::validateParams($params);
25 12
        curl_setopt_array($ch, array_replace($this->options, [
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_setopt_array() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

25
        curl_setopt_array(/** @scrutinizer ignore-type */ $ch, array_replace($this->options, [
Loading history...
26 12
            CURLOPT_URL            => !$params ? $url : $url . '?' . http_build_query($params, '', '&'),
27 12
            CURLOPT_HTTPHEADER     => $this->credential->getOAuthHeaders($url, 'GET', $params),
28 12
            CURLOPT_RETURNTRANSFER => true,
29 12
            CURLOPT_HEADER         => true,
30 12
            CURLOPT_HTTPGET        => true,
31
        ]));
32 12
        return $ch;
33
    }
34
35 16
    protected function custom($method, $endpoint, array $params, $json = false)
36 16
    {
37 16
        $ch = curl_init();
38 16
        list($url, $extra) = UrlNormalizer::twitterSplitUrlAndParameters($endpoint);
39 16
        $params += $extra;
40 16
        if(!$json){
41 16
            $params = RequestParamValidator::validateParams($params);
42 16
            $header = $this->credential->getOAuthHeaders($url, $method, $params);
43 16
            $postfields = http_build_query($params, '', '&');
44
        } else {
45
            $params = RequestParamValidator::validateJsonParams($params);
46
            $header = array_merge(['Content-Type: application/json'], $this->credential->getOAuthHeaders($url, $method, []));
47
            $postfields = json_encode($params);
48
        }
49
50 16
        curl_setopt_array($ch, array_replace($this->options, [
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_setopt_array() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

50
        curl_setopt_array(/** @scrutinizer ignore-type */ $ch, array_replace($this->options, [
Loading history...
51 16
            CURLOPT_URL            => $url,
52 16
            CURLOPT_HTTPHEADER     => $header,
53 16
            CURLOPT_RETURNTRANSFER => true,
54 16
            CURLOPT_HEADER         => true,
55 16
            CURLOPT_CUSTOMREQUEST  => $method,
56 16
            CURLOPT_POSTFIELDS     => $postfields,
57
        ]));
58 16
        return $ch;
59
    }
60
61 16
    public function post($endpoint, array $params)
62 16
    {
63 16
        return $this->custom('POST', $endpoint, $params);
64
    }
65
66
    public function postJson($endpoint, array $params)
67
    {
68
        return $this->custom('POST', $endpoint, $params, true);
69
    }
70
71
    public function delete($endpoint, array $params)
72
    {
73
        return $this->custom('DELETE', $endpoint, $params);
74
    }
75
76
    public function put($endpoint, array $params)
77
    {
78
        return $this->custom('PUT', $endpoint, $params);
79
    }
80
81 12
    public function postMultipart($endpoint, array $params)
82 12
    {
83 12
        $ch = curl_init();
84 12
        list($url, $extra) = UrlNormalizer::twitterSplitUrlAndParameters($endpoint);
85 12
        $params += $extra;
86 12
        $params = RequestParamValidator::validateMultipartParams($params);
87 12
        curl_setopt_array($ch, array_replace($this->options, [
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_setopt_array() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

87
        curl_setopt_array(/** @scrutinizer ignore-type */ $ch, array_replace($this->options, [
Loading history...
88 12
            CURLOPT_URL            => $url,
89 12
            CURLOPT_HTTPHEADER     => $this->credential->getOAuthHeaders($url, 'POST', []),
90 12
            CURLOPT_RETURNTRANSFER => true,
91 12
            CURLOPT_HEADER         => true,
92 12
            CURLOPT_POST           => true,
93 12
            CURLOPT_POSTFIELDS     => $params,
94
        ]));
95 12
        return $ch;
96
    }
97
98 2
    public function getOut($endpoint, array $params)
99 2
    {
100 2
        $ch = curl_init();
101 2
        list($url, $extra) = UrlNormalizer::outSplitUrlAndParameters($endpoint);
102 2
        $params += $extra;
103 2
        $params = RequestParamValidator::validateParams($params);
104 2
        curl_setopt_array($ch, array_replace($this->options, [
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_setopt_array() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

104
        curl_setopt_array(/** @scrutinizer ignore-type */ $ch, array_replace($this->options, [
Loading history...
105 2
            CURLOPT_URL            => !$params ? $url : $url . '?' . http_build_query($params, '', '&'),
106 2
            CURLOPT_HTTPHEADER     => $this->credential->getOAuthHeadersForOAuthEcho(),
107 2
            CURLOPT_RETURNTRANSFER => true,
108 2
            CURLOPT_HEADER         => true,
109 2
            CURLOPT_HTTPGET        => true,
110
        ]));
111 2
        return $ch;
112
    }
113
114 2
    public function postOut($endpoint, array $params)
115 2
    {
116 2
        $ch = curl_init();
117 2
        list($url, $extra) = UrlNormalizer::outSplitUrlAndParameters($endpoint);
118 2
        $params += $extra;
119 2
        $params = RequestParamValidator::validateParams($params);
120 2
        curl_setopt_array($ch, array_replace($this->options, [
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_setopt_array() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

120
        curl_setopt_array(/** @scrutinizer ignore-type */ $ch, array_replace($this->options, [
Loading history...
121 2
            CURLOPT_URL            => $url,
122 2
            CURLOPT_HTTPHEADER     => $this->credential->getOAuthHeadersForOAuthEcho(),
123 2
            CURLOPT_RETURNTRANSFER => true,
124 2
            CURLOPT_HEADER         => true,
125 2
            CURLOPT_POST           => true,
126 2
            CURLOPT_POSTFIELDS     => http_build_query($params, '', '&'),
127
        ]));
128 2
        return $ch;
129
    }
130
131 2
    public function postMultipartOut($endpoint, array $params)
132 2
    {
133 2
        $ch = curl_init();
134 2
        list($url, $extra) = UrlNormalizer::outSplitUrlAndParameters($endpoint);
135 2
        $params += $extra;
136 2
        $params = RequestParamValidator::validateMultipartParams($params);
137 2
        curl_setopt_array($ch, array_replace($this->options, [
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_setopt_array() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

137
        curl_setopt_array(/** @scrutinizer ignore-type */ $ch, array_replace($this->options, [
Loading history...
138 2
            CURLOPT_URL            => $url,
139 2
            CURLOPT_HTTPHEADER     => $this->credential->getOAuthHeadersForOAuthEcho(),
140 2
            CURLOPT_RETURNTRANSFER => true,
141 2
            CURLOPT_HEADER         => true,
142 2
            CURLOPT_POST           => true,
143 2
            CURLOPT_POSTFIELDS     => $params,
144
        ]));
145 2
        return $ch;
146
    }
147
148 15
    public function streaming($endpoint, array $params, StreamHandler $handler)
149 15
    {
150 15
        $ch = curl_init();
151 15
        list($url, $extra) = UrlNormalizer::twitterSplitUrlAndParameters($endpoint);
152 15
        $params += $extra;
153 15
        $params = RequestParamValidator::validateParams($params);
154 15
        curl_setopt_array($ch, array_replace($this->options, [
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_setopt_array() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

154
        curl_setopt_array(/** @scrutinizer ignore-type */ $ch, array_replace($this->options, [
Loading history...
155 15
            CURLOPT_URL            => $url,
156 15
            CURLOPT_HTTPHEADER     => $this->credential->getOAuthHeaders($url, 'POST', $params),
157 15
            CURLOPT_POST           => true,
158 15
            CURLOPT_POSTFIELDS     => http_build_query($params, '', '&'),
159 15
            CURLOPT_HEADERFUNCTION => [$handler, 'headerFunction'],
160 15
            CURLOPT_WRITEFUNCTION  => [$handler, 'writeFunction'],
161
        ]));
162 15
        return $ch;
163
    }
164
165 6
    public function oauthForRequestToken($oauth_callback = null)
166 6
    {
167 6
        if ($oauth_callback !== null) {
168 6
            $oauth_callback = RequestParamValidator::validateStringable('oauth_callback', $oauth_callback);
169
        }
170 6
        $ch = curl_init();
171 6
        curl_setopt_array($ch, array_replace($this->options, [
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_setopt_array() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

171
        curl_setopt_array(/** @scrutinizer ignore-type */ $ch, array_replace($this->options, [
Loading history...
172 6
            CURLOPT_URL            => 'https://api.twitter.com/oauth/request_token',
173 6
            CURLOPT_HTTPHEADER     => $this->credential->getOAuthHeadersForRequestToken($oauth_callback),
174 6
            CURLOPT_RETURNTRANSFER => true,
175 6
            CURLOPT_HEADER         => true,
176 6
            CURLOPT_POST           => true,
177 6
            CURLOPT_POSTFIELDS     => '',
178
        ]));
179 6
        return $ch;
180
    }
181
182 4
    public function oauthForAccessToken($oauth_verifier)
183 4
    {
184 4
        $oauth_verifier = RequestParamValidator::validateStringable('oauth_verifier', $oauth_verifier);
185 4
        $ch = curl_init();
186 4
        curl_setopt_array($ch, array_replace($this->options, [
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_setopt_array() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

186
        curl_setopt_array(/** @scrutinizer ignore-type */ $ch, array_replace($this->options, [
Loading history...
187 4
            CURLOPT_URL            => 'https://api.twitter.com/oauth/access_token',
188 4
            CURLOPT_HTTPHEADER     => $this->credential->getOAuthHeadersForAccessToken($oauth_verifier),
189 4
            CURLOPT_RETURNTRANSFER => true,
190 4
            CURLOPT_HEADER         => true,
191 4
            CURLOPT_POST           => true,
192 4
            CURLOPT_POSTFIELDS     => '',
193
        ]));
194 4
        return $ch;
195
    }
196
197 2
    public function xauthForAccessToken($username, $password)
198 2
    {
199 2
        $username = RequestParamValidator::validateStringable('username', $username);
200 2
        $password = RequestParamValidator::validateStringable('password', $password);
201 2
        $ch = curl_init();
202 2
        curl_setopt_array($ch, array_replace($this->options, [
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_setopt_array() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

202
        curl_setopt_array(/** @scrutinizer ignore-type */ $ch, array_replace($this->options, [
Loading history...
203 2
            CURLOPT_URL            => 'https://api.twitter.com/oauth/access_token',
204 2
            CURLOPT_HTTPHEADER     => $this->credential->getXAuthHeadersForAccessToken($username, $password),
205 2
            CURLOPT_RETURNTRANSFER => true,
206 2
            CURLOPT_HEADER         => true,
207 2
            CURLOPT_POST           => true,
208 2
            CURLOPT_POSTFIELDS     => http_build_query([
209 2
                'x_auth_username' => $username,
210 2
                'x_auth_password' => $password,
211 2
                'x_auth_mode'     => 'client_auth',
212 2
            ], '', '&'),
213
        ]));
214 2
        return $ch;
215
    }
216
217 2
    public function oauthForBearerToken()
218 2
    {
219 2
        $ch = curl_init();
220 2
        curl_setopt_array($ch, array_replace($this->options, [
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_setopt_array() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

220
        curl_setopt_array(/** @scrutinizer ignore-type */ $ch, array_replace($this->options, [
Loading history...
221 2
            CURLOPT_URL            => 'https://api.twitter.com/oauth2/token',
222 2
            CURLOPT_HTTPHEADER     => $this->credential->getBasicHeaders(),
223 2
            CURLOPT_RETURNTRANSFER => true,
224 2
            CURLOPT_HEADER         => true,
225 2
            CURLOPT_POST           => true,
226 2
            CURLOPT_POSTFIELDS     => http_build_query([
227 2
                'grant_type' => 'client_credentials',
228 2
            ], '', '&'),
229
        ]));
230 2
        return $ch;
231
    }
232
233 2
    public function get2($endpoint, $params = [])
234 2
    {
235 2
        $ch = curl_init();
236 2
        list($url, $extra) = UrlNormalizer::twitterSplitUrlAndParameters($endpoint);
237 2
        $params += $extra;
238 2
        $params = RequestParamValidator::validateParams($params);
239 2
        curl_setopt_array($ch, array_replace($this->options, [
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_setopt_array() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

239
        curl_setopt_array(/** @scrutinizer ignore-type */ $ch, array_replace($this->options, [
Loading history...
240 2
            CURLOPT_URL            => !$params ? $url : $url . '?' . http_build_query($params, '', '&'),
241 2
            CURLOPT_HTTPHEADER     => $this->credential->getBearerHeaders(),
242 2
            CURLOPT_RETURNTRANSFER => true,
243 2
            CURLOPT_HEADER         => true,
244 2
            CURLOPT_HTTPGET        => true,
245
        ]));
246 2
        return $ch;
247
    }
248
249
    protected function custom2($method, $endpoint, $params = [])
250
    {
251
        $ch = curl_init();
252
        list($url, $extra) = UrlNormalizer::twitterSplitUrlAndParameters($endpoint);
253
        $params += $extra;
254
        $params = RequestParamValidator::validateParams($params);
255
        curl_setopt_array($ch, array_replace($this->options, [
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_setopt_array() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

255
        curl_setopt_array(/** @scrutinizer ignore-type */ $ch, array_replace($this->options, [
Loading history...
256
            CURLOPT_URL            => $url,
257
            CURLOPT_HTTPHEADER     => $this->credential->getBearerHeaders(),
258
            CURLOPT_RETURNTRANSFER => true,
259
            CURLOPT_HEADER         => true,
260
            CURLOPT_CUSTOMREQUEST  => $method,
261
            CURLOPT_POSTFIELDS     => http_build_query($params, '', '&'),
262
        ]));
263
        return $ch;
264
    }
265
266
    public function post2($endpoint, array $params)
267
    {
268
        return $this->custom2('POST', $endpoint, $params);
269
    }
270
271
    public function delete2($endpoint, array $params)
272
    {
273
        return $this->custom2('DELETE', $endpoint, $params);
274
    }
275
276
    public function put2($endpoint, array $params)
277
    {
278
        return $this->custom2('PUT', $endpoint, $params);
279
    }
280
281 2
    public function invalidateBearerToken()
282 2
    {
283 2
        $ch = curl_init();
284 2
        curl_setopt_array($ch, array_replace($this->options, [
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_setopt_array() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

284
        curl_setopt_array(/** @scrutinizer ignore-type */ $ch, array_replace($this->options, [
Loading history...
285 2
            CURLOPT_URL            => 'https://api.twitter.com/oauth2/invalidate_token',
286 2
            CURLOPT_HTTPHEADER     => $this->credential->getBasicHeaders(),
287 2
            CURLOPT_RETURNTRANSFER => true,
288 2
            CURLOPT_HEADER         => true,
289 2
            CURLOPT_POST           => true,
290 2
            CURLOPT_POSTFIELDS     => http_build_query([
291 2
                'access_token' => rawurldecode($this->credential->token),
292 2
            ], '', '&', PHP_QUERY_RFC3986),
293
        ]));
294 2
        return $ch;
295
    }
296
297 8
    public function browsing()
298 8
    {
299 8
        $ch = curl_init();
300 8
        curl_setopt_array($ch, array_replace($this->options, [
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_setopt_array() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

300
        curl_setopt_array(/** @scrutinizer ignore-type */ $ch, array_replace($this->options, [
Loading history...
301 8
            CURLOPT_COOKIEFILE     => '',
302 8
            CURLOPT_RETURNTRANSFER => true,
303 8
            CURLOPT_HEADER         => true,
304
        ]));
305 8
        return $ch;
306
    }
307
}