Like::like()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 5
eloc 14
c 1
b 0
f 1
nc 4
nop 0
dl 0
loc 21
rs 9.4888
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
11
class Like extends Component
12
{
13
14
    public $comment;
15
    public $count;
16
17
18
    public function mount(\Usamamuneerchaudhary\Commentify\Models\Comment $comment): void
19
    {
20
        $this->comment = $comment;
21
        $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...
22
    }
23
24
    public function like(): void
25
    {
26
        $ip = request()->ip();
27
        $userAgent = request()->userAgent();
28
        if ($this->comment->isLiked()) {
29
            $this->comment->removeLike();
30
31
            $this->count--;
32
        } elseif (auth()->user()) {
33
            $this->comment->likes()->create([
34
                'user_id' => auth()->id(),
35
            ]);
36
37
            $this->count++;
38
        } elseif ($ip && $userAgent) {
39
            $this->comment->likes()->create([
40
                'ip' => $ip,
41
                'user_agent' => $userAgent,
42
            ]);
43
44
            $this->count++;
45
        }
46
    }
47
48
    /**
49
     * @return Factory|Application|View|\Illuminate\Contracts\Foundation\Application|null
50
     */
51
    public function render(
52
    ): \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\Contracts\View\View|\Illuminate\Contracts\Foundation\Application|null
53
    {
54
        return view('commentify::livewire.like');
55
    }
56
57
}
58