Test Setup Failed
Pull Request — master (#2)
by Solo
02:21 queued 10s
created

CurlInitializer::put()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 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
    public function __construct(Credential $credential, array $options)
14
    {
15
        $this->credential = $credential;
16
        $this->options = $options;
17
    }
18
19
    public function get($endpoint, array $params)
20
    {
21
        $ch = curl_init();
22
        list($url, $extra) = UrlNormalizer::twitterSplitUrlAndParameters($endpoint);
23
        $params += $extra;
24
        $params = RequestParamValidator::validateParams($params);
25
        curl_setopt_array($ch, array_replace($this->options, [
26
            CURLOPT_URL            => !$params ? $url : $url . '?' . http_build_query($params, '', '&'),
27
            CURLOPT_HTTPHEADER     => $this->credential->getOAuthHeaders($url, 'GET', $params),
28
            CURLOPT_RETURNTRANSFER => true,
29
            CURLOPT_HEADER         => true,
30
            CURLOPT_HTTPGET        => true,
31
        ]));
32
        return $ch;
33
    }
34
35
    protected function custom($method, $endpoint, array $params)
36
    {
37
        $ch = curl_init();
38
        list($url, $extra) = UrlNormalizer::twitterSplitUrlAndParameters($endpoint);
39
        $params += $extra;
40
        $params = RequestParamValidator::validateParams($params);
41
        curl_setopt_array($ch, array_replace($this->options, [
42
            CURLOPT_URL            => $url,
43
            CURLOPT_HTTPHEADER     => $this->credential->getOAuthHeaders($url, $method, $params),
44
            CURLOPT_RETURNTRANSFER => true,
45
            CURLOPT_HEADER         => true,
46
            CURLOPT_CUSTOMREQUEST  => $method,
47
            CURLOPT_POSTFIELDS     => http_build_query($params, '', '&'),
48
        ]));
49
        return $ch;
50
    }
51
52
    public function post($endpoint, array $params)
53
    {
54
        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
    public function postMultipart($endpoint, array $params)
68
    {
69
        $ch = curl_init();
70
        list($url, $extra) = UrlNormalizer::twitterSplitUrlAndParameters($endpoint);
71
        $params += $extra;
72
        $params = RequestParamValidator::validateMultipartParams($params);
73
        curl_setopt_array($ch, array_replace($this->options, [
74
            CURLOPT_URL            => $url,
75
            CURLOPT_HTTPHEADER     => $this->credential->getOAuthHeaders($url, 'POST', []),
76
            CURLOPT_RETURNTRANSFER => true,
77
            CURLOPT_HEADER         => true,
78
            CURLOPT_POST           => true,
79
            CURLOPT_POSTFIELDS     => $params,
80
        ]));
81
        return $ch;
82
    }
83
84
    public function getOut($endpoint, array $params)
85
    {
86
        $ch = curl_init();
87
        list($url, $extra) = UrlNormalizer::outSplitUrlAndParameters($endpoint);
88
        $params += $extra;
89
        $params = RequestParamValidator::validateParams($params);
90
        curl_setopt_array($ch, array_replace($this->options, [
91
            CURLOPT_URL            => !$params ? $url : $url . '?' . http_build_query($params, '', '&'),
92
            CURLOPT_HTTPHEADER     => $this->credential->getOAuthHeadersForOAuthEcho(),
93
            CURLOPT_RETURNTRANSFER => true,
94
            CURLOPT_HEADER         => true,
95
            CURLOPT_HTTPGET        => true,
96
        ]));
97
        return $ch;
98
    }
99
100
    public function postOut($endpoint, array $params)
101
    {
102
        $ch = curl_init();
103
        list($url, $extra) = UrlNormalizer::outSplitUrlAndParameters($endpoint);
104
        $params += $extra;
105
        $params = RequestParamValidator::validateParams($params);
106
        curl_setopt_array($ch, array_replace($this->options, [
107
            CURLOPT_URL            => $url,
108
            CURLOPT_HTTPHEADER     => $this->credential->getOAuthHeadersForOAuthEcho(),
109
            CURLOPT_RETURNTRANSFER => true,
110
            CURLOPT_HEADER         => true,
111
            CURLOPT_POST           => true,
112
            CURLOPT_POSTFIELDS     => http_build_query($params, '', '&'),
113
        ]));
114
        return $ch;
115
    }
116
117
    public function postMultipartOut($endpoint, array $params)
118
    {
119
        $ch = curl_init();
120
        list($url, $extra) = UrlNormalizer::outSplitUrlAndParameters($endpoint);
121
        $params += $extra;
122
        $params = RequestParamValidator::validateMultipartParams($params);
123
        curl_setopt_array($ch, array_replace($this->options, [
124
            CURLOPT_URL            => $url,
125
            CURLOPT_HTTPHEADER     => $this->credential->getOAuthHeadersForOAuthEcho(),
126
            CURLOPT_RETURNTRANSFER => true,
127
            CURLOPT_HEADER         => true,
128
            CURLOPT_POST           => true,
129
            CURLOPT_POSTFIELDS     => $params,
130
        ]));
131
        return $ch;
132
    }
133
134
    public function streaming($endpoint, array $params, StreamHandler $handler)
135
    {
136
        $ch = curl_init();
137
        list($url, $extra) = UrlNormalizer::twitterSplitUrlAndParameters($endpoint);
138
        $params += $extra;
139
        $params = RequestParamValidator::validateParams($params);
140
        curl_setopt_array($ch, array_replace($this->options, [
141
            CURLOPT_URL            => $url,
142
            CURLOPT_HTTPHEADER     => $this->credential->getOAuthHeaders($url, 'POST', $params),
143
            CURLOPT_POST           => true,
144
            CURLOPT_POSTFIELDS     => http_build_query($params, '', '&'),
145
            CURLOPT_HEADERFUNCTION => [$handler, 'headerFunction'],
146
            CURLOPT_WRITEFUNCTION  => [$handler, 'writeFunction'],
147
        ]));
148
        return $ch;
149
    }
150
151
    public function oauthForRequestToken($oauth_callback = null)
152
    {
153
        if ($oauth_callback !== null) {
154
            $oauth_callback = RequestParamValidator::validateStringable('oauth_callback', $oauth_callback);
155
        }
156
        $ch = curl_init();
157
        curl_setopt_array($ch, array_replace($this->options, [
158
            CURLOPT_URL            => 'https://api.twitter.com/oauth/request_token',
159
            CURLOPT_HTTPHEADER     => $this->credential->getOAuthHeadersForRequestToken($oauth_callback),
160
            CURLOPT_RETURNTRANSFER => true,
161
            CURLOPT_HEADER         => true,
162
            CURLOPT_POST           => true,
163
            CURLOPT_POSTFIELDS     => '',
164
        ]));
165
        return $ch;
166
    }
167
168
    public function oauthForAccessToken($oauth_verifier)
169
    {
170
        $oauth_verifier = RequestParamValidator::validateStringable('oauth_verifier', $oauth_verifier);
171
        $ch = curl_init();
172
        curl_setopt_array($ch, array_replace($this->options, [
173
            CURLOPT_URL            => 'https://api.twitter.com/oauth/access_token',
174
            CURLOPT_HTTPHEADER     => $this->credential->getOAuthHeadersForAccessToken($oauth_verifier),
175
            CURLOPT_RETURNTRANSFER => true,
176
            CURLOPT_HEADER         => true,
177
            CURLOPT_POST           => true,
178
            CURLOPT_POSTFIELDS     => '',
179
        ]));
180
        return $ch;
181
    }
182
183
    public function xauthForAccessToken($username, $password)
184
    {
185
        $username = RequestParamValidator::validateStringable('username', $username);
186
        $password = RequestParamValidator::validateStringable('password', $password);
187
        $ch = curl_init();
188
        curl_setopt_array($ch, array_replace($this->options, [
189
            CURLOPT_URL            => 'https://api.twitter.com/oauth/access_token',
190
            CURLOPT_HTTPHEADER     => $this->credential->getXAuthHeadersForAccessToken($username, $password),
191
            CURLOPT_RETURNTRANSFER => true,
192
            CURLOPT_HEADER         => true,
193
            CURLOPT_POST           => true,
194
            CURLOPT_POSTFIELDS     => http_build_query([
195
                'x_auth_username' => $username,
196
                'x_auth_password' => $password,
197
                'x_auth_mode'     => 'client_auth',
198
            ], '', '&'),
199
        ]));
200
        return $ch;
201
    }
202
203
    public function oauthForBearerToken()
204
    {
205
        $ch = curl_init();
206
        curl_setopt_array($ch, array_replace($this->options, [
207
            CURLOPT_URL            => 'https://api.twitter.com/oauth2/token',
208
            CURLOPT_HTTPHEADER     => $this->credential->getBasicHeaders(),
209
            CURLOPT_RETURNTRANSFER => true,
210
            CURLOPT_HEADER         => true,
211
            CURLOPT_POST           => true,
212
            CURLOPT_POSTFIELDS     => http_build_query([
213
                'grant_type' => 'client_credentials',
214
            ], '', '&'),
215
        ]));
216
        return $ch;
217
    }
218
219
    public function get2($endpoint, $params = [])
220
    {
221
        $ch = curl_init();
222
        list($url, $extra) = UrlNormalizer::twitterSplitUrlAndParameters($endpoint);
223
        $params += $extra;
224
        $params = RequestParamValidator::validateParams($params);
225
        curl_setopt_array($ch, array_replace($this->options, [
226
            CURLOPT_URL            => !$params ? $url : $url . '?' . http_build_query($params, '', '&'),
227
            CURLOPT_HTTPHEADER     => $this->credential->getBearerHeaders(),
228
            CURLOPT_RETURNTRANSFER => true,
229
            CURLOPT_HEADER         => true,
230
            CURLOPT_HTTPGET        => true,
231
        ]));
232
        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
    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),
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
            ], '', '&', 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