1 | <?php |
||||
2 | declare(strict_types=1); |
||||
3 | |||||
4 | namespace Actuallymab\LaravelComment; |
||||
5 | |||||
6 | use Actuallymab\LaravelComment\Contracts\Commentable; |
||||
7 | use Actuallymab\LaravelComment\Models\Comment; |
||||
8 | use Illuminate\Database\Eloquent\Relations\MorphMany; |
||||
9 | |||||
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'); |
|||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
37 | } |
||||
38 | |||||
39 | 1 | public function hasCommentsOn(Commentable $commentable): bool |
|||
40 | { |
||||
41 | 1 | return $this->comments() |
|||
42 | 1 | ->where([ |
|||
43 | 1 | 'commentable_id' => $commentable->primaryId(), |
|||
44 | 1 | 'commentable_type' => get_class($commentable), |
|||
45 | ]) |
||||
46 | 1 | ->exists(); |
|||
47 | } |
||||
48 | |||||
49 | 13 | private function primaryId(): string |
|||
50 | { |
||||
51 | 13 | return (string)$this->getAttribute($this->primaryKey); |
|||
0 ignored issues
–
show
It seems like
getAttribute() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
52 | } |
||||
53 | } |
||||
54 |