Comment   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 232
Duplicated Lines 0 %

Importance

Changes 6
Bugs 4 Features 2
Metric Value
eloc 108
c 6
b 4
f 2
dl 0
loc 232
rs 10
wmc 21

10 Methods

Rating   Name   Duplication   Size   Complexity  
A editComment() 0 9 1
A deleteComment() 0 7 1
A closeReportForm() 0 4 1
A reportComment() 0 47 5
A selectUser() 0 11 3
A render() 0 3 1
A getUsers() 0 7 2
A updatedIsEditing() 0 7 2
A showReportForm() 0 10 2
A postReply() 0 28 3
1
<?php
2
3
namespace Usamamuneerchaudhary\Commentify\Http\Livewire;
4
5
6
use Illuminate\Auth\Access\AuthorizationException;
7
use Illuminate\Contracts\View\Factory;
8
use Illuminate\Contracts\View\View;
9
use Illuminate\Foundation\Application;
10
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
11
use Illuminate\Support\Str;
12
use Livewire\Attributes\On;
13
use Livewire\Component;
14
use Usamamuneerchaudhary\Commentify\Events\CommentReported;
15
use Usamamuneerchaudhary\Commentify\Models\CommentReport;
16
use Usamamuneerchaudhary\Commentify\Models\User;
17
18
class Comment extends Component
19
{
20
    use AuthorizesRequests;
21
22
    public $comment;
23
24
    public $users = [];
25
26
    public $isReplying = false;
27
    public $hasReplies = false;
28
29
    public $showOptions = false;
30
31
    public $isEditing = false;
32
33
    public $isReporting = false;
34
35
    public $alreadyReported = false;
36
37
    public $reportState = [
38
        'reason' => '',
39
        'additional_details' => ''
40
    ];
41
42
    public $replyState = [
43
        'body' => ''
44
    ];
45
46
    public $editState = [
47
        'body' => ''
48
    ];
49
50
    protected $validationAttributes = [
51
        'replyState.body' => 'Reply',
52
        'editState.body' => 'Reply',
53
        'reportState.reason' => 'reason'
54
    ];
55
56
57
    /**
58
     * @param $isEditing
59
     * @return void
60
     */
61
    public function updatedIsEditing($isEditing): void
62
    {
63
        if (!$isEditing) {
64
            return;
65
        }
66
        $this->editState = [
67
            'body' => $this->comment->body
68
        ];
69
    }
70
71
    /**
72
     * @return void
73
     * @throws \Illuminate\Auth\Access\AuthorizationException
74
     */
75
    public function editComment(): void
76
    {
77
        $this->authorize('update', $this->comment);
78
        $this->validate([
79
            'editState.body' => 'required|min:2'
80
        ]);
81
        $this->comment->update($this->editState);
82
        $this->isEditing = false;
83
        $this->showOptions = false;
84
    }
85
86
    /**
87
     * @return void
88
     * @throws AuthorizationException
89
     */
90
    #[On('refresh')]
91
    public function deleteComment(): void
92
    {
93
        $this->authorize('destroy', $this->comment);
94
        $this->comment->delete();
95
        $this->showOptions = false;
96
        $this->dispatch('refresh');
97
    }
98
99
    /**
100
     * @return Factory|Application|View|\Illuminate\Contracts\Foundation\Application|null
101
     */
102
    /**
103
     * @return void
104
     */
105
    public function showReportForm(): void
106
    {
107
        if ($this->comment->isReportedByCurrentUser()) {
108
            $this->alreadyReported = true;
109
            $this->isReporting = true;
110
        } else {
111
            $this->alreadyReported = false;
112
            $this->isReporting = true;
113
        }
114
        $this->showOptions = false;
115
    }
116
117
    /**
118
     * @return Factory|Application|View|\Illuminate\Contracts\Foundation\Application|null
119
     */
120
    public function render(): \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\Contracts\View\View|\Illuminate\Contracts\Foundation\Application|null
121
    {
122
        return view('commentify::livewire.comment');
123
    }
124
125
    /**
126
     * @return void
127
     */
128
    #[On('refresh')]
129
    public function postReply(): void
130
    {
131
        if (config('commentify.read_only')) {
132
            session()->flash('message', __('commentify::commentify.comments.read_only_message'));
133
            session()->flash('alertType', 'warning');
134
            return;
135
        }
136
137
        $this->authorize('create', \Usamamuneerchaudhary\Commentify\Models\Comment::class);
138
139
        if (!$this->comment->isParent()) {
140
            return;
141
        }
142
        $this->validate([
143
            'replyState.body' => 'required'
144
        ]);
145
        $reply = $this->comment->children()->make($this->replyState);
146
        $reply->user()->associate(auth()->user());
147
        $reply->commentable()->associate($this->comment->commentable);
148
        $reply->save();
149
150
        $this->replyState = [
151
            'body' => ''
152
        ];
153
        $this->isReplying = false;
154
        $this->showOptions = false;
155
        $this->dispatch('refresh')->self();
156
    }
157
158
    /**
159
     * @param $userName
160
     * @return void
161
     */
162
    public function selectUser($userName): void
163
    {
164
        if ($this->replyState['body']) {
165
            $this->replyState['body'] = preg_replace('/@(\w+)$/', '@' . str_replace(' ', '_', Str::lower($userName)) . ' ',
166
                $this->replyState['body']);
167
//            $this->replyState['body'] =$userName;
168
            $this->users = [];
169
        } elseif ($this->editState['body']) {
170
            $this->editState['body'] = preg_replace('/@(\w+)$/', '@' . str_replace(' ', '_', Str::lower($userName)) . ' ',
171
                $this->editState['body']);
172
            $this->users = [];
173
        }
174
    }
175
176
177
    /**
178
     * @param $searchTerm
179
     * @return void
180
     */
181
    #[On('getUsers')]
182
    public function getUsers($searchTerm): void
183
    {
184
        if (!empty($searchTerm)) {
185
            $this->users = User::where('name', 'like', '%' . $searchTerm . '%')->take(5)->get();
186
        } else {
187
            $this->users = [];
188
        }
189
    }
190
191
    /**
192
     * @return void
193
     */
194
    public function reportComment(): void
195
    {
196
        if (!config('commentify.enable_reporting', true)) {
197
            return;
198
        }
199
200
        // Check if user has already reported this comment
201
        if ($this->comment->isReportedByCurrentUser()) {
202
            session()->flash('message', __('commentify::commentify.comments.already_reported'));
203
            session()->flash('alertType', 'warning');
204
            $this->isReporting = false;
205
            $this->showOptions = false;
206
            return;
207
        }
208
209
        $reportReasons = config('commentify.report_reasons', ['spam', 'inappropriate', 'offensive', 'other']);
210
        
211
        $this->validate([
212
            'reportState.reason' => 'required|in:' . implode(',', $reportReasons),
213
            'reportState.additional_details' => 'nullable|max:500'
214
        ]);
215
216
        $reason = $this->reportState['reason'];
217
        if (!empty($this->reportState['additional_details'])) {
218
            $reason .= ': ' . $this->reportState['additional_details'];
219
        }
220
221
        $report = CommentReport::create([
222
            'comment_id' => $this->comment->id,
223
            'user_id' => auth()->id(),
224
            'ip' => request()->ip(),
225
            'user_agent' => request()->userAgent(),
226
            'reason' => $reason,
227
            'status' => 'pending',
228
        ]);
229
230
        if (config('commentify.enable_notifications', false)) {
231
            event(new CommentReported($this->comment, $report));
0 ignored issues
show
Bug introduced by
It seems like $report can also be of type Illuminate\Database\Eloq...gHasThroughRelationship; however, parameter $report of Usamamuneerchaudhary\Com...Reported::__construct() does only seem to accept Usamamuneerchaudhary\Com...fy\Models\CommentReport, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

231
            event(new CommentReported($this->comment, /** @scrutinizer ignore-type */ $report));
Loading history...
232
        }
233
234
        $this->reportState = ['reason' => '', 'additional_details' => ''];
235
        $this->isReporting = false;
236
        $this->alreadyReported = false;
237
        $this->showOptions = false;
238
239
        session()->flash('message', __('commentify::commentify.comments.report_submitted'));
240
        session()->flash('alertType', 'success');
241
    }
242
243
    /**
244
     * @return void
245
     */
246
    public function closeReportForm(): void
247
    {
248
        $this->isReporting = false;
249
        $this->alreadyReported = false;
250
    }
251
252
}
253