/**
  * @ApiDoc(
  *  description="Get friends from facebook already subscribed in Tastd",
  *  statusCodes={200="Get My friends"},
  *  section="Friends")
  * @Route("/api/friends")
  * @Method({"GET"})
  * @throws CredentialNotFoundException
  * @return View
  */
 public function getAction()
 {
     $user = $this->getUser();
     $facebookCredential = $user->getCredentialByProvider(CredentialProvider::FACEBOOK);
     if (!$facebookCredential) {
         throw new CredentialNotFoundException();
     }
     $this->facebookClient->connect($facebookCredential->getToken());
     $ids = $this->facebookClient->getFriendsIds();
     $friends = $this->userRepository->getUsersByCredentialExternalIds(CredentialProvider::FACEBOOK, $ids);
     return $this->view(array('friends' => $friends, 'ids' => $ids));
 }
 /**
  * @param Credential $credential
  * @param $token
  * @return Credential
  * @throws FacebookException
  */
 private function buildFromFacebookToken(Credential $credential, $token)
 {
     $this->facebookClient->connect($token);
     $graphUser = $this->facebookClient->getGraphUser();
     $credential->setToken($token);
     $credential->setExternalId($graphUser->getProperty('id'));
     $permissions = array();
     $permissionsStdClass = $graphUser->getProperty('permissions')->asArray();
     foreach ($permissionsStdClass as $stdClass) {
         $permissions[] = (array) $stdClass;
     }
     $credential->setPermissions($permissions);
     $credential->setProvider(CredentialProvider::FACEBOOK);
     return $credential;
 }
 /**
  * @param array $data
  *
  * @return Notification
  * @throws \Exception
  */
 public function create($data)
 {
     $userId = $data['user'];
     $user = $this->userRepository->get($userId);
     $credential = $user->getCredentialByProvider(CredentialProvider::FACEBOOK);
     $friends = array();
     if ($credential) {
         $this->facebookClient->connect($credential->getToken());
         $friendsIds = $this->facebookClient->getFriendsIds();
         $friends = $this->userRepository->getUsersByCredentialExternalIds(CredentialProvider::FACEBOOK, $friendsIds);
     }
     $notification = new Notification();
     $notification->setName(Notification::NEW_FACEBOOK_FRIEND);
     $notification->setContent(array('user' => $userId, 'userFullName' => $data['userFullName'], 'image' => $data['image']));
     $translatedMessage = $this->translator->trans($notification->getTranslationKey(), array('%name%' => $data['userFullName']));
     $notification->setMessage($translatedMessage);
     foreach ($friends as $friend) {
         $pushMessage = new PushMessage();
         $pushMessage->setNotification($notification);
         $pushMessage->setUser($friend);
         $notification->addPushMessage($pushMessage);
     }
     return $notification;
 }
 /**
  * @ApiDoc(
  *  description="Post a public invite on facebook wall",
  *  statusCodes={200="Success"},
  *  section="Invite",
  *  parameters={
  *      {"name"="message", "dataType"="string", "required"=true}
  *     }
  * )
  * @Route("/api/facebook-public-invite")
  * @Template{}
  * @Method({"POST"})
  * @return mixed
  * @throws CredentialNotFoundException
  */
 public function facebookPublicInviteAction()
 {
     $request = $this->requestStack->getCurrentRequest();
     $message = $request->request->get('message');
     $user = $this->getUser();
     $credential = $user->getCredentialByProvider(CredentialProvider::FACEBOOK);
     if (!$credential) {
         throw new CredentialNotFoundException();
     }
     $this->facebookClient->connect($credential->getToken());
     $invite = new Invite();
     $invite->setChannel(InviteChannel::FACEBOOK);
     $invite->setUser($user);
     $this->entityManager->persist($invite);
     $this->entityManager->flush();
     $this->facebookClient->publishLink('https://tastdapp.com/invite?invite=' . $invite->getCode(), $message);
     return $this->view(array('invite' => $invite), 201);
 }