| Total Complexity | 8 |
| Total Lines | 42 |
| Duplicated Lines | 0 % |
| Coverage | 90.91% |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 10 | trait CanComment |
||
| 11 | { |
||
| 12 | 13 | public function comment(Commentable $commentable, string $commentText = '', int $rate = 0): Comment |
|
| 13 | { |
||
| 14 | 13 | $commentModel = config('comment.model'); |
|
| 15 | |||
| 16 | 13 | $comment = new $commentModel([ |
|
| 17 | 13 | 'comment' => $commentText, |
|
| 18 | 13 | 'rate' => $commentable->canBeRated() ? $rate : null, |
|
| 19 | 13 | 'approved' => $commentable->mustBeApproved() && !$this->canCommentWithoutApprove() ? false : true, |
|
| 20 | 13 | 'commented_id' => $this->primaryId(), |
|
| 21 | 13 | 'commented_type' => get_class(), |
|
| 22 | ]); |
||
| 23 | |||
| 24 | 13 | $commentable->comments()->save($comment); |
|
| 25 | |||
| 26 | 13 | return $comment; |
|
| 27 | } |
||
| 28 | |||
| 29 | public function canCommentWithoutApprove(): bool |
||
| 30 | { |
||
| 31 | return false; |
||
| 32 | } |
||
| 33 | |||
| 34 | 5 | public function comments(): MorphMany |
|
| 35 | { |
||
| 36 | 5 | return $this->morphMany(config('comment.model'), 'commented'); |
|
|
|
|||
| 37 | } |
||
| 38 | |||
| 39 | 1 | public function hasCommentsOn(Commentable $commentable): bool |
|
| 47 | } |
||
| 48 | |||
| 49 | 13 | private function primaryId(): string |
|
| 52 | } |
||
| 53 | } |
||
| 54 |