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($this->codebird->oauth_requestToken())->getContent(); |
74
|
|
|
|
75
|
3 |
|
$this->authenticate(new AuthenticationToken($reply->oauth_token, $reply->oauth_token_secret)); |
76
|
|
|
|
77
|
3 |
|
return $this->codebird->oauth_authorize(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get the authentication token by providing the verifier. |
82
|
|
|
* |
83
|
|
|
* @param string $verificationToken |
84
|
|
|
* |
85
|
|
|
* @return AuthenticationToken |
86
|
|
|
* |
87
|
|
|
* @throws TwitterException |
88
|
|
|
*/ |
89
|
3 |
|
public function getAuthenticationToken($verificationToken) |
90
|
|
|
{ |
91
|
3 |
|
$reply = $this->handleResult( |
92
|
3 |
|
$this->codebird->oauth_accessToken(['oauth_verifier' => $verificationToken]) |
93
|
3 |
|
)->getContent(); |
94
|
|
|
|
95
|
3 |
|
return new AuthenticationToken($reply->oauth_token, $reply->oauth_token_secret); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Sets streaming callback |
100
|
|
|
* |
101
|
|
|
* @param UserStreamQuery $request |
102
|
|
|
* @param callable $callback |
103
|
|
|
* |
104
|
|
|
* @throws \Exception |
105
|
|
|
*/ |
106
|
3 |
|
public function consumeUserStream( |
107
|
|
|
UserStreamQuery $request, |
108
|
|
|
callable $callback |
109
|
|
|
) { |
110
|
3 |
|
$this->codebird->setStreamingCallback($callback); |
111
|
3 |
|
$this->codebird->user($request->toArray()); |
112
|
3 |
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param UserInformationQuery $request |
116
|
|
|
* |
117
|
|
|
* @return ApiResponse |
118
|
|
|
* |
119
|
|
|
* @throws TwitterException |
120
|
|
|
*/ |
121
|
3 |
|
public function getUserInformation(UserInformationQuery $request) |
122
|
|
|
{ |
123
|
3 |
|
return $this->handleResult($this->codebird->users_show($request->toArray())); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Get the tweets mentioning the user |
128
|
|
|
* |
129
|
|
|
* @param MentionsTimelineQuery $query |
130
|
|
|
* |
131
|
|
|
* @return ApiResponse |
132
|
|
|
* |
133
|
|
|
* @throws TwitterException |
134
|
|
|
*/ |
135
|
3 |
|
public function statusesMentionsTimeLine(MentionsTimelineQuery $query) |
136
|
|
|
{ |
137
|
3 |
|
return $this->handleResult( |
138
|
3 |
|
$this->codebird->statuses_mentionsTimeline($query->toArray()), |
139
|
1 |
|
true |
140
|
2 |
|
); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Get the tweets of the user |
145
|
|
|
* |
146
|
|
|
* @param UserTimelineQuery $query |
147
|
|
|
* |
148
|
|
|
* @return ApiResponse |
149
|
|
|
* |
150
|
|
|
* @throws TwitterException |
151
|
|
|
*/ |
152
|
|
|
public function statusesUserTimeLine(UserTimelineQuery $query) |
153
|
|
|
{ |
154
|
|
|
return $this->handleResult( |
155
|
|
|
$this->codebird->statuses_userTimeline($query->toArray()), |
156
|
|
|
true |
157
|
|
|
); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Get the direct messages to teh user |
162
|
|
|
* |
163
|
|
|
* @param DirectMessageQuery $query |
164
|
|
|
* |
165
|
|
|
* @return ApiResponse |
166
|
|
|
* |
167
|
|
|
* @throws TwitterException |
168
|
|
|
*/ |
169
|
3 |
|
public function directMessages(DirectMessageQuery $query) |
170
|
|
|
{ |
171
|
3 |
|
return $this->handleResult( |
172
|
3 |
|
$this->codebird->directMessages($query->toArray()), |
173
|
1 |
|
true |
174
|
2 |
|
); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Get the direct messages sent by the user |
179
|
|
|
* |
180
|
|
|
* @param SentDirectMessageQuery $query |
181
|
|
|
* |
182
|
|
|
* @return ApiResponse |
183
|
|
|
* |
184
|
|
|
* @throws TwitterException |
185
|
|
|
*/ |
186
|
|
|
public function sentDirectMessages(SentDirectMessageQuery $query) |
187
|
|
|
{ |
188
|
|
|
return $this->handleResult( |
189
|
|
|
$this->codebird->directMessages_sent($query->toArray()), |
190
|
|
|
true |
191
|
|
|
); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @param FriendsListQuery $query |
196
|
|
|
* |
197
|
|
|
* @return ApiResponse |
198
|
|
|
* |
199
|
|
|
* @throws TwitterException |
200
|
|
|
*/ |
201
|
|
|
public function friends(FriendsListQuery $query) |
202
|
|
|
{ |
203
|
|
|
return $this->handleResult($this->codebird->friends_list($query->toArray())); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Sends a tweet |
208
|
|
|
* |
209
|
|
|
* @param TweetParameters $parameters |
210
|
|
|
* |
211
|
|
|
* @return ApiResponse |
212
|
|
|
* |
213
|
|
|
* @throws TwitterException |
214
|
|
|
*/ |
215
|
9 |
|
public function updateStatus(TweetParameters $parameters) |
216
|
|
|
{ |
217
|
9 |
|
return $this->handleResult($this->codebird->statuses_update($parameters->toArray())); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Sends a direct message to $user |
222
|
|
|
* |
223
|
|
|
* @param DirectMessageParameters $parameters |
224
|
|
|
* |
225
|
|
|
* @return ApiResponse |
226
|
|
|
* |
227
|
|
|
* @throws TwitterException |
228
|
|
|
*/ |
229
|
3 |
|
public function newDirectMessage(DirectMessageParameters $parameters) |
230
|
|
|
{ |
231
|
3 |
|
return $this->handleResult($this->codebird->directMessages_new($parameters->toArray())); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Delete a tweet |
236
|
|
|
* |
237
|
|
|
* @param DeleteTweetParameters $parameters |
238
|
|
|
* |
239
|
|
|
* @return ApiResponse |
240
|
|
|
* |
241
|
|
|
* @throws TwitterException |
242
|
|
|
*/ |
243
|
|
|
public function deleteStatus(DeleteTweetParameters $parameters) |
244
|
|
|
{ |
245
|
|
|
return $this->handleResult($this->codebird->statuses_destroy_ID($parameters->toArray())); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Delete a direct message |
250
|
|
|
* |
251
|
|
|
* @param DeleteDirectMessageParameters $parameters |
252
|
|
|
* |
253
|
|
|
* @return ApiResponse |
254
|
|
|
* |
255
|
|
|
* @throws TwitterException |
256
|
|
|
*/ |
257
|
|
|
public function deleteDirectMessage(DeleteDirectMessageParameters $parameters) |
258
|
|
|
{ |
259
|
|
|
return $this->handleResult($this->codebird->directMessages_destroy($parameters->toArray())); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Follow a $user |
264
|
|
|
* |
265
|
|
|
* @param FollowParameters $parameters |
266
|
|
|
* |
267
|
|
|
* @return ApiResponse |
268
|
|
|
* |
269
|
|
|
* @throws TwitterException |
270
|
|
|
*/ |
271
|
3 |
|
public function createFriendship(FollowParameters $parameters) |
272
|
|
|
{ |
273
|
3 |
|
return $this->handleResult($this->codebird->friendships_create($parameters->toArray())); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Unfollow a $user |
278
|
|
|
* |
279
|
|
|
* @param UserIdentifier $parameters |
280
|
|
|
* |
281
|
|
|
* @return ApiResponse |
282
|
|
|
* |
283
|
|
|
* @throws TwitterException |
284
|
|
|
*/ |
285
|
3 |
|
public function destroyFriendship(UserIdentifier $parameters) |
286
|
|
|
{ |
287
|
3 |
|
return $this->handleResult($this->codebird->friendships_destroy($parameters->toArray())); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Handles a twitter API response object |
292
|
|
|
* |
293
|
|
|
* @param object $result |
294
|
|
|
* @param bool $isList |
295
|
|
|
* |
296
|
|
|
* @return ApiResponse |
297
|
|
|
* |
298
|
|
|
* @throws TwitterException |
299
|
|
|
*/ |
300
|
33 |
|
private function handleResult($result, $isList = false) |
301
|
|
|
{ |
302
|
33 |
|
$this->handleErrors($result); |
303
|
|
|
|
304
|
30 |
|
$httpStatus = $this->getHttpStatus($result); |
305
|
27 |
|
$rate = $this->getRate($result); |
306
|
27 |
|
$content = $this->getContent($result, $isList); |
307
|
|
|
|
308
|
27 |
|
return new ApiResponse($httpStatus, $content, $rate); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* @param object $result |
313
|
|
|
* |
314
|
|
|
* @throws TwitterException |
315
|
|
|
*/ |
316
|
33 |
|
private function handleErrors($result) |
317
|
|
|
{ |
318
|
33 |
|
if (isset($result->errors)) { |
319
|
3 |
|
$error = reset($result->errors); |
320
|
3 |
|
throw new TwitterException($error->message, $error->code); |
321
|
|
|
} |
322
|
30 |
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* @param object $result |
326
|
|
|
* |
327
|
|
|
* @return HttpStatus |
328
|
|
|
* |
329
|
|
|
* @throws TwitterException |
330
|
|
|
*/ |
331
|
30 |
|
private function getHttpStatus($result) |
332
|
|
|
{ |
333
|
30 |
|
$httpStatus = new HttpStatus($result->httpstatus); |
334
|
30 |
|
if ($httpStatus->isError()) { |
335
|
3 |
|
throw new TwitterException($result->message); |
336
|
|
|
} |
337
|
|
|
|
338
|
27 |
|
return $httpStatus; |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* @param object $result |
343
|
|
|
* |
344
|
|
|
* @return ApiRate |
345
|
|
|
*/ |
346
|
27 |
|
private function getRate($result) |
347
|
|
|
{ |
348
|
27 |
|
if (isset($result->rate)) { |
349
|
|
|
return new LimitedApiRate($result->rate['limit'], $result->rate['remaining'], $result->rate['reset']); |
350
|
|
|
} |
351
|
|
|
|
352
|
27 |
|
return new UnlimitedApiRate(); |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* @param object $result |
357
|
|
|
* @param bool $isList |
358
|
|
|
* |
359
|
|
|
* @return object|array|null |
360
|
|
|
*/ |
361
|
27 |
|
private function getContent($result, $isList) |
362
|
|
|
{ |
363
|
27 |
|
$content = $result; |
364
|
|
|
|
365
|
27 |
|
unset($content->httpstatus, $content->rate); |
366
|
|
|
|
367
|
27 |
|
if ($isList) { |
368
|
6 |
|
$content = []; |
369
|
6 |
|
foreach ($result as $index => $obj) { |
370
|
|
|
if (is_numeric($index)) { |
371
|
2 |
|
$content[(int) $index] = $obj; |
372
|
|
|
} |
373
|
4 |
|
} |
374
|
18 |
|
} elseif (empty($content)) { |
375
|
|
|
$content = null; |
376
|
|
|
} |
377
|
|
|
|
378
|
27 |
|
return $content; |
379
|
|
|
} |
380
|
|
|
} |
381
|
|
|
|