Comments   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 4
Bugs 1 Features 1
Metric Value
eloc 38
c 4
b 1
f 1
dl 0
loc 76
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A mount() 0 3 1
A postComment() 0 28 2
A render() 0 10 1
1
<?php
2
3
namespace Usamamuneerchaudhary\Commentify\Http\Livewire;
4
5
use Illuminate\Auth\Access\AuthorizationException;
6
use Illuminate\Contracts\View\Factory;
7
use Illuminate\Contracts\View\View;
8
use Illuminate\Foundation\Application;
9
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
10
use Livewire\Attributes\On;
11
use Livewire\Component;
12
use Livewire\WithPagination;
13
use Illuminate\Database\Eloquent\Model;
14
15
class Comments extends Component
16
{
17
    use WithPagination, AuthorizesRequests;
18
19
    public Model $model;
20
21
    public $users = [];
22
23
    public $showDropdown = false;
24
25
    protected $numberOfPaginatorsRendered = [];
26
27
    public $newCommentState = [
28
        'body' => ''
29
    ];
30
31
    protected $listeners = [
32
        'refresh' => '$refresh'
33
    ];
34
35
    protected $validationAttributes = [
36
        'newCommentState.body' => 'comment'
37
    ];
38
39
    public function mount(Model $model)
40
    {
41
        $this->model = $model;
42
    }
43
44
    /**
45
     * @return Factory|Application|View|\Illuminate\Contracts\Foundation\Application|null
46
     */
47
    public function render(): \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\Contracts\View\View|\Illuminate\Contracts\Foundation\Application|null
48
    {
49
        $comments = $this->model
50
            ->comments()
51
            ->with('user', 'children.user', 'children.children')
52
            ->parent()
53
            ->latest()
54
            ->paginate(config('commentify.pagination_count', 10));
55
        return view('commentify::livewire.comments', [
56
            'comments' => $comments
57
        ]);
58
    }
59
60
    /**
61
     * @return void
62
     */
63
    #[On('refresh')]
64
    public function postComment(): void
65
    {
66
        if (config('commentify.read_only')) {
67
            session()->flash('message', __('commentify::commentify.comments.read_only_message'));
68
            session()->flash('alertType', 'warning');
69
            return;
70
        }
71
72
        // Authorize using the CommentPolicy@create method
73
        $this->authorize('create', \Usamamuneerchaudhary\Commentify\Models\Comment::class);
74
75
        $this->validate([
76
            'newCommentState.body' => 'required'
77
        ]);
78
79
        $comment = $this->model->comments()->make($this->newCommentState);
80
        $comment->user()->associate(auth()->user());
81
        $comment->save();
82
83
        $this->newCommentState = [
84
            'body' => ''
85
        ];
86
        $this->users = [];
87
        $this->showDropdown = false;
88
89
        $this->resetPage();
90
        session()->flash('message', 'Comment Posted Successfully!');
91
    }
92
}
93