/**
  * Compares the comments score.
  *
  * @param  CommentInterface $a
  * @param  CommentInterface $b
  * @return -1|0|1           As expected for uasort()
  */
 protected function compare(CommentInterface $a, CommentInterface $b)
 {
     if ($a->getScore() == $b->getScore()) {
         return 0;
     }
     return $a->getScore() < $b->getScore() ? -1 : 1;
 }
 /**
  * Checks if the Security token is allowed to delete a specific Comment.
  *
  * @param  CommentInterface $comment
  * @return boolean
  */
 public function canDelete(CommentInterface $comment)
 {
     if ($comment instanceof SignedCommentInterface) {
         if ($comment->getAuthor() == $this->securityContext->getToken()->getUser() || $this->securityContext->getToken()->getUser()->isVp()) {
             return true;
         }
     }
     return parent::canDelete($comment);
 }
 /**
  * Compares the comments score divided by the number of days since the 1970.
  *
  * The end result is a comment that is newer with tje same votes will be ranked
  * higher.
  *
  * @param  CommentInterface $a
  * @param  CommentInterface $b
  * @return -1|0|1           As expected for uasort()
  */
 protected function compare(CommentInterface $a, CommentInterface $b)
 {
     $aScore = $a->getScore() / ($a->getCreatedAt()->getTimestamp() / 60 / 60 / 24);
     $bScore = $b->getScore() / ($b->getCreatedAt()->getTimestamp() / 60 / 60 / 24);
     if ($aScore == $bScore) {
         return 0;
     }
     return $aScore < $bScore ? -1 : 1;
 }
 /**
  * Assigns the currently logged in user to a Comment.
  *
  * @throws InvalidArgumentException when the Comment is not a SignedCommentInterface
  * @param CommentInterface $comment
  * @return void
  */
 public function blame(CommentInterface $comment)
 {
     if (!$comment instanceof SignedCommentInterface) {
         throw new InvalidArgumentException('The comment must implement SignedCommentInterface');
     }
     if (null === $this->securityContext->getToken()) {
         throw new RuntimeException('You must configure a firewall for this route');
     }
     if ($this->securityContext->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
         $comment->setAuthor($this->securityContext->getToken()->getUser());
     }
 }
 /**
  * {@inheritdoc}
  */
 public function saveComment(CommentInterface $comment)
 {
     if (null === $comment->getThread()) {
         throw new InvalidArgumentException('The comment must have a thread');
     }
     $event = new CommentPersistEvent($comment);
     $this->dispatcher->dispatch(Events::COMMENT_PRE_PERSIST, $event);
     if ($event->isPersistenceAborted()) {
         return false;
     }
     $this->doSaveComment($comment);
     $event = new CommentEvent($comment);
     $this->dispatcher->dispatch(Events::COMMENT_POST_PERSIST, $event);
     return true;
 }
 /**
  * {@inheritDoc}
  */
 public function saveComment(CommentInterface $comment)
 {
     if (!$this->threadAcl->canView($comment->getThread())) {
         throw new AccessDeniedException();
     }
     if (!$this->commentAcl->canReply($comment->getParent())) {
         throw new AccessDeniedException();
     }
     $newComment = $this->isNewComment($comment);
     if (!$newComment && !$this->commentAcl->canEdit($comment)) {
         throw new AccessDeniedException();
     }
     if (($comment::STATE_DELETED === $comment->getState() || $comment::STATE_DELETED === $comment->getPreviousState()) && !$this->commentAcl->canDelete($comment)) {
         throw new AccessDeniedException();
     }
     $this->realManager->saveComment($comment);
     if ($newComment) {
         $this->commentAcl->setDefaultAcl($comment);
     }
 }
 /**
  * {@inheritdoc}
  */
 public function setParent(CommentInterface $parent)
 {
     $this->parent = $parent;
     if (!$parent->getId()) {
         throw new InvalidArgumentException('Parent comment must be persisted.');
     }
     $ancestors = $parent->getAncestors();
     $ancestors[] = $parent->getId();
     $this->setAncestors($ancestors);
 }
 /**
  * {@inheritDoc}
  */
 public function addComment(CommentInterface $comment, CommentInterface $parent = null)
 {
     if (!$this->threadAcl->canView($comment->getThread())) {
         throw new AccessDeniedException();
     }
     if (!$this->commentAcl->canReply($parent)) {
         throw new AccessDeniedException();
     }
     $this->realManager->addComment($comment, $parent);
     $this->commentAcl->setDefaultAcl($comment);
 }
 /**
  * Adds a comment
  *
  * @param CommentInterface $comment
  */
 protected function doSaveComment(CommentInterface $comment)
 {
     $this->dm->persist($comment->getThread());
     $this->dm->persist($comment);
     $this->dm->flush();
 }
 /**
  * Adds a comment
  *
  * @param CommentInterface $comment
  */
 public function addComment(CommentInterface $comment)
 {
     if (null !== $comment->getId()) {
         throw new InvalidArgumentException('Can not add already saved comment');
     }
     if (null === $comment->getThread()) {
         throw new InvalidArgumentException('The comment must have a thread');
     }
     $thread = $comment->getThread();
     $thread->incrementNumComments(1);
     $thread->setLastCommentAt(new DateTime());
     $this->em->persist($thread);
     $this->em->persist($comment);
     $this->em->flush();
 }
 /**
  * Checks if the current user is able to edit a comment.
  *
  * @param CommentInterface $comment
  *
  * @return bool If the user is able to comment
  */
 public function canEditComment(CommentInterface $comment)
 {
     if (!$comment->getThread()->isCommentable()) {
         return false;
     }
     if (null === $this->commentAcl) {
         return false;
     }
     return $this->commentAcl->canEdit($comment);
 }
 /**
  * Compiles comment data into a format Akismet accepts.
  *
  * @param  CommentInterface $comment
  * @return array
  */
 protected function getCommentData(CommentInterface $comment)
 {
     $data = array('comment_type' => 'comment', 'comment_content' => $comment->getBody());
     $data['comment_author'] = $comment->getAuthorName();
     return $data;
 }