Skip to content

Commit

Permalink
Only assign value if it was given
Browse files Browse the repository at this point in the history
  • Loading branch information
aizatto committed Jan 25, 2012
1 parent 06813cb commit 55d57f6
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 32 deletions.
22 changes: 8 additions & 14 deletions Entity/FacebookManager.php
Expand Up @@ -4,8 +4,9 @@

use Aizatto\Bundle\FacebookBundle\Entity\FacebookUser;
use Aizatto\Bundle\FacebookBundle\Entity\FacebookFriend;
use Doctrine\ORM\EntityManager;
use BaseFacebook;
use Doctrine\ORM\EntityManager;
use Symfony\Bridge\Monolog\Logger;

class FacebookManager {

Expand All @@ -18,7 +19,7 @@ class FacebookManager {

public function __construct(BaseFacebook $facebook,
EntityManager $em,
$logger,
Logger $logger,
$facebook_user_class,
$facebook_friend_class) {
$this->em = $em;
Expand All @@ -41,21 +42,14 @@ public function updateFacebookUserThroughAPI(FacebookUser $facebook_user) {
}

public function updateFacebookUserThroughFQL(FacebookUser $facebook_user) {
$fql = $this->getUserFQL("me()");
$fql = $this->getUserFQL("uid = me()");
$data = $this->facebook->api('/fql', array(
'q' => $fql,
'access_token' => $facebook_user->getAccessToken(),
));
$facebook_user->setDataFromFQL($data);
}

private function updateFriendsAndCheckedAt(User $user) {
$user->setCheckedFacebookAt(new \DateTime());
$this->em->persist($user);
$this->em->flush();
$this->updateFriends($user);
}

/**
* https://developers.facebook.com/docs/reference/fql/user/
*/
Expand All @@ -70,7 +64,7 @@ public function updateFriends(User $user) {
->findOneBy(array('facebook_id' => $facebook_id));

try {
$fql = $this->getUserFQL("SELECT uid2 FROM friend WHERE uid1 = me()");
$fql = $this->getUserFQL("uid IN (SELECT uid2 FROM friend WHERE uid1 = me()");
$data = $this->facebook->api('/fql', array(
'q' => $fql,
'access_token' => $data->getAccessToken(),
Expand Down Expand Up @@ -130,14 +124,14 @@ public function updateFriends(User $user) {
$this->em->flush();
}

private function getUserFQL($subquery) {
private function getUserFQL($conditions) {
return <<<SQL
SELECT
uid, name, first_name, middle_name, last_name, username, birthday_date,
sex, locale, current_location, hometown_location, profile_url, verified
sex, locale, current_location, hometown_location, profile_url, verified,
pic, pic_small, pic_big, pic_square
FROM user
WHERE uid IN ($subquery)
WHERE {$conditions}
SQL;
}

Expand Down
71 changes: 53 additions & 18 deletions Entity/FacebookUser.php
Expand Up @@ -665,28 +665,63 @@ public function getVerified()
}

public function setDataFromAPI($data) {
$this
->setFacebookID(idx($data, 'id'))
->setName(idx($data, 'name'))
->setFirstName(idx($data, 'first_name'))
->setMiddleName(idx($data, 'middle_name'))
->setLastName(idx($data, 'last_name'))
->setLink(idx($data, 'link'))
->setUsername(idx($data, 'username'))
->setEmail(idx($data, 'email'))
->setGender(idx($data, 'gender'))
->setLocale(idx($data, 'locale'))
->setVerified(idx($data, 'verified'));
if ($value = idx($data, 'id')) {
$this->setFacebookID($value);
}

if ($value = idx($data, 'name')) {
$this->setName($value);
}

if ($value = idx($data, 'first_name')) {
$this->setFirstName($value);
}

if ($value = idx($data, 'middle_name')) {
$this->setMiddleName($value);
}

if ($value = idx($data, 'last_name')) {
$this->setLastName($value);
}

if ($value = idx($data, 'link')) {
$this->setLink($value);
}

if ($value = idx($data, 'username')) {
$this->setUsername($value);
}

if ($value = idx($data, 'email')) {
$this->setEmail($value);
}

if ($value = idx($data, 'gender')) {
$this->setGender($value);
}

if ($value = idx($data, 'locale')) {
$this->setLocale($value);
}

if ($value = idx($data, 'verified')) {
$this->setVerified($value);
}

$hometown = idx($data, 'hometown', array());
$this
->setHometownID(idx($hometown, 'id'))
->setHometownName(idx($hometown, 'name'));
if ($hometown) {
$this
->setHometownID(idx($hometown, 'id'))
->setHometownName(idx($hometown, 'name'));
}

$location = idx($data, 'location', array());
$this
->setLocationID(idx($location, 'id'))
->setLocationName(idx($location, 'name'));
if ($location) {
$this
->setLocationID(idx($location, 'id'))
->setLocationName(idx($location, 'name'));
}

if (idx($data, 'birthday')) {
$birthday = DateTime::createFromFormat('m/d/Y', $data['birthday']);
Expand Down

0 comments on commit 55d57f6

Please sign in to comment.