Like::mount()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 1
b 0
f 1
1
<?php
2
3
namespace Usamamuneerchaudhary\Commentify\Http\Livewire;
4
5
6
use Illuminate\Contracts\View\Factory;
7
use Illuminate\Contracts\View\View;
8
use Illuminate\Foundation\Application;
9
use Livewire\Component;
10
use Usamamuneerchaudhary\Commentify\Events\CommentLiked;
11
12
class Like extends Component
13
{
14
15
    public $comment;
16
    public $count;
17
18
19
    public function mount(\Usamamuneerchaudhary\Commentify\Models\Comment $comment): void
20
    {
21
        $this->comment = $comment;
22
        $this->count = $comment->likes_count;
0 ignored issues
show
Bug introduced by
The property likes_count does not seem to exist on Usamamuneerchaudhary\Commentify\Models\Comment. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
23
    }
24
25
    public function like(): void
26
    {
27
        $ip = request()->ip();
28
        $userAgent = request()->userAgent();
29
        if ($this->comment->isLiked()) {
30
            $this->comment->removeLike();
31
32
            $this->count--;
33
        } elseif (auth()->user()) {
34
            $this->comment->likes()->create([
35
                'user_id' => auth()->id(),
36
            ]);
37
38
            $this->count++;
39
40
            if (config('commentify.enable_notifications', false)) {
41
                event(new CommentLiked($this->comment, auth()->id()));
42
            }
43
        } elseif ($ip && $userAgent) {
44
            $this->comment->likes()->create([
45
                'ip' => $ip,
46
                'user_agent' => $userAgent,
47
            ]);
48
49
            $this->count++;
50
        }
51
    }
52
53
    /**
54
     * @return Factory|Application|View|\Illuminate\Contracts\Foundation\Application|null
55
     */
56
    public function render(
57
    ): \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\Contracts\View\View|\Illuminate\Contracts\Foundation\Application|null
58
    {
59
        return view('commentify::livewire.like');
60
    }
61
62
}
63