/**
  * @param Notification $notification
  */
 protected function hydratePicture(Notification $notification)
 {
     $content = $notification->getContent();
     if (array_key_exists('image', $content)) {
         $content['image'] = $this->cloudfrontUrl . $content['image'];
     } else {
         $content['image'] = $this->cloudfrontUrl . 'avatar/_default_avatar.png';
     }
     $notification->setContent($content);
 }
 /**
  * @param Notification $notification
  * @param string       $deviceToken
  *
  * @return iOSMessage
  */
 protected function createMessage(Notification $notification, $deviceToken)
 {
     $pushNotification = new iOSMessage();
     $pushNotification->setMessage($notification->getMessage());
     $pushNotification->setDeviceIdentifier($deviceToken['token']);
     $pushNotification->setAPSSound('default');
     $pushNotification->setAPSBadge($deviceToken['badge']);
     /* @TODO send extra data to the client or not
        foreach ($notification->getContent() as $key => $value) {
            $pushNotification->addCustomData($key, $value);
        }
        */
     return $pushNotification;
 }
 /**
  * @param Notification $notification
  *
  * @return array
  */
 public function getNotificationTokens(Notification $notification)
 {
     $sql = 'SELECT recipients.badge AS badge, devices.token as token
         FROM (
             SELECT COUNT(push_messages.id) AS badge, push_messages.user_id AS user_id
             FROM push_messages
             WHERE push_messages.seen = 0
             GROUP BY push_messages.user_id
             ) AS recipients
         LEFT JOIN push_messages ON push_messages.user_id = recipients.user_id
         LEFT JOIN devices ON devices.user_id = recipients.user_id
         WHERE push_messages.notification_id = :notification_id
         AND  devices.token IS NOT NULL; ';
     $stmt = $this->getEntityManager()->getConnection()->prepare($sql);
     $stmt->bindValue('notification_id', $notification->getId());
     $stmt->execute();
     $results = $stmt->fetchAll();
     return $results;
 }
 /**
  * @param array $data
  *
  * @return Notification
  */
 public function create($data)
 {
     $notification = new Notification();
     $notification->setName(Notification::LEADER_WISH);
     $notification->setContent(array('wish' => $data['wish'], 'userFullName' => $data['userFullName'], 'user' => $data['user'], 'restaurant' => $data['restaurant'], 'restaurantName' => $data['restaurantName'], 'image' => $data['image']));
     $translatedMessage = $this->translator->trans($notification->getTranslationKey(), array('%name%' => $data['userFullName'], '%restaurant%' => $data['restaurantName']));
     $notification->setMessage($translatedMessage);
     $followersIds = $this->userRepository->getFollowersIdsWithWish($data['user'], $data['wish']);
     foreach ($followersIds as $followersId) {
         $this->addPushMessage($notification, $followersId);
     }
     return $notification;
 }
 /**
  * @param array $data
  *
  * @return Notification
  */
 public function create($data)
 {
     $notification = new Notification();
     $notification->setName(Notification::LEADER_FOLLOWS);
     $notification->setContent(array('follower' => $data['follower'], 'followerFullName' => $data['followerFullName'], 'leader' => $data['leader'], 'leaderFullName' => $data['leaderFullName'], 'image' => $data["followerImage"]));
     $followersIds = $this->userRepository->getFollowersIds($data['follower']);
     $translatedMessage = $this->translator->trans($notification->getTranslationKey(), array('%name%' => $data['followerFullName'], '%leader%' => $data['leaderFullName']));
     $notification->setMessage($translatedMessage);
     foreach ($followersIds as $followerId) {
         $this->addPushMessage($notification, $followerId);
     }
     return $notification;
 }
 /**
  * @param array $data
  *
  * @return Notification
  */
 public function create($data)
 {
     $leaderId = $data['leader'];
     $notification = new Notification();
     $notification->setName(Notification::NEW_FOLLOWER);
     $notification->setContent(array('follower' => $data['follower'], 'followerFullName' => $data['followerFullName'], 'image' => $data['followerImage']));
     $translatedMessage = $this->translator->trans($notification->getTranslationKey(), array('%name%' => $data['followerFullName']));
     $notification->setMessage($translatedMessage);
     $pushMessage = new PushMessage();
     $pushMessage->setNotification($notification);
     $pushMessage->setUser($this->entityManager->getReference(User::CLASS_NAME, $leaderId));
     $notification->addPushMessage($pushMessage);
     return $notification;
 }
 /**
  * @param array $data
  *
  * @return Notification
  */
 public function create($data)
 {
     $userId = $data['user'];
     $reviewId = $data['review'];
     $newTaggedFriendsIdsString = $data['newTaggedFriends'];
     $notification = new Notification();
     $notification->setName(Notification::NEW_TAGGED_FRIENDS);
     $notification->setContent(array('review' => $reviewId, 'restaurant' => $data['restaurant'], 'restaurantName' => $data['restaurantName'], 'user' => $data['user'], 'userFullName' => $data['userFullName'], 'image' => $data['image']));
     $translatedMessage = $this->translator->trans($notification->getTranslationKey(), array('%name%' => $data['userFullName'], '%restaurant%' => $data['restaurantName']));
     $notification->setMessage($translatedMessage);
     $newTaggedFriendsIds = explode(',', $newTaggedFriendsIdsString);
     foreach ($newTaggedFriendsIds as $newTaggedFriendId) {
         $this->addPushMessage($notification, $newTaggedFriendId);
     }
     return $notification;
 }
 /**
  * @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;
 }
 /**
  * @param Notification $notification
  * @param string       $messageId
  */
 protected function processNotification(Notification $notification, $messageId)
 {
     $messageLog = sprintf('Process notification %s %s', $messageId, $notification->getMessage());
     $this->output->writeln($messageLog);
     $notification->setQueueId($messageId);
     $errors = $this->validator->validate($notification);
     if (count($errors) === 0) {
         $this->entityManager->persist($notification);
         $this->entityManager->flush();
         $this->output->writeln('Notification persisted');
         $this->notificationDispatcher->dispatchPushMessages($notification);
         $this->output->writeln('Notification dispatched');
     } else {
         $errorString = (string) $errors;
         $this->output->writeln('Notification validation error' . $errorString);
     }
 }