Passed
Push — dev5 ( 1c1697...6a3ae7 )
by Ron
08:21
created

TechTipCommentsController::destroy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 8
ccs 0
cts 5
cp 0
crap 2
rs 10
1
<?php
2
3
namespace App\Http\Controllers\TechTips;
4
5
use App\User;
6
use App\TechTips;
7
use App\TechTipComments;
8
use Illuminate\Http\Request;
9
use Illuminate\Support\Facades\Log;
10
use Illuminate\Support\Facades\Auth;
11
use App\Http\Controllers\Controller;
12
use Illuminate\Support\Facades\Route;
13
use App\Notifications\NewTechTipComment;
14
use Illuminate\Support\Facades\Notification;
15
16
class TechTipCommentsController extends Controller
17
{
18 8
    public function __construct()
19
    {
20 8
        $this->middleware('auth');
21 8
    }
22
23
    //  Add a new Tech Tip Comment
24 2
    public function store(Request $request)
25
    {
26 2
        Log::debug('Route ' . Route::currentRouteName() . ' visited by ' . Auth::user()->full_name.'. Submitted Data - ', $request->toArray());
27
28 2
        $request->validate([
29 2
            'comment' => 'required',
30
            'tipID' => 'required',
31
        ]);
32
33 2
        TechTipComments::create([
34 2
            'tip_id' => $request->tipID,
35 2
            'user_id' => Auth::user()->user_id,
36 2
            'comment' => $request->comment
37
        ]);
38
39 2
        $ownerID = TechTips::find($request->tipID)->user_id;
40 2
        $owner = User::find($ownerID);
41
42 2
        Notification::send($owner, new NewTechTipComment(Auth::user()->full_name, $request->tipID));
43
44 2
        Log::info('User '.Auth::user()->full_name.' left a comment on Tech Tip '.$request->tipID);
45 2
        return response()->json(['success' => true]);
46
    }
47
48
    //  Retrieve the comments for a tech tip
49 2
    public function show($id)
50
    {
51 2
        Log::debug('Route ' . Route::currentRouteName() . ' visited by ' . Auth::user()->full_name);
52
53 2
        return TechTipComments::where('tip_id', $id)->with('user')->get();
54
    }
55
56
    //  Delete a comment
57
    public function destroy($id)
58
    {
59
        Log::debug('Route ' . Route::currentRouteName() . ' visited by ' . Auth::user()->full_name);
60
61
        TechTipComments::find($id)->delete();
62
63
        Log::warning('A Tech Tip Comment (id# '.$id.') was deleted by '.Auth::user()->full_name);
64
        return response()->json(['success' => true]);
65
    }
66
}
67