Completed
Push — master ( 055f78...aa6956 )
by Rémi
04:33
created

CodebirdTwitterApiGateway::callApi()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Twitter\API\REST\Gateway;
4
5
use Codebird\Codebird;
6
use Psr\Log\LoggerAwareInterface;
7
use Psr\Log\LoggerAwareTrait;
8
use Twitter\API\Exception\TwitterException;
9
use Twitter\API\REST\DTO\DeleteDirectMessageParameters;
10
use Twitter\API\REST\DTO\DeleteTweetParameters;
11
use Twitter\API\REST\DTO\DirectMessageParameters;
12
use Twitter\API\REST\DTO\FollowParameters;
13
use Twitter\API\REST\DTO\TweetParameters;
14
use Twitter\API\REST\DTO\UserIdentifier;
15
use Twitter\API\REST\Factory\CodebirdFactory;
16
use Twitter\API\REST\OAuth\AuthenticationToken;
17
use Twitter\API\REST\Query\DirectMessage\DirectMessageQuery;
18
use Twitter\API\REST\Query\DirectMessage\SentDirectMessageQuery;
19
use Twitter\API\REST\Query\Friends\FriendsListQuery;
20
use Twitter\API\REST\Query\Stream\UserStreamQuery;
21
use Twitter\API\REST\Query\Tweet\MentionsTimelineQuery;
22
use Twitter\API\REST\Query\Tweet\UserTimelineQuery;
23
use Twitter\API\REST\Query\User\UserInformationQuery;
24
use Twitter\API\REST\Response\ApiRate;
25
use Twitter\API\REST\Response\ApiResponse;
26
use Twitter\API\REST\Response\HttpStatus;
27
use Twitter\API\REST\Response\LimitedApiRate;
28
use Twitter\API\REST\Response\UnlimitedApiRate;
29
use Twitter\API\REST\TwitterApiGateway;
30
31
class CodebirdTwitterApiGateway implements TwitterApiGateway, LoggerAwareInterface
32
{
33
    use LoggerAwareTrait;
34
35
    /** @var Codebird */
36
    private $codebird;
37
38
    /**
39
     * Constructor
40
     *
41
     * @param CodebirdFactory $factory
42
     * @param string          $consumerKey
43
     * @param string          $consumerSecret
44
     */
45 39
    public function __construct(
46
        CodebirdFactory $factory,
47
        $consumerKey,
48
        $consumerSecret
49
    ) {
50 39
        $this->codebird = $factory->build($consumerKey, $consumerSecret);
51 39
        $this->codebird->setReturnFormat('OBJECT');
52 39
    }
53
54
    /**
55
     * Authenticate a user
56
     *
57
     * @param AuthenticationToken $token
58
     */
59 6
    public function authenticate(AuthenticationToken $token)
60
    {
61 6
        $this->codebird->setToken($token->getToken(), $token->getSecret());
62 6
    }
63
64
    /**
65
     * Get Oauth authentication URL
66
     *
67
     * @return string
68
     *
69
     * @throws TwitterException
70
     */
71 3
    public function getAuthenticationUrl()
72
    {
73 3
        $reply = $this->handleResult(
74 3
            $this->callApi('oauth_requestToken')
0 ignored issues
show
Bug introduced by
It seems like $this->callApi('oauth_requestToken') targeting Twitter\API\REST\Gateway...erApiGateway::callApi() can also be of type array; however, Twitter\API\REST\Gateway...Gateway::handleResult() does only seem to accept object, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
75 3
        )->getContent();
76
77 3
        $this->authenticate(new AuthenticationToken($reply->oauth_token, $reply->oauth_token_secret));
78
79 3
        return $this->codebird->oauth_authorize();
80
    }
81
82
    /**
83
     * Get the authentication token by providing the verifier.
84
     *
85
     * @param string $verificationToken
86
     *
87
     * @return AuthenticationToken
88
     *
89
     * @throws TwitterException
90
     */
91 3
    public function getAuthenticationToken($verificationToken)
92
    {
93 3
        $reply = $this->handleResult(
94 3
            $this->callApi('oauth_accessToken', ['oauth_verifier' => $verificationToken])
0 ignored issues
show
Bug introduced by
It seems like $this->callApi('oauth_ac...=> $verificationToken)) targeting Twitter\API\REST\Gateway...erApiGateway::callApi() can also be of type array; however, Twitter\API\REST\Gateway...Gateway::handleResult() does only seem to accept object, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
95 3
        )->getContent();
96
97 3
        return new AuthenticationToken($reply->oauth_token, $reply->oauth_token_secret);
98
    }
99
100
    /**
101
     * Sets streaming callback
102
     *
103
     * @param UserStreamQuery $request
104
     * @param callable        $callback
105
     *
106
     * @throws \Exception
107
     */
108 3
    public function consumeUserStream(
109
        UserStreamQuery $request,
110
        callable $callback
111
    ) {
112 3
        $this->codebird->setStreamingCallback($callback);
113 3
        $this->callApi('user', $request->toArray());
114 3
    }
115
116
    /**
117
     * @param UserInformationQuery $request
118
     *
119
     * @return ApiResponse
120
     *
121
     * @throws TwitterException
122
     */
123 3
    public function getUserInformation(UserInformationQuery $request)
124
    {
125 3
        return $this->handleResult(
126 3
            $this->callApi('users_show', $request->toArray())
0 ignored issues
show
Bug introduced by
It seems like $this->callApi('users_show', $request->toArray()) targeting Twitter\API\REST\Gateway...erApiGateway::callApi() can also be of type array; however, Twitter\API\REST\Gateway...Gateway::handleResult() does only seem to accept object, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
127 2
        );
128
    }
129
130
    /**
131
     * Get the tweets mentioning the user
132
     *
133
     * @param MentionsTimelineQuery $query
134
     *
135
     * @return ApiResponse
136
     *
137
     * @throws TwitterException
138
     */
139 3
    public function statusesMentionsTimeLine(MentionsTimelineQuery $query)
140
    {
141 3
        return $this->handleResult(
142 3
            $this->callApi('statuses_mentionsTimeline', $query->toArray()),
0 ignored issues
show
Bug introduced by
It seems like $this->callApi('statuses...ne', $query->toArray()) targeting Twitter\API\REST\Gateway...erApiGateway::callApi() can also be of type array; however, Twitter\API\REST\Gateway...Gateway::handleResult() does only seem to accept object, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
143 1
            true
144 2
        );
145
    }
146
147
    /**
148
     * Get the tweets of the user
149
     *
150
     * @param UserTimelineQuery $query
151
     *
152
     * @return ApiResponse
153
     *
154
     * @throws TwitterException
155
     */
156
    public function statusesUserTimeLine(UserTimelineQuery $query)
157
    {
158
        return $this->handleResult(
159
            $this->callApi('statuses_userTimeline', $query->toArray()),
0 ignored issues
show
Bug introduced by
It seems like $this->callApi('statuses...ne', $query->toArray()) targeting Twitter\API\REST\Gateway...erApiGateway::callApi() can also be of type array; however, Twitter\API\REST\Gateway...Gateway::handleResult() does only seem to accept object, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
160
            true
161
        );
162
    }
163
164
    /**
165
     * Get the direct messages to teh user
166
     *
167
     * @param DirectMessageQuery $query
168
     *
169
     * @return ApiResponse
170
     *
171
     * @throws TwitterException
172
     */
173 3
    public function directMessages(DirectMessageQuery $query)
174
    {
175 3
        return $this->handleResult(
176 3
            $this->callApi('directMessages', $query->toArray()),
0 ignored issues
show
Bug introduced by
It seems like $this->callApi('directMe...es', $query->toArray()) targeting Twitter\API\REST\Gateway...erApiGateway::callApi() can also be of type array; however, Twitter\API\REST\Gateway...Gateway::handleResult() does only seem to accept object, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
177 1
            true
178 2
        );
179
    }
180
181
    /**
182
     * Get the direct messages sent by the user
183
     *
184
     * @param SentDirectMessageQuery $query
185
     *
186
     * @return ApiResponse
187
     *
188
     * @throws TwitterException
189
     */
190
    public function sentDirectMessages(SentDirectMessageQuery $query)
191
    {
192
        return $this->handleResult(
193
            $this->callApi('directMessages_sent', $query->toArray()),
0 ignored issues
show
Bug introduced by
It seems like $this->callApi('directMe...nt', $query->toArray()) targeting Twitter\API\REST\Gateway...erApiGateway::callApi() can also be of type array; however, Twitter\API\REST\Gateway...Gateway::handleResult() does only seem to accept object, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
194
            true
195
        );
196
    }
197
198
    /**
199
     * @param FriendsListQuery $query
200
     *
201
     * @return ApiResponse
202
     *
203
     * @throws TwitterException
204
     */
205
    public function friends(FriendsListQuery $query)
206
    {
207
        return $this->handleResult(
208
            $this->callApi('friends_list', $query->toArray())
0 ignored issues
show
Bug introduced by
It seems like $this->callApi('friends_list', $query->toArray()) targeting Twitter\API\REST\Gateway...erApiGateway::callApi() can also be of type array; however, Twitter\API\REST\Gateway...Gateway::handleResult() does only seem to accept object, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
209
        );
210
    }
211
212
    /**
213
     * Sends a tweet
214
     *
215
     * @param TweetParameters $parameters
216
     *
217
     * @return ApiResponse
218
     *
219
     * @throws TwitterException
220
     */
221 9
    public function updateStatus(TweetParameters $parameters)
222
    {
223 9
        return $this->handleResult(
224 9
            $this->callApi('statuses_update', $parameters->toArray())
0 ignored issues
show
Bug introduced by
It seems like $this->callApi('statuses...$parameters->toArray()) targeting Twitter\API\REST\Gateway...erApiGateway::callApi() can also be of type array; however, Twitter\API\REST\Gateway...Gateway::handleResult() does only seem to accept object, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
225 6
        );
226
    }
227
228
    /**
229
     * Sends a direct message to $user
230
     *
231
     * @param DirectMessageParameters $parameters
232
     *
233
     * @return ApiResponse
234
     *
235
     * @throws TwitterException
236
     */
237 3
    public function newDirectMessage(DirectMessageParameters $parameters)
238
    {
239 3
        return $this->handleResult(
240 3
            $this->callApi('directMessages_new', $parameters->toArray())
0 ignored issues
show
Bug introduced by
It seems like $this->callApi('directMe...$parameters->toArray()) targeting Twitter\API\REST\Gateway...erApiGateway::callApi() can also be of type array; however, Twitter\API\REST\Gateway...Gateway::handleResult() does only seem to accept object, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
241 2
        );
242
    }
243
244
    /**
245
     * Delete a tweet
246
     *
247
     * @param DeleteTweetParameters $parameters
248
     *
249
     * @return ApiResponse
250
     *
251
     * @throws TwitterException
252
     */
253
    public function deleteStatus(DeleteTweetParameters $parameters)
254
    {
255
        return $this->handleResult(
256
            $this->callApi('statuses_destroy_ID', $parameters->toArray())
0 ignored issues
show
Bug introduced by
It seems like $this->callApi('statuses...$parameters->toArray()) targeting Twitter\API\REST\Gateway...erApiGateway::callApi() can also be of type array; however, Twitter\API\REST\Gateway...Gateway::handleResult() does only seem to accept object, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
257
        );
258
    }
259
260
    /**
261
     * Delete a direct message
262
     *
263
     * @param DeleteDirectMessageParameters $parameters
264
     *
265
     * @return ApiResponse
266
     *
267
     * @throws TwitterException
268
     */
269
    public function deleteDirectMessage(DeleteDirectMessageParameters $parameters)
270
    {
271
        return $this->handleResult(
272
            $this->callApi('directMessages_destroy', $parameters->toArray())
0 ignored issues
show
Bug introduced by
It seems like $this->callApi('directMe...$parameters->toArray()) targeting Twitter\API\REST\Gateway...erApiGateway::callApi() can also be of type array; however, Twitter\API\REST\Gateway...Gateway::handleResult() does only seem to accept object, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
273
        );
274
    }
275
276
    /**
277
     * Follow a $user
278
     *
279
     * @param FollowParameters $parameters
280
     *
281
     * @return ApiResponse
282
     *
283
     * @throws TwitterException
284
     */
285 3
    public function createFriendship(FollowParameters $parameters)
286
    {
287 3
        return $this->handleResult(
288 3
            $this->callApi('friendships_create', $parameters->toArray())
0 ignored issues
show
Bug introduced by
It seems like $this->callApi('friendsh...$parameters->toArray()) targeting Twitter\API\REST\Gateway...erApiGateway::callApi() can also be of type array; however, Twitter\API\REST\Gateway...Gateway::handleResult() does only seem to accept object, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
289 2
        );
290
    }
291
292
    /**
293
     * Unfollow a $user
294
     *
295
     * @param UserIdentifier $parameters
296
     *
297
     * @return ApiResponse
298
     *
299
     * @throws TwitterException
300
     */
301 3
    public function destroyFriendship(UserIdentifier $parameters)
302
    {
303 3
        return $this->handleResult(
304 3
            $this->callApi('friendships_destroy', $parameters->toArray())
0 ignored issues
show
Bug introduced by
It seems like $this->callApi('friendsh...$parameters->toArray()) targeting Twitter\API\REST\Gateway...erApiGateway::callApi() can also be of type array; however, Twitter\API\REST\Gateway...Gateway::handleResult() does only seem to accept object, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
305 2
        );
306
    }
307
308
    /**
309
     * Call the twitter API
310
     *
311
     * @param string   $slugifiedRoute
312
     * @param string[] $parameters
313
     *
314
     * @return object|array
315
     */
316 36
    private function callApi($slugifiedRoute, array $parameters = [])
317
    {
318 36
        return $this->codebird->{$slugifiedRoute}($parameters);
319
    }
320
321
    /**
322
     * Handles a twitter API response object
323
     *
324
     * @param object $result
325
     * @param bool   $isList
326
     *
327
     * @return ApiResponse
328
     *
329
     * @throws TwitterException
330
     */
331 33
    private function handleResult($result, $isList = false)
332
    {
333 33
        $this->handleErrors($result);
334
335 30
        $httpStatus = $this->getHttpStatus($result);
336 27
        $rate = $this->getRate($result);
337 27
        $content = $this->getContent($result, $isList);
338
339 27
        return new ApiResponse($httpStatus, $content, $rate);
340
    }
341
342
    /**
343
     * @param object $result
344
     *
345
     * @throws TwitterException
346
     */
347 33
    private function handleErrors($result)
348
    {
349 33
        if (isset($result->errors)) {
350 3
            $error = reset($result->errors);
351 3
            throw new TwitterException($error->message, $error->code);
352
        }
353 30
    }
354
355
    /**
356
     * @param object $result
357
     *
358
     * @return HttpStatus
359
     *
360
     * @throws TwitterException
361
     */
362 30
    private function getHttpStatus($result)
363
    {
364 30
        $httpStatus = new HttpStatus($result->httpstatus);
365 30
        if ($httpStatus->isError()) {
366 3
            throw new TwitterException($result->message);
367
        }
368
369 27
        return $httpStatus;
370
    }
371
372
    /**
373
     * @param object $result
374
     *
375
     * @return ApiRate
376
     */
377 27
    private function getRate($result)
378
    {
379 27
        if (isset($result->rate)) {
380
            return new LimitedApiRate($result->rate['limit'], $result->rate['remaining'], $result->rate['reset']);
381
        }
382
383 27
        return new UnlimitedApiRate();
384
    }
385
386
    /**
387
     * @param object $result
388
     * @param bool   $isList
389
     *
390
     * @return object|array|null
391
     */
392 27
    private function getContent($result, $isList)
393
    {
394 27
        $content = $result;
395
396 27
        unset($content->httpstatus, $content->rate);
397
398 27
        if ($isList) {
399 6
            $content = [];
400 6
            foreach ($result as $index => $obj) {
401
                if (is_numeric($index)) {
402 2
                    $content[(int) $index] = $obj;
403
                }
404 4
            }
405 18
        } elseif (empty($content)) {
406
            $content = null;
407
        }
408
409 27
        return $content;
410
    }
411
}
412