/**
* @param array $values
* @return Comment
* @throws ActionFailedException
*/
public function save(array $values)
{
$numberOfComments = $this->getNumberOfComments($values['page']);
$repliesReferences = $this->findRepliesReferences($values['text']);
try {
$this->em->beginTransaction();
// no replies references found
if (empty($repliesReferences)) {
$comment = new Comment($values['author'], $this->texy->process($values['text']), $values['page'], $numberOfComments + 1, $this->request->getRemoteAddress());
$this->em->persist($comment)->flush();
$this->em->commit();
return $comment;
}
$commentsToReply = $this->findCommentsToReply($values['page'], $repliesReferences);
$values['text'] = $this->replaceReplyReferencesByAuthors($values['text'], $commentsToReply);
$comment = new Comment($values['author'], $this->texy->process($values['text']), $values['page'], $numberOfComments + 1);
$this->em->persist($comment);
/** @var Comment $comment */
foreach ($commentsToReply as $commentToReply) {
$commentToReply->addReaction($comment);
$this->em->persist($commentToReply);
}
$this->em->flush();
$this->em->commit();
} catch (\Exception $e) {
$this->em->rollback();
$this->em->close();
throw new ActionFailedException();
}
return $comment;
}