Rubenwouters\CrmLauncher\ApiCalls\FetchTwitterContent::fetchMentions PHP Method

fetchMentions() public method

Fetch all mentions
public fetchMentions ( ) : array | Illuminate\View\View
return array | Illuminate\View\View
    public function fetchMentions()
    {
        $latestMentionId = latestMentionId();
        try {
            $client = initTwitter();
            if ($latestMentionId) {
                $mentions_response = $client->get('statuses/mentions_timeline.json?since_id=' . $latestMentionId);
            } else {
                $mentions_response = $client->get('statuses/mentions_timeline.json?count=1');
            }
            return json_decode($mentions_response->getBody(), true);
        } catch (\GuzzleHttp\Exception\ClientException $e) {
            getErrorMessage($e->getResponse()->getStatusCode());
            return back();
        }
    }

Usage Example

 /**
  * Gets all public mentions on Twitter
  * @return void
  */
 public function collectMentions()
 {
     $mentions = array_reverse($this->twitterContent->fetchMentions());
     foreach ($mentions as $key => $mention) {
         $date = changeDateFormat($mention['created_at']);
         $inReplyTo = $mention['in_reply_to_status_id_str'];
         $message = new Message();
         if ($inReplyTo == null) {
             $contact = $this->contact->createContact('twitter_mention', $mention);
             $case = $this->case->createCase('twitter_mention', $mention, $contact);
         }
         if (($this->answer->where('tweet_id', $inReplyTo)->exists() || $this->message->where('tweet_id', $inReplyTo)->exists()) && $inReplyTo != null) {
             $contact = $this->contact->createContact('twitter_mention', $mention);
             $message->contact_id = $contact->id;
             if ($this->answer->where('tweet_id', $inReplyTo)->exists()) {
                 $post = $this->answer->where('tweet_id', $inReplyTo)->first();
             } else {
                 $post = $this->message->where('tweet_id', $inReplyTo)->first();
             }
             $message->case_id = $post->case_id;
             $case = $this->case->find($post->case_id);
         } else {
             if ($inReplyTo != null && $this->publishment->where('tweet_id', $inReplyTo)->exists()) {
                 $this->fetchSpecificMention($mention);
                 continue;
             } else {
                 if ($inReplyTo != null) {
                     $message->tweet_reply_id = $inReplyTo;
                 } else {
                     $message->case_id = $case->id;
                 }
             }
         }
         $message->contact_id = $contact->id;
         $message->tweet_id = $mention['id_str'];
         $message->message = filterUrl($mention['text']);
         $message->post_date = $date;
         $message->save();
         $this->media->handleMedia($message->id, $mention, 'twitter');
         $this->updateCase($case->id, 'twitter', $mention['id_str']);
     }
 }