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

TechTipCommentsController   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Test Coverage

Coverage 79.17%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 49
ccs 19
cts 24
cp 0.7917
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A show() 0 5 1
A destroy() 0 8 1
A __construct() 0 3 1
A store() 0 22 1
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