1
0
mirror of https://gitee.com/koogua/course-tencent-cloud.git synced 2025-06-26 20:52:44 +08:00
2020-07-13 19:23:36 +08:00

89 lines
2.1 KiB
PHP

<?php
namespace App\Services\Frontend\Comment;
use App\Models\Comment as CommentModel;
use App\Models\CommentLike as CommentLikeModel;
use App\Models\User as UserModel;
use App\Services\Frontend\CommentTrait;
use App\Services\Frontend\Service as FrontendService;
use App\Validators\Comment as CommentValidator;
use App\Validators\UserDailyLimit as UserDailyLimitValidator;
use Phalcon\Events\Manager as EventsManager;
class CommentLike extends FrontendService
{
use CommentTrait;
public function handle($id)
{
$comment = $this->checkComment($id);
$user = $this->getLoginUser();
$validator = new UserDailyLimitValidator();
$validator->checkCommentLikeLimit($user);
$validator = new CommentValidator();
$commentLike = $validator->checkIfLiked($comment->id, $user->id);
if (!$commentLike) {
$commentLike = new CommentLikeModel();
$commentLike->create([
'comment_id' => $comment->id,
'user_id' => $user->id,
]);
$this->incrLikeCount($comment);
} else {
if ($commentLike->deleted == 0) {
$commentLike->update(['deleted' => 1]);
$this->decrLikeCount($comment);
} else {
$commentLike->update(['deleted' => 0]);
$this->incrLikeCount($comment);
}
}
$this->incrUserDailyCommentLikeCount($user);
return $comment;
}
protected function incrLikeCount(CommentModel $comment)
{
$this->getPhEventsManager()->fire('commentCounter:incrLikeCount', $this, $comment);
}
protected function decrLikeCount(CommentModel $comment)
{
$this->getPhEventsManager()->fire('commentCounter:decrLikeCount', $this, $comment);
}
protected function incrUserDailyCommentLikeCount(UserModel $user)
{
$this->getPhEventsManager()->fire('userDailyCounter:incrCommentLikeCount', $this, $user);
}
/**
* @return EventsManager
*/
protected function getPhEventsManager()
{
return $this->getDI()->get('eventsManager');
}
}