checkComment($id); $user = $this->getLoginUser(); $validator = new UserLimitValidator(); $validator->checkDailyCommentLikeLimit($user); $likeRepo = new CommentLikeRepo(); $commentLike = $likeRepo->findCommentLike($comment->id, $user->id); if (!$commentLike) { $action = 'do'; $commentLike = new CommentLikeModel(); $commentLike->comment_id = $comment->id; $commentLike->user_id = $user->id; $commentLike->create(); $this->incrCommentLikeCount($comment); } else { $action = 'undo'; $commentLike->delete(); $this->decrCommentLikeCount($comment); } $this->incrUserDailyCommentLikeCount($user); return [ 'action' => $action, 'count' => $comment->like_count, ]; } protected function incrCommentLikeCount(CommentModel $comment) { $comment->like_count += 1; $comment->update(); } protected function decrCommentLikeCount(CommentModel $comment) { if ($comment->like_count > 0) { $comment->like_count -= 1; $comment->update(); } } protected function incrUserDailyCommentLikeCount(UserModel $user) { $this->eventsManager->fire('UserDailyCounter:incrCommentLikeCount', $this, $user); } }