1 | <?php |
||
33 | class TwitterApiClient implements TwitterClient, LoggerAwareInterface |
||
34 | { |
||
35 | use LoggerAwareTrait; |
||
36 | |||
37 | /** @var TwitterApiGateway */ |
||
38 | private $adapter; |
||
39 | |||
40 | /** @var TwitterUserSerializer */ |
||
41 | private $userSerializer; |
||
42 | |||
43 | /** @var TweetSerializer */ |
||
44 | private $tweetSerializer; |
||
45 | |||
46 | /** @var TwitterDirectMessageSerializer */ |
||
47 | private $directMessageSerializer; |
||
48 | |||
49 | /** @var ApiRate[] */ |
||
50 | private $rateLimits; |
||
51 | |||
52 | /** |
||
53 | * Constructor |
||
54 | * |
||
55 | * @param TwitterApiGateway $adapter |
||
56 | * @param TwitterUserSerializer $userSerializer |
||
57 | * @param TweetSerializer $tweetSerializer |
||
58 | * @param TwitterDirectMessageSerializer $directMessageSerializer |
||
59 | */ |
||
60 | 54 | public function __construct( |
|
76 | |||
77 | /** |
||
78 | * @param string $userName |
||
79 | * |
||
80 | * @return TwitterUser |
||
81 | * |
||
82 | * @throws TwitterException |
||
83 | */ |
||
84 | 6 | public function getUser($userName) |
|
99 | |||
100 | /** |
||
101 | * Gets the mention tweets with id between $from and $to |
||
102 | * |
||
103 | * @param string $from |
||
104 | * @param string $to |
||
105 | * |
||
106 | * @return Tweet[] |
||
107 | * |
||
108 | * @throws TwitterException |
||
109 | */ |
||
110 | 9 | public function getMentionsTweets($from = null, $to = null) |
|
136 | |||
137 | /** |
||
138 | * Gets the direct messages with id between $from and $to |
||
139 | * |
||
140 | * @param string $from |
||
141 | * @param string $to |
||
142 | * |
||
143 | * @return TwitterDirectMessage[] |
||
144 | * |
||
145 | * @throws TwitterException |
||
146 | */ |
||
147 | 9 | public function getDirectMessages($from = null, $to = null) |
|
173 | |||
174 | /** |
||
175 | * @param string $userName |
||
176 | * |
||
177 | * @return Tweet[] |
||
178 | * |
||
179 | * @throws TwitterException |
||
180 | */ |
||
181 | 3 | public function getSentTweets($userName) |
|
182 | { |
||
183 | 3 | $this->checkRate(self::GET_SENT_TWEETS); |
|
184 | |||
185 | 3 | $response = $this->adapter->statusesUserTimeLine( |
|
186 | 3 | new UserTimelineQuery( |
|
187 | 3 | UserIdentifier::fromScreenName($userName), |
|
188 | 1 | 200 |
|
189 | 2 | ) |
|
190 | 2 | ); |
|
191 | |||
192 | 3 | $this->handleResponse(self::GET_SENT_TWEETS, $response); |
|
193 | |||
194 | 3 | $tweets = []; |
|
195 | 3 | foreach ($response->getContent() as $index => $obj) { |
|
196 | 3 | $tweet = $this->tweetSerializer->unserialize($obj); |
|
197 | 3 | $id = (string) $tweet->getId(); |
|
198 | 3 | $tweets[(int) $id] = $tweet; |
|
199 | 2 | } |
|
200 | 3 | ksort($tweets); |
|
201 | 3 | return $tweets; |
|
202 | } |
||
203 | |||
204 | /** |
||
205 | * @return TwitterDirectMessage[] |
||
206 | * |
||
207 | * @throws TwitterException |
||
208 | */ |
||
209 | 3 | public function getSentDirectMessages() |
|
210 | { |
||
211 | 3 | $this->checkRate(self::GET_SENT_DIRECT_MESSAGES); |
|
212 | |||
213 | 3 | $response = $this->adapter->sentDirectMessages(new SentDirectMessageQuery(200)); |
|
214 | |||
215 | 3 | $this->handleResponse(self::GET_SENT_DIRECT_MESSAGES, $response); |
|
216 | |||
217 | 3 | $dms = []; |
|
218 | 3 | foreach ($response->getContent() as $index => $obj) { |
|
219 | 3 | $dm = $this->directMessageSerializer->unserialize($obj); |
|
220 | 3 | $id = (string) $dm->getId(); |
|
221 | 3 | $dms[(int) $id] = $dm; |
|
222 | 2 | } |
|
223 | 3 | ksort($dms); |
|
224 | 3 | return $dms; |
|
225 | } |
||
226 | |||
227 | /** |
||
228 | * @param TwitterUser $user |
||
229 | * |
||
230 | * @return TwitterUser[] |
||
231 | * |
||
232 | * @throws TwitterException |
||
233 | */ |
||
234 | 3 | public function getFollowedUsers(TwitterUser $user) |
|
235 | { |
||
236 | 3 | $cursor = -1; |
|
237 | 3 | $serializedUsers = []; |
|
238 | |||
239 | 3 | while ($cursor !== 0) { |
|
240 | 3 | $this->checkRate(self::GET_FOLLOWED_USERS); |
|
241 | |||
242 | 3 | $response = $this->adapter->friends( |
|
243 | 3 | new FriendsListQuery( |
|
244 | 3 | UserIdentifier::fromId($user->getId()), |
|
245 | 3 | 200, |
|
246 | 1 | $cursor |
|
247 | 2 | ) |
|
248 | 2 | ); |
|
249 | |||
250 | 3 | $this->handleResponse(self::GET_FOLLOWED_USERS, $response); |
|
251 | |||
252 | 3 | $friendsResponse = $response->getContent(); |
|
253 | 3 | $serializedUsers = array_merge($serializedUsers, $friendsResponse->users); |
|
254 | |||
255 | 3 | $cursor = $friendsResponse->next_cursor; |
|
256 | 2 | } |
|
257 | |||
258 | 3 | return array_map(function ($serializedUser) { |
|
259 | 3 | return $this->userSerializer->unserialize($serializedUser); |
|
260 | 3 | }, $serializedUsers); |
|
261 | } |
||
262 | |||
263 | /** |
||
264 | * Sends a tweet |
||
265 | * |
||
266 | * @param string $message |
||
267 | * @param Tweet $replyTo |
||
268 | * |
||
269 | * @throws TwitterException |
||
270 | */ |
||
271 | 6 | public function sendTweet($message, Tweet $replyTo = null) |
|
284 | |||
285 | /** |
||
286 | * Sends a direct message to $user |
||
287 | * |
||
288 | * @param TwitterUser $user |
||
289 | * @param string $message |
||
290 | * |
||
291 | * @throws TwitterException |
||
292 | */ |
||
293 | 3 | public function sendDirectMessage(TwitterUser $user, $message) |
|
306 | |||
307 | /** |
||
308 | * @param Tweet $tweet |
||
309 | * |
||
310 | * @throws TwitterException |
||
311 | */ |
||
312 | public function deleteTweet(Tweet $tweet) |
||
322 | |||
323 | /** |
||
324 | * @param TwitterDirectMessage $directMessage |
||
325 | * |
||
326 | * @throws TwitterException |
||
327 | */ |
||
328 | public function deleteDirectMessage(TwitterDirectMessage $directMessage) |
||
338 | |||
339 | /** |
||
340 | * Follow a $user |
||
341 | * |
||
342 | * @param TwitterUser $user |
||
343 | * |
||
344 | * @throws TwitterException |
||
345 | */ |
||
346 | 3 | public function follow(TwitterUser $user) |
|
356 | |||
357 | /** |
||
358 | * Unfollow a $user |
||
359 | * |
||
360 | * @param TwitterUser $user |
||
361 | * |
||
362 | * @throws TwitterException |
||
363 | */ |
||
364 | 3 | public function unfollow(TwitterUser $user) |
|
374 | |||
375 | /** |
||
376 | * @param string $category |
||
377 | * @param ApiRate $rate |
||
378 | */ |
||
379 | 48 | public function setRateLimit($category, ApiRate $rate) |
|
383 | |||
384 | /** |
||
385 | * @param string $category |
||
386 | * |
||
387 | * @throws TwitterRateLimitException |
||
388 | */ |
||
389 | 48 | private function checkRate($category) |
|
400 | |||
401 | /** |
||
402 | * @param string $category |
||
403 | * @param ApiResponse $response |
||
404 | */ |
||
405 | 45 | private function handleResponse($category, ApiResponse $response) |
|
409 | } |
||
410 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: