TechTipCommentsController   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Test Coverage

Coverage 62.5%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
eloc 34
c 2
b 0
f 0
dl 0
loc 85
ccs 5
cts 8
cp 0.625
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A show() 0 9 1
A edit() 0 7 1
A index() 0 6 1
A destroy() 0 10 1
A update() 0 8 1
A store() 0 12 1
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 8
use App\Http\Requests\TechTips\TechTipCommentRequest;
15
use App\Http\Requests\TechTips\UpdateCommentRequest;
16 8
use Inertia\Inertia;
17 8
18
class TechTipCommentsController extends Controller
19
{
20 2
    /**
21
     * Index method will list all comments that have been flagged
22 2
     */
23 2
    public function index()
24
    {
25
        $this->authorize('manage', TechTip::class);
26
27 2
        return Inertia::render('TechTips/Comments/Index', [
28
            'flagged' => TechTipComment::where('flagged', true)->with('User')->get(),
29 2
        ]);
30
    }
31
32
    /**
33
     * Store a newly created Tech Tip Comment
34
     */
35
    public function store(TechTipCommentRequest $request)
36
    {
37
        $comment = TechTipComment::create([
38
            'tip_id'  => $request->tip_id,
39
            'user_id' => $request->user()->user_id,
40
            'comment' => $request->comment,
41
        ]);
42
43
        event(new TechTipCommentCreatedEvent($comment));
44
        return back()->with([
45
            'message' => 'Comment Created',
46
            'type'    => 'success',
47
        ]);
48
    }
49
50
    /**
51
     * Show Method will un-flag a comment
52
     */
53
    public function show($id)
54
    {
55
        $comment = TechTipComment::findOrFail($id);
56
        $this->authorize('manage', TechTip::class);
57
        $comment->update(['flagged' => false]);
58
59
        return back()->with([
60
            'message' => 'Comment has been unflagged',
61
            'type'    => 'warning',
62
        ]);
63
    }
64
65
    /**
66
     * Edit function will flag a Tech Tip as innapropriate
67
     */
68
    public function edit($id)
69
    {
70
        $comment = TechTipComment::find($id);
71
        $comment->update(['flagged' => true]);
72
73
        event(new TechTipCommentFlaggedEvent($comment));
74
        return back();
75
    }
76
77
    /**
78
     * Update a Tech Tip Comment
79
     */
80
    public function update(UpdateCommentRequest $request, $id)
81
    {
82
        $comment = TechTipComment::find($id);
83
        $comment->update($request->only(['comment']));
84
        $comment->save();
85
86
        event(new TechTipCommentUpdatedEvent($comment));
87
        return back();
88
    }
89
90
    /**
91
     * Delete the Tech Tip Comment
92
     */
93
    public function destroy($id)
94
    {
95
        $comment = TechTipComment::find($id);
96
        $this->authorize('delete', $comment);
97
        $comment->delete();
98
99
        event(new TechTipCommentDeletedEvent($comment));
100
        return back()->with([
101
            'message' => 'Comment Deleted',
102
            'type'    => 'success',
103
        ]);
104
    }
105
}
106