protected static function boot()
 {
     parent::boot();
     static::deleting(function ($userSocialToken) {
         UserSocialPost::join('social_post', 'social_post.id', '=', 'user_social_post.social_post_id')->where('user_social_post.user_id', '=', $userSocialToken->user_id)->where('social_post.type', '=', $userSocialToken->type)->delete();
         UserSocialTokenFilter::where('user_social_token_filter.user_social_token_id', '=', $userSocialToken->id)->delete();
     });
 }
 public function assign()
 {
     DB::transaction(function () {
         $postCreatedTimestamp = strtotime($this->post['created_time']);
         $postCreatedTime = Carbon::createFromTimestamp($postCreatedTimestamp);
         //Save the post
         $socialPost = new SocialPost();
         $socialPost->type = 'facebook';
         $socialPost->post = json_encode($this->post);
         $socialPost->created_at = $postCreatedTime;
         $socialPost->save();
         //Assign social post to the user
         $userSocialPost = new UserSocialPost();
         $userSocialPost->user_id = $this->userId;
         $userSocialPost->social_post_id = $socialPost->id;
         $userSocialPost->created_at = $postCreatedTime;
         $userSocialPost->save();
     });
     return true;
 }
 public function assign()
 {
     DB::transaction(function () {
         if (!$this->checkPostInDB($this->post->id, $this->userId)) {
             $postCreatedTime = Carbon::createFromTimestamp($this->post->created_time);
             //Save the post
             $socialPost = new SocialPost();
             $socialPost->type = 'instagram';
             $socialPost->post = json_encode($this->post);
             $socialPost->created_at = $postCreatedTime;
             $socialPost->save();
             //Assign social post to the user
             $userSocialPost = new UserSocialPost();
             $userSocialPost->user_id = $this->userId;
             $userSocialPost->social_post_id = $socialPost->id;
             $userSocialPost->created_at = $postCreatedTime;
             $userSocialPost->save();
         }
     });
     return true;
 }
 public function handle()
 {
     $facebook = App::make('SammyK\\LaravelFacebookSdk\\LaravelFacebookSdk');
     //Check if long term access token is set
     if (!$this->userFacebookToken->long_lived_token) {
         //If user has no short term token then abort as cannot extend
         if (!$this->userFacebookToken->short_lived_token) {
             abort(422, 'Cannot access posts for user without short lived token!');
         }
         $tokenExtender = new ExtendFacebookShortLiveToken($this->userFacebookToken);
         $tokenExtender->extend();
     }
     $facebook->setDefaultAccessToken($this->userFacebookToken->long_lived_token);
     $firstCall = true;
     $feedQuery = $this->userFacebookToken->entity_id . '/feed?fields=call_to_action,caption,link,message_tags,full_picture,message,description,from{name,picture},icon,name,place,source,story_tags,story,created_time,type,with_tags&limit=100';
     if ($lastFacebookPostByUser = UserSocialPost::join('social_post', 'user_social_post.social_post_id', '=', 'social_post.id')->where('user_social_post.user_id', '=', $this->userFacebookToken->user_id)->where('social_post.type', '=', 'facebook')->orderBy('user_social_post.created_at', 'DESC')->first()) {
         $firstCall = false;
         $feedQuery = $feedQuery . '&since=' . $lastFacebookPostByUser->created_at;
     }
     do {
         try {
             $latestPostsRaw = $facebook->get($feedQuery);
             $latestPostsDecoded = $latestPostsRaw->getDecodedBody();
             $latestPostsJSON = $latestPostsDecoded['data'];
         } catch (Facebook\Exceptions\FacebookResponseException $e) {
             //Session does not match current stored session!
             $this->userFacebookToken->expires_at = Carbon::now();
         } catch (Facebook\Exceptions\FacebookSDKException $e) {
             //Session does not match current stored session!
             $this->userFacebookToken->expires_at = Carbon::now();
         }
         foreach ($latestPostsJSON as $post) {
             $assignPostToDb = new AssignFacebookPostsToDatabase($this->userFacebookToken->user_id, $post);
             $assignPostToDb->assign();
         }
         $latestPostsPaging = array();
         if (array_key_exists('paging', $latestPostsDecoded)) {
             $latestPostsPaging = $latestPostsDecoded['paging'];
         }
     } while (!$firstCall && array_key_exists('next', $latestPostsPaging) && ($feedQuery = $this->extractMeaningfulPath($latestPostsPaging['next'])));
 }