/**
  * Route to add friends (which also accepts friend requests)
  *
  * @param User $friend User to link current user with
  *
  * @return null
  *
  * @ApiDoc(
  *  resource=false,
  *  description="Creates a friendship request of current user to desired one",
  *  statusCodes={
  *      401 = "Unauthorized - log in first",
  *      204 = "Successfully requested",
  *      409 = "User already added as friend"
  *  }
  * )
  * @Annotations\Link(requirements={"friend"="\w{24}"})
  */
 public function linkAction(User $friend)
 {
     $currentUser = $this->getUser();
     if (!$currentUser instanceof User) {
         throw new UnauthorizedHttpException('Probably you are not authorized');
     }
     $currentUserId = $currentUser->getId();
     $friendId = $friend->getId();
     if ($currentUser->hasFriend($friendId)) {
         $conflictMessage = sprintf('user %s is already your friend', $friendId);
         throw new ConflictHttpException($conflictMessage);
     }
     // Add a friend (subscription) and make friendship request
     $currentUser->addFriend($friendId);
     $friend->addRequest($currentUserId);
     // Accept removes request if exists any
     $currentUser->removeRequest($friendId);
     // Persist changes to DB
     $documentManager = $this->get('doctrine.odm.mongodb.document_manager');
     $documentManager->flush();
 }
Example #2
0
 /**
  * @param User $friend
  *
  * @return bool
  */
 public function isFriend($friend)
 {
     /* @var User $userFriend */
     foreach ($this->getFriends() as $userFriend) {
         if ($userFriend->getId() == $friend->getId()) {
             return true;
         }
     }
     return false;
 }