Comment::commentable()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 1
1
<?php
2
3
namespace Usamamuneerchaudhary\Commentify\Models;
4
5
use Illuminate\Database\Eloquent\Factories\HasFactory;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Database\Eloquent\SoftDeletes;
8
use Usamamuneerchaudhary\Commentify\Database\Factories\CommentFactory;
9
use Usamamuneerchaudhary\Commentify\Models\Presenters\CommentPresenter;
10
use Usamamuneerchaudhary\Commentify\Scopes\CommentScopes;
11
use Usamamuneerchaudhary\Commentify\Scopes\HasLikes;
12
13
class Comment extends Model
14
{
15
    use CommentScopes, HasFactory, HasLikes, SoftDeletes;
0 ignored issues
show
introduced by
The trait Usamamuneerchaudhary\Commentify\Scopes\HasLikes requires some properties which are not provided by Usamamuneerchaudhary\Commentify\Models\Comment: $likes, $ip, $user_agent
Loading history...
Bug introduced by
The trait Illuminate\Database\Eloquent\Factories\HasFactory requires the property $factoryClass which is not provided by Usamamuneerchaudhary\Commentify\Models\Comment.
Loading history...
16
17
    /**
18
     * @var string
19
     */
20
    protected $table = 'comments';
21
22
    /**
23
     * @var string[]
24
     */
25
    protected $fillable = ['body', 'is_approved'];
26
27
    /**
28
     * @var string[]
29
     */
30
    protected $casts = [
31
        'is_approved' => 'boolean',
32
    ];
33
34
    protected $withCount = [
35
        'likes',
36
    ];
37
38
    public function presenter(): CommentPresenter
39
    {
40
        return new CommentPresenter($this);
41
    }
42
43
    public function isParent(): bool
44
    {
45
        return is_null($this->parent_id);
0 ignored issues
show
Bug introduced by
The property parent_id does not exist on Usamamuneerchaudhary\Commentify\Models\Comment. Did you mean parent?
Loading history...
46
    }
47
48
    public function user(): \Illuminate\Database\Eloquent\Relations\BelongsTo
49
    {
50
        return $this->belongsTo(User::class);
51
    }
52
53
    public function parent(): \Illuminate\Database\Eloquent\Relations\BelongsTo
54
    {
55
        return $this->belongsTo(Comment::class, 'parent_id');
56
    }
57
58
    public function children(): \Illuminate\Database\Eloquent\Relations\HasMany
59
    {
60
        return $this->hasMany(Comment::class, 'parent_id')->oldest();
61
    }
62
63
    public function commentable(): \Illuminate\Database\Eloquent\Relations\MorphTo
64
    {
65
        return $this->morphTo();
66
    }
67
68
    public function reports(): \Illuminate\Database\Eloquent\Relations\HasMany
69
    {
70
        return $this->hasMany(CommentReport::class);
71
    }
72
73
    /**
74
     * Check if the current user/IP has already reported this comment
75
     */
76
    public function isReportedByCurrentUser(): bool
77
    {
78
        $query = $this->reports();
79
80
        if (auth()->check()) {
81
            return $query->where('user_id', auth()->id())->exists();
82
        }
83
84
        $ip = request()->ip();
85
        $userAgent = request()->userAgent();
86
87
        if ($ip && $userAgent) {
88
            return $query->whereNull('user_id')
89
                ->where('ip', $ip)
90
                ->where('user_agent', $userAgent)
91
                ->exists();
92
        }
93
94
        return false;
95
    }
96
97
    /**
98
     * Check if the comment is approved
99
     */
100
    public function isApproved(): bool
101
    {
102
        return $this->is_approved === true;
103
    }
104
105
    /**
106
     * Check if the comment is pending approval
107
     */
108
    public function isPending(): bool
109
    {
110
        return $this->is_approved === false;
111
    }
112
113
    protected static function newFactory(): CommentFactory
114
    {
115
        return CommentFactory::new();
116
    }
117
}
118