Passed
Pull Request — master (#2)
by Solo
02:56
created

CurlInitializer::postOut()   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
ccs 14
cts 14
cp 1
rs 9.7333
c 0
b 0
f 0
cc 1
nc 1
nop 2
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 16
    public function post($endpoint, array $params)
53 16
    {
54 16
        return $this->custom('POST', $endpoint, $params);
55
    }
56
57
    public function delete($endpoint, array $params)
58
    {
59
        return $this->custom('DELETE', $endpoint, $params);
60
    }
61
62
    public function put($endpoint, array $params)
63
    {
64
        return $this->custom('PUT', $endpoint, $params);
65
    }
66
67 12
    public function postMultipart($endpoint, array $params)
68 12
    {
69 12
        $ch = curl_init();
70 12
        list($url, $extra) = UrlNormalizer::twitterSplitUrlAndParameters($endpoint);
71 12
        $params += $extra;
72 12
        $params = RequestParamValidator::validateMultipartParams($params);
73 12
        curl_setopt_array($ch, array_replace($this->options, [
74 12
            CURLOPT_URL            => $url,
75 12
            CURLOPT_HTTPHEADER     => $this->credential->getOAuthHeaders($url, 'POST', []),
76 12
            CURLOPT_RETURNTRANSFER => true,
77 12
            CURLOPT_HEADER         => true,
78 12
            CURLOPT_POST           => true,
79 12
            CURLOPT_POSTFIELDS     => $params,
80
        ]));
81 12
        return $ch;
82
    }
83
84 2
    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
        ]));
97 2
        return $ch;
98
    }
99
100 2
    public function postOut($endpoint, array $params)
101 2
    {
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
        ]));
114 2
        return $ch;
115
    }
116
117 2
    public function postMultipartOut($endpoint, array $params)
118 2
    {
119 2
        $ch = curl_init();
120 2
        list($url, $extra) = UrlNormalizer::outSplitUrlAndParameters($endpoint);
121 2
        $params += $extra;
122 2
        $params = RequestParamValidator::validateMultipartParams($params);
123 2
        curl_setopt_array($ch, array_replace($this->options, [
124 2
            CURLOPT_URL            => $url,
125 2
            CURLOPT_HTTPHEADER     => $this->credential->getOAuthHeadersForOAuthEcho(),
126 2
            CURLOPT_RETURNTRANSFER => true,
127 2
            CURLOPT_HEADER         => true,
128 2
            CURLOPT_POST           => true,
129 2
            CURLOPT_POSTFIELDS     => $params,
130
        ]));
131 2
        return $ch;
132
    }
133
134 15
    public function streaming($endpoint, array $params, StreamHandler $handler)
135 15
    {
136 15
        $ch = curl_init();
137 15
        list($url, $extra) = UrlNormalizer::twitterSplitUrlAndParameters($endpoint);
138 15
        $params += $extra;
139 15
        $params = RequestParamValidator::validateParams($params);
140 15
        curl_setopt_array($ch, array_replace($this->options, [
141 15
            CURLOPT_URL            => $url,
142 15
            CURLOPT_HTTPHEADER     => $this->credential->getOAuthHeaders($url, 'POST', $params),
143 15
            CURLOPT_POST           => true,
144 15
            CURLOPT_POSTFIELDS     => http_build_query($params, '', '&'),
145 15
            CURLOPT_HEADERFUNCTION => [$handler, 'headerFunction'],
146 15
            CURLOPT_WRITEFUNCTION  => [$handler, 'writeFunction'],
147
        ]));
148 15
        return $ch;
149
    }
150
151 6
    public function oauthForRequestToken($oauth_callback = null)
152 6
    {
153 6
        if ($oauth_callback !== null) {
154 6
            $oauth_callback = RequestParamValidator::validateStringable('oauth_callback', $oauth_callback);
155
        }
156 6
        $ch = curl_init();
157 6
        curl_setopt_array($ch, array_replace($this->options, [
158 6
            CURLOPT_URL            => 'https://api.twitter.com/oauth/request_token',
159 6
            CURLOPT_HTTPHEADER     => $this->credential->getOAuthHeadersForRequestToken($oauth_callback),
160 6
            CURLOPT_RETURNTRANSFER => true,
161 6
            CURLOPT_HEADER         => true,
162 6
            CURLOPT_POST           => true,
163 6
            CURLOPT_POSTFIELDS     => '',
164
        ]));
165 6
        return $ch;
166
    }
167
168 4
    public function oauthForAccessToken($oauth_verifier)
169 4
    {
170 4
        $oauth_verifier = RequestParamValidator::validateStringable('oauth_verifier', $oauth_verifier);
171 4
        $ch = curl_init();
172 4
        curl_setopt_array($ch, array_replace($this->options, [
173 4
            CURLOPT_URL            => 'https://api.twitter.com/oauth/access_token',
174 4
            CURLOPT_HTTPHEADER     => $this->credential->getOAuthHeadersForAccessToken($oauth_verifier),
175 4
            CURLOPT_RETURNTRANSFER => true,
176 4
            CURLOPT_HEADER         => true,
177 4
            CURLOPT_POST           => true,
178 4
            CURLOPT_POSTFIELDS     => '',
179
        ]));
180 4
        return $ch;
181
    }
182
183 2
    public function xauthForAccessToken($username, $password)
184 2
    {
185 2
        $username = RequestParamValidator::validateStringable('username', $username);
186 2
        $password = RequestParamValidator::validateStringable('password', $password);
187 2
        $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
        ]));
200 2
        return $ch;
201
    }
202
203 2
    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
        ]));
216 2
        return $ch;
217
    }
218
219 2
    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
        ]));
232 2
        return $ch;
233
    }
234
235
    protected function custom2($endpoint, $params = [])
236
    {
237
        $ch = curl_init();
238
        list($url, $extra) = UrlNormalizer::twitterSplitUrlAndParameters($endpoint);
239
        $params += $extra;
240
        $params = RequestParamValidator::validateParams($params);
241
        curl_setopt_array($ch, array_replace($this->options, [
242
            CURLOPT_URL            => $url,
243
            CURLOPT_HTTPHEADER     => $this->credential->getBearerHeaders(),
244
            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 2
    public function invalidateBearerToken()
268 2
    {
269 2
        $ch = curl_init();
270 2
        curl_setopt_array($ch, array_replace($this->options, [
271 2
            CURLOPT_URL            => 'https://api.twitter.com/oauth2/invalidate_token',
272 2
            CURLOPT_HTTPHEADER     => $this->credential->getBasicHeaders(),
273 2
            CURLOPT_RETURNTRANSFER => true,
274 2
            CURLOPT_HEADER         => true,
275 2
            CURLOPT_POST           => true,
276 2
            CURLOPT_POSTFIELDS     => http_build_query([
277 2
                'access_token' => rawurldecode($this->credential->token),
0 ignored issues
show
Documentation introduced by
The property $token is declared protected in mpyw\Cowitter\Components\Credential. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
278 2
            ], '', '&', PHP_QUERY_RFC3986),
279
        ]));
280 2
        return $ch;
281
    }
282
283 8
    public function browsing()
284 8
    {
285 8
        $ch = curl_init();
286 8
        curl_setopt_array($ch, array_replace($this->options, [
287 8
            CURLOPT_COOKIEFILE     => '',
288 8
            CURLOPT_RETURNTRANSFER => true,
289 8
            CURLOPT_HEADER         => true,
290
        ]));
291 8
        return $ch;
292
    }
293
}
294