2014-10-04 13:56:38 +02:00
|
|
|
<?php
|
|
|
|
namespace Szurubooru\Services;
|
2014-10-08 14:47:47 +02:00
|
|
|
use Szurubooru\Dao\CommentDao;
|
|
|
|
use Szurubooru\Dao\TransactionManager;
|
|
|
|
use Szurubooru\Entities\Comment;
|
|
|
|
use Szurubooru\Entities\Post;
|
|
|
|
use Szurubooru\SearchServices\Filters\CommentFilter;
|
|
|
|
use Szurubooru\Services\AuthService;
|
|
|
|
use Szurubooru\Services\TimeService;
|
|
|
|
use Szurubooru\Validator;
|
2014-10-04 13:56:38 +02:00
|
|
|
|
|
|
|
class CommentService
|
|
|
|
{
|
|
|
|
private $validator;
|
|
|
|
private $commentDao;
|
|
|
|
private $transactionManager;
|
|
|
|
private $authService;
|
|
|
|
private $timeService;
|
|
|
|
|
|
|
|
public function __construct(
|
2014-10-08 14:47:47 +02:00
|
|
|
Validator $validator,
|
|
|
|
CommentDao $commentDao,
|
|
|
|
TransactionManager $transactionManager,
|
|
|
|
AuthService $authService,
|
|
|
|
TimeService $timeService)
|
2014-10-04 13:56:38 +02:00
|
|
|
{
|
|
|
|
$this->validator = $validator;
|
|
|
|
$this->commentDao = $commentDao;
|
|
|
|
$this->transactionManager = $transactionManager;
|
|
|
|
$this->authService = $authService;
|
|
|
|
$this->timeService = $timeService;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getById($commentId)
|
|
|
|
{
|
|
|
|
$transactionFunc = function() use ($commentId)
|
|
|
|
{
|
|
|
|
$comment = $this->commentDao->findById($commentId);
|
|
|
|
if (!$comment)
|
|
|
|
throw new \InvalidArgumentException('Comment with ID "' . $commentId . '" was not found.');
|
|
|
|
return $comment;
|
|
|
|
};
|
|
|
|
return $this->transactionManager->rollback($transactionFunc);
|
|
|
|
}
|
|
|
|
|
2014-10-08 14:47:47 +02:00
|
|
|
public function getByPost(Post $post)
|
2014-10-04 13:56:38 +02:00
|
|
|
{
|
|
|
|
$transactionFunc = function() use ($post)
|
|
|
|
{
|
|
|
|
return $this->commentDao->findByPost($post);
|
|
|
|
};
|
|
|
|
return $this->transactionManager->rollback($transactionFunc);
|
|
|
|
}
|
|
|
|
|
2014-10-08 14:47:47 +02:00
|
|
|
public function getFiltered(CommentFilter $filter)
|
2014-10-04 13:56:38 +02:00
|
|
|
{
|
|
|
|
$transactionFunc = function() use ($filter)
|
|
|
|
{
|
|
|
|
return $this->commentDao->findFiltered($filter);
|
|
|
|
};
|
|
|
|
return $this->transactionManager->rollback($transactionFunc);
|
|
|
|
}
|
|
|
|
|
2014-10-08 14:47:47 +02:00
|
|
|
public function createComment(Post $post, $text)
|
2014-10-04 13:56:38 +02:00
|
|
|
{
|
|
|
|
$transactionFunc = function() use ($post, $text)
|
|
|
|
{
|
2014-10-08 14:47:47 +02:00
|
|
|
$comment = new Comment();
|
2014-10-04 13:56:38 +02:00
|
|
|
$comment->setCreationTime($this->timeService->getCurrentTime());
|
|
|
|
$comment->setLastEditTime($this->timeService->getCurrentTime());
|
|
|
|
$comment->setUser($this->authService->isLoggedIn() ? $this->authService->getLoggedInUser() : null);
|
|
|
|
$comment->setPost($post);
|
|
|
|
|
|
|
|
$this->updateCommentText($comment, $text);
|
|
|
|
|
|
|
|
return $this->commentDao->save($comment);
|
|
|
|
};
|
|
|
|
return $this->transactionManager->commit($transactionFunc);
|
|
|
|
}
|
|
|
|
|
2014-10-08 14:47:47 +02:00
|
|
|
public function updateComment(Comment $comment, $newText)
|
2014-10-04 13:56:38 +02:00
|
|
|
{
|
|
|
|
$transactionFunc = function() use ($comment, $newText)
|
|
|
|
{
|
|
|
|
$comment->setLastEditTime($this->timeService->getCurrentTime());
|
|
|
|
|
|
|
|
$this->updateCommentText($comment, $newText);
|
|
|
|
return $this->commentDao->save($comment);
|
|
|
|
};
|
|
|
|
return $this->transactionManager->commit($transactionFunc);
|
|
|
|
}
|
|
|
|
|
2014-10-08 14:47:47 +02:00
|
|
|
public function deleteComment(Comment $comment)
|
2014-10-04 13:56:38 +02:00
|
|
|
{
|
|
|
|
$transactionFunc = function() use ($comment)
|
|
|
|
{
|
|
|
|
$this->commentDao->deleteById($comment->getId());
|
|
|
|
};
|
|
|
|
$this->transactionManager->commit($transactionFunc);
|
|
|
|
}
|
|
|
|
|
2014-10-08 14:47:47 +02:00
|
|
|
private function updateCommentText(Comment $comment, $text)
|
2014-10-04 13:56:38 +02:00
|
|
|
{
|
|
|
|
$this->validator->validateLength($text, 5, 2000, 'Comment text');
|
|
|
|
$comment->setText($text);
|
|
|
|
}
|
|
|
|
}
|