Test Setup Failed
Pull Request — master (#16)
by
unknown
14:30
created

CurlInitializer::custom()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 16
c 0
b 0
f 0
ccs 14
cts 14
cp 1
rs 9.7333
cc 1
nc 1
nop 3
crap 1
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, [
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)
36 16
    {
37 16
        $ch = curl_init();
38 16
        list($url, $extra) = UrlNormalizer::twitterSplitUrlAndParameters($endpoint);
39 16
        $params += $extra;
40 16
        $params = RequestParamValidator::validateParams($params);
41 16
        curl_setopt_array($ch, array_replace($this->options, [
42 16
            CURLOPT_URL            => $url,
43 16
            CURLOPT_HTTPHEADER     => $this->credential->getOAuthHeaders($url, $method, $params),
44 16
            CURLOPT_RETURNTRANSFER => true,
45 16
            CURLOPT_HEADER         => true,
46 16
            CURLOPT_CUSTOMREQUEST  => $method,
47 16
            CURLOPT_POSTFIELDS     => http_build_query($params, '', '&'),
48
        ]));
49 16
        return $ch;
50
    }
51
52 12
    public function post($endpoint, array $params)
53 12
    {
54 12
        return $this->custom('POST', $endpoint, $params);
55 12
    }
56 12
57 12
    public function delete($endpoint, array $params)
58 12
    {
59 12
        return $this->custom('DELETE', $endpoint, $params);
60 12
    }
61 12
62 12
    public function put($endpoint, array $params)
63 12
    {
64 12
        return $this->custom('PUT', $endpoint, $params);
65
    }
66 12
67
    public function postMultipart($endpoint, array $params)
68
    {
69 2
        $ch = curl_init();
70 2
        list($url, $extra) = UrlNormalizer::twitterSplitUrlAndParameters($endpoint);
71 2
        $params += $extra;
72 2
        $params = RequestParamValidator::validateMultipartParams($params);
73 2
        curl_setopt_array($ch, array_replace($this->options, [
74 2
            CURLOPT_URL            => $url,
75 2
            CURLOPT_HTTPHEADER     => $this->credential->getOAuthHeaders($url, 'POST', []),
76 2
            CURLOPT_RETURNTRANSFER => true,
77 2
            CURLOPT_HEADER         => true,
78 2
            CURLOPT_POST           => true,
79 2
            CURLOPT_POSTFIELDS     => $params,
80 2
        ]));
81
        return $ch;
82 2
    }
83
84
    public function getOut($endpoint, array $params)
85 2
    {
86 2
        $ch = curl_init();
87 2
        list($url, $extra) = UrlNormalizer::outSplitUrlAndParameters($endpoint);
88 2
        $params += $extra;
89 2
        $params = RequestParamValidator::validateParams($params);
90 2
        curl_setopt_array($ch, array_replace($this->options, [
91 2
            CURLOPT_URL            => !$params ? $url : $url . '?' . http_build_query($params, '', '&'),
92 2
            CURLOPT_HTTPHEADER     => $this->credential->getOAuthHeadersForOAuthEcho(),
93 2
            CURLOPT_RETURNTRANSFER => true,
94 2
            CURLOPT_HEADER         => true,
95 2
            CURLOPT_HTTPGET        => true,
96 2
        ]));
97 2
        return $ch;
98
    }
99 2
100
    public function postOut($endpoint, array $params)
101
    {
102 2
        $ch = curl_init();
103 2
        list($url, $extra) = UrlNormalizer::outSplitUrlAndParameters($endpoint);
104 2
        $params += $extra;
105 2
        $params = RequestParamValidator::validateParams($params);
106 2
        curl_setopt_array($ch, array_replace($this->options, [
107 2
            CURLOPT_URL            => $url,
108 2
            CURLOPT_HTTPHEADER     => $this->credential->getOAuthHeadersForOAuthEcho(),
109 2
            CURLOPT_RETURNTRANSFER => true,
110 2
            CURLOPT_HEADER         => true,
111 2
            CURLOPT_POST           => true,
112 2
            CURLOPT_POSTFIELDS     => http_build_query($params, '', '&'),
113 2
        ]));
114 2
        return $ch;
115
    }
116 2
117
    public function postMultipartOut($endpoint, array $params)
118
    {
119 15
        $ch = curl_init();
120 15
        list($url, $extra) = UrlNormalizer::outSplitUrlAndParameters($endpoint);
121 15
        $params += $extra;
122 15
        $params = RequestParamValidator::validateMultipartParams($params);
123 15
        curl_setopt_array($ch, array_replace($this->options, [
124 15
            CURLOPT_URL            => $url,
125 15
            CURLOPT_HTTPHEADER     => $this->credential->getOAuthHeadersForOAuthEcho(),
126 15
            CURLOPT_RETURNTRANSFER => true,
127 15
            CURLOPT_HEADER         => true,
128 15
            CURLOPT_POST           => true,
129 15
            CURLOPT_POSTFIELDS     => $params,
130 15
        ]));
131 15
        return $ch;
132
    }
133 15
134
    public function streaming($endpoint, array $params, StreamHandler $handler)
135
    {
136 6
        $ch = curl_init();
137 6
        list($url, $extra) = UrlNormalizer::twitterSplitUrlAndParameters($endpoint);
138 6
        $params += $extra;
139 6
        $params = RequestParamValidator::validateParams($params);
140
        curl_setopt_array($ch, array_replace($this->options, [
141 6
            CURLOPT_URL            => $url,
142 6
            CURLOPT_HTTPHEADER     => $this->credential->getOAuthHeaders($url, 'POST', $params),
143 6
            CURLOPT_POST           => true,
144 6
            CURLOPT_POSTFIELDS     => http_build_query($params, '', '&'),
145 6
            CURLOPT_HEADERFUNCTION => [$handler, 'headerFunction'],
146 6
            CURLOPT_WRITEFUNCTION  => [$handler, 'writeFunction'],
147 6
        ]));
148 6
        return $ch;
149
    }
150 6
151
    public function oauthForRequestToken($oauth_callback = null)
152
    {
153 4
        if ($oauth_callback !== null) {
154 4
            $oauth_callback = RequestParamValidator::validateStringable('oauth_callback', $oauth_callback);
155 4
        }
156 4
        $ch = curl_init();
157 4
        curl_setopt_array($ch, array_replace($this->options, [
158 4
            CURLOPT_URL            => 'https://api.twitter.com/oauth/request_token',
159 4
            CURLOPT_HTTPHEADER     => $this->credential->getOAuthHeadersForRequestToken($oauth_callback),
160 4
            CURLOPT_RETURNTRANSFER => true,
161 4
            CURLOPT_HEADER         => true,
162 4
            CURLOPT_POST           => true,
163 4
            CURLOPT_POSTFIELDS     => '',
164
        ]));
165 4
        return $ch;
166
    }
167
168 2
    public function oauthForAccessToken($oauth_verifier)
169 2
    {
170 2
        $oauth_verifier = RequestParamValidator::validateStringable('oauth_verifier', $oauth_verifier);
171 2
        $ch = curl_init();
172 2
        curl_setopt_array($ch, array_replace($this->options, [
173 2
            CURLOPT_URL            => 'https://api.twitter.com/oauth/access_token',
174 2
            CURLOPT_HTTPHEADER     => $this->credential->getOAuthHeadersForAccessToken($oauth_verifier),
175 2
            CURLOPT_RETURNTRANSFER => true,
176 2
            CURLOPT_HEADER         => true,
177 2
            CURLOPT_POST           => true,
178 2
            CURLOPT_POSTFIELDS     => '',
179 2
        ]));
180 2
        return $ch;
181 2
    }
182 2
183 2
    public function xauthForAccessToken($username, $password)
184
    {
185 2
        $username = RequestParamValidator::validateStringable('username', $username);
186
        $password = RequestParamValidator::validateStringable('password', $password);
187
        $ch = curl_init();
188 2
        curl_setopt_array($ch, array_replace($this->options, [
189 2
            CURLOPT_URL            => 'https://api.twitter.com/oauth/access_token',
190 2
            CURLOPT_HTTPHEADER     => $this->credential->getXAuthHeadersForAccessToken($username, $password),
191 2
            CURLOPT_RETURNTRANSFER => true,
192 2
            CURLOPT_HEADER         => true,
193 2
            CURLOPT_POST           => true,
194 2
            CURLOPT_POSTFIELDS     => http_build_query([
195 2
                'x_auth_username' => $username,
196 2
                'x_auth_password' => $password,
197 2
                'x_auth_mode'     => 'client_auth',
198 2
            ], '', '&'),
199 2
        ]));
200
        return $ch;
201 2
    }
202
203
    public function oauthForBearerToken()
204 2
    {
205 2
        $ch = curl_init();
206 2
        curl_setopt_array($ch, array_replace($this->options, [
207 2
            CURLOPT_URL            => 'https://api.twitter.com/oauth2/token',
208 2
            CURLOPT_HTTPHEADER     => $this->credential->getBasicHeaders(),
209 2
            CURLOPT_RETURNTRANSFER => true,
210 2
            CURLOPT_HEADER         => true,
211 2
            CURLOPT_POST           => true,
212 2
            CURLOPT_POSTFIELDS     => http_build_query([
213 2
                'grant_type' => 'client_credentials',
214 2
            ], '', '&'),
215 2
        ]));
216
        return $ch;
217 2
    }
218
219
    public function get2($endpoint, $params = [])
220 2
    {
221 2
        $ch = curl_init();
222 2
        list($url, $extra) = UrlNormalizer::twitterSplitUrlAndParameters($endpoint);
223 2
        $params += $extra;
224 2
        $params = RequestParamValidator::validateParams($params);
225 2
        curl_setopt_array($ch, array_replace($this->options, [
226 2
            CURLOPT_URL            => !$params ? $url : $url . '?' . http_build_query($params, '', '&'),
227 2
            CURLOPT_HTTPHEADER     => $this->credential->getBearerHeaders(),
228 2
            CURLOPT_RETURNTRANSFER => true,
229 2
            CURLOPT_HEADER         => true,
230 2
            CURLOPT_HTTPGET        => true,
231 2
        ]));
232
        return $ch;
233 2
    }
234
235
    protected function custom2($endpoint, $params = [])
236 8
    {
237 8
        $ch = curl_init();
238 8
        list($url, $extra) = UrlNormalizer::twitterSplitUrlAndParameters($endpoint);
239 8
        $params += $extra;
240 8
        $params = RequestParamValidator::validateParams($params);
241 8
        curl_setopt_array($ch, array_replace($this->options, [
242 8
            CURLOPT_URL            => $url,
243
            CURLOPT_HTTPHEADER     => $this->credential->getBearerHeaders(),
244 8
            CURLOPT_RETURNTRANSFER => true,
245
            CURLOPT_HEADER         => true,
246
            CURLOPT_CUSTOMREQUEST  => $method,
0 ignored issues
show
Bug introduced by
The variable $method does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
247
            CURLOPT_POSTFIELDS     => http_build_query($params, '', '&'),
248
        ]));
249
        return $ch;
250
    }
251
252
    public function post2($endpoint, array $params)
253
    {
254
        return $this->custom2('POST', $endpoint, $params);
0 ignored issues
show
Unused Code introduced by
The call to CurlInitializer::custom2() has too many arguments starting with $params.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
255
    }
256
257
    public function delete2($endpoint, array $params)
258
    {
259
        return $this->custom2('DELETE', $endpoint, $params);
0 ignored issues
show
Unused Code introduced by
The call to CurlInitializer::custom2() has too many arguments starting with $params.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
260
    }
261
262
    public function put2($endpoint, array $params)
263
    {
264
        return $this->custom2('PUT', $endpoint, $params);
0 ignored issues
show
Unused Code introduced by
The call to CurlInitializer::custom2() has too many arguments starting with $params.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
265
    }
266
267
    public function invalidateBearerToken()
268
    {
269
        $ch = curl_init();
270
        curl_setopt_array($ch, array_replace($this->options, [
271
            CURLOPT_URL            => 'https://api.twitter.com/oauth2/invalidate_token',
272
            CURLOPT_HTTPHEADER     => $this->credential->getBasicHeaders(),
273
            CURLOPT_RETURNTRANSFER => true,
274
            CURLOPT_HEADER         => true,
275
            CURLOPT_POST           => true,
276
            CURLOPT_POSTFIELDS     => http_build_query([
277
                'access_token' => rawurldecode($this->credential->token),
278
            ], '', '&', PHP_QUERY_RFC3986),
279
        ]));
280
        return $ch;
281
    }
282
283
    public function browsing()
284
    {
285
        $ch = curl_init();
286
        curl_setopt_array($ch, array_replace($this->options, [
287
            CURLOPT_COOKIEFILE     => '',
288
            CURLOPT_RETURNTRANSFER => true,
289
            CURLOPT_HEADER         => true,
290
        ]));
291
        return $ch;
292
    }
293
}
294