checkChapter($id); $user = $this->getLoginUser(); $validator = new UserDailyLimitValidator(); $validator->checkChapterVoteLimit($user); $chapterVoteRepo = new ChapterVoteRepo(); $chapterVote = $chapterVoteRepo->findChapterVote($chapter->id, $user->id); if (!$chapterVote) { $chapterVote = new ChapterVoteModel(); $chapterVote->chapter_id = $chapter->id; $chapterVote->user_id = $user->id; $chapterVote->type = ChapterVoteModel::TYPE_AGREE; $chapterVote->create(); $this->incrAgreeCount($chapter); } else { if ($chapterVote->type == ChapterVoteModel::TYPE_AGREE) { $chapterVote->type = ChapterVoteModel::TYPE_NONE; $this->decrAgreeCount($chapter); } elseif ($chapterVote->type == ChapterVoteModel::TYPE_OPPOSE) { $chapterVote->type = ChapterVoteModel::TYPE_AGREE; $this->incrAgreeCount($chapter); $this->decrOpposeCount($chapter); } elseif ($chapterVote->type == ChapterVoteModel::TYPE_NONE) { $chapterVote->type = ChapterVoteModel::TYPE_AGREE; $this->incrAgreeCount($chapter); } $chapterVote->update(); } $this->incrUserDailyChapterVoteCount($user); return $chapter; } public function oppose($id) { $chapter = $this->checkChapter($id); $user = $this->getLoginUser(); $validator = new UserDailyLimitValidator(); $validator->checkChapterVoteLimit($user); $chapterVoteRepo = new ChapterVoteRepo(); $chapterVote = $chapterVoteRepo->findChapterVote($chapter->id, $user->id); if (!$chapterVote) { $chapterVote = new ChapterVoteModel(); $chapterVote->chapter_id = $chapter->id; $chapterVote->user_id = $user->id; $chapterVote->type = ChapterVoteModel::TYPE_OPPOSE; $chapterVote->create(); $this->incrOpposeCount($chapter); } else { if ($chapterVote->type == ChapterVoteModel::TYPE_AGREE) { $chapterVote->type = ChapterVoteModel::TYPE_OPPOSE; $this->decrAgreeCount($chapter); $this->incrOpposeCount($chapter); } elseif ($chapterVote->type == ChapterVoteModel::TYPE_OPPOSE) { $chapterVote->type = ChapterVoteModel::TYPE_NONE; $this->decrOpposeCount($chapter); } elseif ($chapterVote->type == ChapterVoteModel::TYPE_NONE) { $chapterVote->type = ChapterVoteModel::TYPE_OPPOSE; $this->incrOpposeCount($chapter); } $chapterVote->update(); } $this->incrUserDailyChapterVoteCount($user); return $chapter; } protected function incrAgreeCount(ChapterModel $chapter) { $this->eventsManager->fire('chapterCounter:incrAgreeCount', $this, $chapter); } protected function decrAgreeCount(ChapterModel $chapter) { $this->eventsManager->fire('chapterCounter:decrAgreeCount', $this, $chapter); } protected function incrOpposeCount(ChapterModel $chapter) { $this->eventsManager->fire('chapterCounter:incrOpposeCount', $this, $chapter); } protected function decrOpposeCount(ChapterModel $chapter) { $this->eventsManager->fire('chapterCounter:decrOpposeCount', $this, $chapter); } protected function incrUserDailyChapterVoteCount(UserModel $user) { $this->eventsManager->fire('userDailyCounter:incrChapterVoteCount', $this, $user); } }