Skip to content

Commit

Permalink
Fixed comment trait (added config path to models) & now votes model i…
Browse files Browse the repository at this point in the history
…s configurable
  • Loading branch information
iooe committed Jan 26, 2022
1 parent 06ec229 commit 59360d8
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 21 deletions.
9 changes: 2 additions & 7 deletions config/comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,9 @@
return [
// The model which creates the comments aka the User model
'models' => [
/**
* Commenter model
*/
'commenter' => \App\User::class,
/**
* Comment model
*/
'comment' => \App\Comment::class
'comment' => \App\Comment::class,
'votes' => \tizis\laraComments\Entity\CommentVotes::class,
],
'ui' => 'bootstrap4',
'purifier' => [
Expand Down
9 changes: 9 additions & 0 deletions src/Contracts/Vote.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace tizis\laraComments\Contracts;

interface Vote
{
public function commenter();
public function updateCommenterVote($updatedVote):void;
}
4 changes: 2 additions & 2 deletions src/Entity/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function votesCount(): int
*/
public function votes(): \Illuminate\Database\Eloquent\Relations\HasMany
{
return $this->hasMany(CommentVotes::class, 'comment_id');
return $this->hasMany(config('comments.models.votes'), 'comment_id');
}

/**
Expand All @@ -114,7 +114,7 @@ public function votes(): \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function addNewVoteIntoRatingRecords($commenterId, $vote)
{
$this->votes()->save(new CommentVotes([
$this->votes()->save(new (config('comments.models.votes'))([
'commenter_id' => $commenterId,
'commenter_vote' => $vote
]
Expand Down
3 changes: 2 additions & 1 deletion src/Entity/CommentVotes.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
namespace tizis\laraComments\Entity;

use Illuminate\Database\Eloquent\Model;
use tizis\laraComments\Contracts\Vote;

class CommentVotes extends Model
class CommentVotes extends Model implements Vote
{

protected $fillable = ['commenter_id', 'commenter_vote'];
Expand Down
9 changes: 3 additions & 6 deletions src/Traits/Commenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

namespace tizis\laraComments\Traits;

use tizis\laraComments\Entity\Comment;
use tizis\laraComments\Entity\CommentVotes;

/**
* Add this trait to your User model so
* that you can retrieve the comments for a user.
Expand All @@ -17,7 +14,7 @@ trait Commenter
*/
public function comments(): \Illuminate\Database\Eloquent\Relations\HasMany
{
return $this->hasMany(Comment::class, 'commenter_id');
return $this->hasMany(config('comments.models.comment'), 'commenter_id');
}

/**
Expand All @@ -28,7 +25,7 @@ public function comments(): \Illuminate\Database\Eloquent\Relations\HasMany
public function commentsWithChildrenAndCommenter(): \Illuminate\Database\Eloquent\Relations\HasMany
{
// 'allChildrenWithCommenter.commenter' needs for eager loading of first level Comment::class
return $this->hasMany(Comment::class, 'commenter_id')
return $this->hasMany(config('comments.models.comment'), 'commenter_id')
->with('allChildrenWithCommenter', 'allChildrenWithCommenter.commenter', 'commenter');
}

Expand All @@ -39,6 +36,6 @@ public function commentsWithChildrenAndCommenter(): \Illuminate\Database\Eloquen
*/
public function commentsVotes(): \Illuminate\Database\Eloquent\Relations\HasMany
{
return $this->hasMany(CommentVotes::class, 'commenter_id');
return $this->hasMany(config('comments.models.votes'), 'commenter_id');
}
}
10 changes: 5 additions & 5 deletions src/UseCases/VoteService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use DB;
use tizis\laraComments\Contracts\Comment as CommentInterface;
use tizis\laraComments\Entity\CommentVotes;
use tizis\laraComments\Contracts\Vote;

class VoteService
{
Expand Down Expand Up @@ -44,19 +44,19 @@ private function isUselessValue($oldVote, int $newVote): bool
}

/**
* @param CommentVotes $vote
* @param Vote $vote
* @throws \Exception
*/
private function remove(CommentVotes $vote): void
private function remove(Vote $vote): void
{
$vote->delete();
}

/**
* @param CommentVotes $vote
* @param Vote $vote
* @param int $updatedVote
*/
private function update(CommentVotes $vote, int $updatedVote): void
private function update(Vote $vote, int $updatedVote): void
{
$vote->updateCommenterVote($updatedVote);
}
Expand Down

0 comments on commit 59360d8

Please sign in to comment.