Test Failed
Push — dev6 ( 5da90e...83e762 )
by Ron
17:49
created

TechTipCommentsController::index()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace App\Http\Controllers\TechTips;
4
5
use Illuminate\Http\Request;
6
use App\Http\Controllers\Controller;
7
8
use App\Models\TechTip;
9
use App\Models\TechTipComment;
10
use App\Events\TechTips\TechTipCommentCreatedEvent;
11
use App\Events\TechTips\TechTipCommentDeletedEvent;
12
use App\Events\TechTips\TechTipCommentFlaggedEvent;
13
use App\Events\TechTips\TechTipCommentUpdatedEvent;
14
use App\Http\Requests\TechTips\TechTipCommentRequest;
15
use App\Http\Requests\TechTips\UpdateCommentRequest;
16
use Inertia\Inertia;
17
18
class TechTipCommentsController extends Controller
19
{
20
    /**
21
     * Index method will list all comments that have been flagged
22
     */
23
    public function index()
24
    {
25
        return Inertia::render('TechTips/Comments/Index', [
26
            'flagged' => TechTipComment::where('flagged', true)->with('User')->get(),
27
        ]);
28
    }
29
30
    /**
31
     * Store a newly created Tech Tip Comment
32
     */
33
    public function store(TechTipCommentRequest $request)
34
    {
35
        $comment = TechTipComment::create([
36
            'tip_id'  => $request->tip_id,
37
            'user_id' => $request->user()->user_id,
38
            'comment' => $request->comment,
39
        ]);
40
41
        event(new TechTipCommentCreatedEvent($comment));
42
        return back()->with([
43
            'message' => 'Comment Created',
44
            'type'    => 'success',
45
        ]);
46
    }
47
48
    /**
49
     * Show Method will un-flag a comment
50
     */
51
    public function show($id)
52
    {
53
        $comment = TechTipComment::findOrFail($id);
54
        $this->authorize('manage', TechTip::class);
55
        $comment->update(['flagged' => false]);
56
57
        return back()->with([
58
            'message' => 'Comment has been unflagged',
59
            'type'    => 'warning',
60
        ]);
61
    }
62
63
    /**
64
     * Edit function will flag a Tech Tip as innapropriate
65
     */
66
    public function edit($id)
67
    {
68
        $comment = TechTipComment::find($id);
69
        $comment->update(['flagged' => true]);
70
71
        event(new TechTipCommentFlaggedEvent($comment));
1 ignored issue
show
Bug introduced by
It seems like $comment can also be of type null; however, parameter $comment of App\Events\TechTips\Tech...gedEvent::__construct() does only seem to accept App\Models\TechTipComment, 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

71
        event(new TechTipCommentFlaggedEvent(/** @scrutinizer ignore-type */ $comment));
Loading history...
72
        return back();
73
    }
74
75
    /**
76
     * Update a Tech Tip Comment
77
     */
78
    public function update(UpdateCommentRequest $request, $id)
79
    {
80
        $comment = TechTipComment::find($id);
81
        $comment->update($request->only(['comment']));
82
        $comment->save();
83
84
        event(new TechTipCommentUpdatedEvent($comment));
1 ignored issue
show
Bug introduced by
It seems like $comment can also be of type null; however, parameter $comment of App\Events\TechTips\Tech...tedEvent::__construct() does only seem to accept App\Models\TechTipComment, 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

84
        event(new TechTipCommentUpdatedEvent(/** @scrutinizer ignore-type */ $comment));
Loading history...
85
        return back();
86
    }
87
88
    /**
89
     * Delete the Tech Tip Comment
90
     */
91
    public function destroy($id)
92
    {
93
        $comment = TechTipComment::find($id);
94
        $this->authorize('delete', $comment);
95
        $comment->delete();
96
97
        event(new TechTipCommentDeletedEvent($comment));
1 ignored issue
show
Bug introduced by
It seems like $comment can also be of type null; however, parameter $comment of App\Events\TechTips\Tech...tedEvent::__construct() does only seem to accept App\Models\TechTipComment, 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

97
        event(new TechTipCommentDeletedEvent(/** @scrutinizer ignore-type */ $comment));
Loading history...
98
        return back()->with([
99
            'message' => 'Comment Deleted',
100
            'type'    => 'success',
101
        ]);
102
    }
103
}
104