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\OAuth\AuthenticationToken; |
16
|
|
|
use Twitter\API\REST\Query\DirectMessage\DirectMessageQuery; |
17
|
|
|
use Twitter\API\REST\Query\DirectMessage\SentDirectMessageQuery; |
18
|
|
|
use Twitter\API\REST\Query\Friends\FriendsListQuery; |
19
|
|
|
use Twitter\API\REST\Query\Stream\UserStreamQuery; |
20
|
|
|
use Twitter\API\REST\Query\Tweet\MentionsTimelineQuery; |
21
|
|
|
use Twitter\API\REST\Query\Tweet\UserTimelineQuery; |
22
|
|
|
use Twitter\API\REST\Query\User\UserInformationQuery; |
23
|
|
|
use Twitter\API\REST\Response\ApiResponse; |
24
|
|
|
use Twitter\API\REST\TwitterApiGateway; |
25
|
|
|
|
26
|
|
|
class CodebirdTwitterApiGateway implements TwitterApiGateway, LoggerAwareInterface |
27
|
|
|
{ |
28
|
|
|
use LoggerAwareTrait; |
29
|
|
|
|
30
|
|
|
/** @var Codebird */ |
31
|
|
|
private $codebird; |
32
|
|
|
|
33
|
|
|
/** @var CodebirdResponseParser */ |
34
|
|
|
private $responseParser; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Constructor |
38
|
|
|
* |
39
|
|
|
* @param Codebird $codebird |
40
|
|
|
* @param CodebirdResponseParser $responseParser |
41
|
|
|
*/ |
42
|
54 |
|
public function __construct( |
43
|
|
|
Codebird $codebird, |
44
|
|
|
CodebirdResponseParser $responseParser |
45
|
|
|
) { |
46
|
54 |
|
$this->codebird = $codebird; |
47
|
54 |
|
$this->codebird->setReturnFormat('OBJECT'); |
48
|
|
|
|
49
|
54 |
|
$this->responseParser = $responseParser; |
50
|
54 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Authenticate a user |
54
|
|
|
* |
55
|
|
|
* @param AuthenticationToken $token |
56
|
|
|
*/ |
57
|
6 |
|
public function authenticate(AuthenticationToken $token) |
58
|
|
|
{ |
59
|
6 |
|
$this->codebird->setToken($token->getToken(), $token->getSecret()); |
60
|
6 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get Oauth authentication URL |
64
|
|
|
* |
65
|
|
|
* @return string |
66
|
|
|
* |
67
|
|
|
* @throws TwitterException |
68
|
|
|
*/ |
69
|
3 |
|
public function getAuthenticationUrl() |
70
|
|
|
{ |
71
|
3 |
|
$reply = $this->responseParser->parseObject( |
72
|
3 |
|
$this->callApi('oauth_requestToken') |
|
|
|
|
73
|
3 |
|
)->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->responseParser->parseObject( |
92
|
3 |
|
$this->callApi('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->callApi('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->responseParser->parseObject( |
124
|
3 |
|
$this->callApi('users_show', $request->toArray()) |
|
|
|
|
125
|
2 |
|
); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Get the tweets mentioning the user |
130
|
|
|
* |
131
|
|
|
* @param MentionsTimelineQuery $query |
132
|
|
|
* |
133
|
|
|
* @return ApiResponse |
134
|
|
|
* |
135
|
|
|
* @throws TwitterException |
136
|
|
|
*/ |
137
|
3 |
|
public function statusesMentionsTimeLine(MentionsTimelineQuery $query) |
138
|
|
|
{ |
139
|
3 |
|
return $this->responseParser->parseList( |
140
|
3 |
|
$this->callApi('statuses_mentionsTimeline', $query->toArray()) |
|
|
|
|
141
|
2 |
|
); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Get the tweets of the user |
146
|
|
|
* |
147
|
|
|
* @param UserTimelineQuery $query |
148
|
|
|
* |
149
|
|
|
* @return ApiResponse |
150
|
|
|
* |
151
|
|
|
* @throws TwitterException |
152
|
|
|
*/ |
153
|
3 |
|
public function statusesUserTimeLine(UserTimelineQuery $query) |
154
|
|
|
{ |
155
|
3 |
|
return $this->responseParser->parseList( |
156
|
3 |
|
$this->callApi('statuses_userTimeline', $query->toArray()) |
|
|
|
|
157
|
2 |
|
); |
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->responseParser->parseList( |
172
|
3 |
|
$this->callApi('directMessages', $query->toArray()) |
|
|
|
|
173
|
2 |
|
); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Get the direct messages sent by the user |
178
|
|
|
* |
179
|
|
|
* @param SentDirectMessageQuery $query |
180
|
|
|
* |
181
|
|
|
* @return ApiResponse |
182
|
|
|
* |
183
|
|
|
* @throws TwitterException |
184
|
|
|
*/ |
185
|
3 |
|
public function sentDirectMessages(SentDirectMessageQuery $query) |
186
|
|
|
{ |
187
|
3 |
|
return $this->responseParser->parseList( |
188
|
3 |
|
$this->callApi('directMessages_sent', $query->toArray()) |
|
|
|
|
189
|
2 |
|
); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @param FriendsListQuery $query |
194
|
|
|
* |
195
|
|
|
* @return ApiResponse |
196
|
|
|
* |
197
|
|
|
* @throws TwitterException |
198
|
|
|
*/ |
199
|
3 |
|
public function friends(FriendsListQuery $query) |
200
|
|
|
{ |
201
|
3 |
|
return $this->responseParser->parseObject( |
202
|
3 |
|
$this->callApi('friends_list', $query->toArray()) |
|
|
|
|
203
|
2 |
|
); |
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->responseParser->parseObject( |
218
|
9 |
|
$this->callApi('statuses_update', $parameters->toArray()) |
|
|
|
|
219
|
6 |
|
); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Sends a direct message to $user |
224
|
|
|
* |
225
|
|
|
* @param DirectMessageParameters $parameters |
226
|
|
|
* |
227
|
|
|
* @return ApiResponse |
228
|
|
|
* |
229
|
|
|
* @throws TwitterException |
230
|
|
|
*/ |
231
|
3 |
|
public function newDirectMessage(DirectMessageParameters $parameters) |
232
|
|
|
{ |
233
|
3 |
|
return $this->responseParser->parseObject( |
234
|
3 |
|
$this->callApi('directMessages_new', $parameters->toArray()) |
|
|
|
|
235
|
2 |
|
); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Delete a tweet |
240
|
|
|
* |
241
|
|
|
* @param DeleteTweetParameters $parameters |
242
|
|
|
* |
243
|
|
|
* @return ApiResponse |
244
|
|
|
* |
245
|
|
|
* @throws TwitterException |
246
|
|
|
*/ |
247
|
3 |
|
public function deleteStatus(DeleteTweetParameters $parameters) |
248
|
|
|
{ |
249
|
3 |
|
return $this->responseParser->parseObject( |
250
|
3 |
|
$this->callApi('statuses_destroy_ID', $parameters->toArray()) |
|
|
|
|
251
|
2 |
|
); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Delete a direct message |
256
|
|
|
* |
257
|
|
|
* @param DeleteDirectMessageParameters $parameters |
258
|
|
|
* |
259
|
|
|
* @return ApiResponse |
260
|
|
|
* |
261
|
|
|
* @throws TwitterException |
262
|
|
|
*/ |
263
|
3 |
|
public function deleteDirectMessage(DeleteDirectMessageParameters $parameters) |
264
|
|
|
{ |
265
|
3 |
|
return $this->responseParser->parseObject( |
266
|
3 |
|
$this->callApi('directMessages_destroy', $parameters->toArray()) |
|
|
|
|
267
|
2 |
|
); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Follow a $user |
272
|
|
|
* |
273
|
|
|
* @param FollowParameters $parameters |
274
|
|
|
* |
275
|
|
|
* @return ApiResponse |
276
|
|
|
* |
277
|
|
|
* @throws TwitterException |
278
|
|
|
*/ |
279
|
3 |
|
public function createFriendship(FollowParameters $parameters) |
280
|
|
|
{ |
281
|
3 |
|
return $this->responseParser->parseObject( |
282
|
3 |
|
$this->callApi('friendships_create', $parameters->toArray()) |
|
|
|
|
283
|
2 |
|
); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Unfollow a $user |
288
|
|
|
* |
289
|
|
|
* @param UserIdentifier $parameters |
290
|
|
|
* |
291
|
|
|
* @return ApiResponse |
292
|
|
|
* |
293
|
|
|
* @throws TwitterException |
294
|
|
|
*/ |
295
|
3 |
|
public function destroyFriendship(UserIdentifier $parameters) |
296
|
|
|
{ |
297
|
3 |
|
return $this->responseParser->parseObject( |
298
|
3 |
|
$this->callApi('friendships_destroy', $parameters->toArray()) |
|
|
|
|
299
|
2 |
|
); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Call the twitter API |
304
|
|
|
* |
305
|
|
|
* @param string $slugifiedRoute |
306
|
|
|
* @param string[] $parameters |
307
|
|
|
* |
308
|
|
|
* @return object|array |
309
|
|
|
*/ |
310
|
51 |
|
private function callApi($slugifiedRoute, array $parameters = []) |
311
|
|
|
{ |
312
|
51 |
|
return $this->codebird->{$slugifiedRoute}($parameters); |
313
|
|
|
} |
314
|
|
|
} |
315
|
|
|
|
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.