Rubenwouters\CrmLauncher\Models\Contact::createContact PHP Метод

createContact() публичный Метод

Inserts new contact in DB
public createContact ( string $type, array $message ) : object
$type string
$message array
Результат object
    public function createContact($type, $message)
    {
        if ($type == "twitter_mention") {
            $contact = $this->getContact('twitter', $message['user']['id_str']);
            $contact->name = $message['user']['name'];
            $contact->twitter_handle = $message['user']['screen_name'];
            $contact->twitter_id = $message['user']['id_str'];
            $contact->profile_picture = $message['user']['profile_image_url'];
        } else {
            if ($type == "twitter_direct") {
                $contact = $this->getContact('twitter', $message['sender']['id_str']);
                $contact->name = $message['sender']['name'];
                $contact->twitter_handle = $message['sender']['screen_name'];
                $contact->twitter_id = $message['sender']['id_str'];
                $contact->profile_picture = $message['sender']['profile_image_url'];
            } else {
                if ($type == "facebook") {
                    $contact = $this->getContact('facebook', $message->from->id);
                    $contact->name = $message->from->name;
                    $contact->facebook_id = $message->from->id;
                    $contact->profile_picture = getProfilePicture($message->from->id);
                }
            }
        }
        $contact->save();
        return $contact;
    }

Usage Example

Пример #1
0
 /**
  * Fetch inner comments of facebook
  * @param  datetime $newest
  * @return void
  */
 private function fetchInnerComments($newest)
 {
     $messages = $this->getComments();
     foreach ($messages as $key => $message) {
         $comments = $this->facebookContent->fetchInnerComments($newest, $message['fb_post_id']);
         if ($comments == null) {
             continue;
         }
         foreach ($comments->data as $key => $comment) {
             if ($comment->from->id != config('crm-launcher.facebook_credentials.facebook_page_id') && new Datetime(changeFbDateFormat($comment->created_time)) > new Datetime($newest)) {
                 if (!$this->contact->findByFbId($comment->from->id)->exists()) {
                     $contact = $this->contact->createContact('facebook', $comment);
                 } else {
                     $contact = $this->contact->where('facebook_id', $comment->from->id)->first();
                 }
                 $innerComment = new InnerComment();
                 $innerComment->fb_reply_id = $message['fb_post_id'];
                 $innerComment->post_date = changeFbDateFormat($comment->created_time);
                 $innerComment->contact_id = $contact->id;
                 if (is_a($message, "Rubenwouters\\CrmLauncher\\Models\\Answer")) {
                     $innerComment->answer_id = $message['id'];
                 } else {
                     if (is_a($message, "Rubenwouters\\CrmLauncher\\Models\\Reaction")) {
                         $innerComment->reaction_id = $message['id'];
                     } else {
                         $innerComment->message_id = $message['id'];
                     }
                 }
                 $innerComment->fb_post_id = $comment->id;
                 $innerComment->message = $comment->message;
                 $innerComment->save();
                 $this->media->handleMedia($innerComment->id, $comment, 'facebook_innerComment');
             }
         }
     }
 }