/**
  * Takes the request and tries to connect the user to an account
  *
  * @param   sfFacebookGraphUser  $user
  * @return  sfFacebookGraphUser
  */
 public static function getCurrentFacebookUser($user)
 {
     // get facebook user
     $facebookUid = sfFacebookGraph::getCurrentUser();
     if (!$facebookUid) {
         throw new Exception('Facebook user not found');
     }
     // get current user info from api
     try {
         $facebookUserInfo = sfFacebookGraph::getCurrentUserInfo();
     } catch (Exception $e) {
         throw $e;
     }
     $accessToken = sfFacebookGraph::getCurrentAccessToken();
     $accessTokenExpiry = sfFacebookGraph::getCurrentAccessExpiry();
     if ($user->isAuthenticated()) {
         if ($user->isFacebookConnected()) {
             // user already signed in
             $user->getProfile()->_facebookUpdateProfile($accessToken, $accessTokenExpiry, $facebookUserInfo, $user->getGuardUser());
         } else {
             if ($user->getProfile()->getFacebookUid() === null) {
                 // user account exists but not connected to facebook
                 $user->getProfile()->_connectToFacebook($facebookUid, $accessToken, $accessTokenExpiry, $facebookUserInfo, $user->getGuardUser());
             } else {
                 // user connected to different facebook account
                 // sign them out (we'll try sign them in again)
                 $user->signOut();
             }
         }
     }
     // check if user exists
     if ($user->isAnonymous()) {
         $userObj = self::getUserByFacebookUid($facebookUid);
         if ($userObj) {
             $user->signIn($userObj, false, null, true);
         }
     }
     // check if a user with the username exists
     if ($user->isAnonymous()) {
         $userObj = self::getUserByFacebookUsername($facebookUid);
         if (!$userObj) {
             // check by email address
             $email = isset($facebookUserInfo['email']) ? $facebookUserInfo['email'] : '';
             if (sfConfig::get('app_facebook_dont_store_proxy_emails', false)) {
                 if (sfFacebookGraph::checkProxyEmail($email)) {
                     $email = '';
                 }
             }
             $userObj = self::getUserByEmail($email);
         }
         if ($userObj) {
             $userObj->getProfile()->_connectToFacebook($facebookUid, $accessToken, $accessTokenExpiry, $facebookUserInfo, $userObj);
             $user->signIn($userObj, false, null, true);
         }
     }
     // if nothing exists create a new account
     if ($user->isAnonymous()) {
         try {
             $user->signIn(self::createUser($facebookUid, $accessToken, $accessTokenExpiry, $facebookUserInfo), false, null, true);
         } catch (Exception $e) {
             throw $e;
         }
     }
     return $user;
 }
 /**
  * Takes the request and tries to connect the user to an account
  *
  * @param   sfFacebookGraphUser  $user
  * @return  sfFacebookGraphUser
  */
 public static function getCurrentFacebookUser($user)
 {
     // get facebook user
     $facebookUid = sfFacebookGraph::getCurrentUser();
     if (!$facebookUid) {
         throw new Exception('Facebook user not found');
     }
     // get current user info from api
     try {
         $facebookUserInfo = sfFacebookGraph::getCurrentUserInfo();
     } catch (Exception $e) {
         throw $e;
     }
     $accessToken = sfFacebookGraph::getCurrentAccessToken();
     $accessTokenExpiry = sfFacebookGraph::getCurrentAccessExpiry();
     // user signed in and facebook connected
     if ($user->isAuthenticated() && $user->isFacebookConnected()) {
         $user->getProfile()->_facebookUpdateProfile($accessToken, $accessTokenExpiry, $facebookUserInfo, $user->getGuardUser());
         return $user;
     }
     // get user object
     // email to find facebook user
     $email = isset($facebookUserInfo['email']) ? $facebookUserInfo['email'] : '';
     if (sfConfig::get('app_facebook_dont_store_proxy_emails', false)) {
         if (sfFacebookGraph::checkProxyEmail($email)) {
             $email = null;
         }
     }
     $userObj = self::getUserByFacebookCredentials($facebookUid, $email);
     if ($user->isAuthenticated()) {
         if ($user === $userObj) {
             $newlyConnected = $user->getProfile()->getFacebookUid() === null;
             // user already signed in
             $user->getProfile()->_connectToFacebook($facebookUid, $accessToken, $accessTokenExpiry, $facebookUserInfo, $user->getGuardUser())->setNewlyConnected($newlyConnected);
             return $user;
         } else {
             if (!$userObj) {
                 // user account exists but not connected to facebook
                 $user->getProfile()->_connectToFacebook($facebookUid, $accessToken, $accessTokenExpiry, $facebookUserInfo, $user->getGuardUser())->setNewlyConnected(true);
                 return $user;
             } else {
                 // user has a different facebook account
                 // sign them out (we'll try sign them in again)
                 $user->signOut();
             }
         }
     }
     // user exists
     if ($userObj) {
         $newlyConnected = $userObj->getProfile()->getFacebookUid() === null;
         $userObj->getProfile()->_connectToFacebook($facebookUid, $accessToken, $accessTokenExpiry, $facebookUserInfo, $userObj)->setNewlyConnected($newlyConnected);
         $user->signIn($userObj, false, null, true);
         return $user;
     }
     try {
         // create new user
         $user->signIn(self::createUser($facebookUid, $accessToken, $accessTokenExpiry, $facebookUserInfo), false, null, true);
     } catch (Exception $e) {
         throw $e;
     }
     return $user;
 }