Passed
Push — dev5 ( 1fbf2b...8c60e2 )
by Ron
09:02
created

DashboardController::delNotification()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.1481

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 15
ccs 6
cts 9
cp 0.6667
crap 2.1481
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\TechTips;
6
use App\FileLinks;
7
use Carbon\Carbon;
8
use App\TechTipFavs;
9
use App\CustomerFavs;
10
use Illuminate\Support\Facades\Log;
11
use Illuminate\Support\Facades\Auth;
12
use Illuminate\Support\Facades\Route;
13
14
class DashboardController extends Controller
15
{
16 20
    public function __construct()
17
    {
18
        //  Only authorized users have access to these pages
19 20
        $this->middleware('auth');
20 20
    }
21
22
    //  Dashboard is the Logged In User home landing page
23 2
    public function index()
24
    {
25 2
        Log::debug('Route "' . Route::currentRouteName() . '" visited by ' . Auth::user()->full_name);
26
27
        //  Get Dashboard data
28 2
        $custFavs    = CustomerFavs::where('user_id', Auth::user()->user_id)
29
            ->with(array('Customers' => function($query){
30
                $query->select('cust_id', 'name');
31 2
            }))
32 2
            ->get();
33 2
        $tipFavs     = TechTipFavs::where('user_id', Auth::user()->user_id)
34
            ->with(array('TechTips' => function($query){
35
                $query->select('tip_id', 'subject');
36 2
            }))
37 2
            ->get();
38 2
        $tips30Days  = TechTips::where('created_at', '>', Carbon::now()->subDays(30))->count();
39 2
        $tipsTotal   = TechTips::all()->count();
40 2
        $activeLinks = FileLinks::where('user_id', Auth::user()->user_id)->where('expire', '>', Carbon::now())->count();
41 2
        $totalLinks  = FileLinks::where('user_id', Auth::user()->user_id)->count();
42
43
        //  Debug Data
44 2
        Log::debug('Dashboard - Customer Favorites for '.Auth::user()->full_name.': ', $custFavs->toArray());
45 2
        Log::debug('Dashboard - Tech Tip Favorites for '.Auth::user()->full_name.': ', $tipFavs->toArray());
46
47 2
        return view('dashboard', [
48 2
            'custFavs'    => $custFavs,
49 2
            'tipFavs'     => $tipFavs,
50 2
            'tips30'      => $tips30Days,
51 2
            'tipsAll'     => $tipsTotal,
52 2
            'activeLinks' => $activeLinks,
53 2
            'totalLinks'  => $totalLinks,
54
        ]);
55
    }
56
57
    //  Get the users notifications
58
    public function getNotifications()
59
    {
60
        //  Debug Data
61
        Log::debug('Route '.Route::currentRouteName().' visited by '.Auth::user()->full_name);
62
        Log::debug('Notifications for '.Auth::user()->full_name.':', Auth::user()->notifications->toArray());
63
64
        return Auth::user()->notifications;
65
    }
66
67
    //  Mark a notification as read
68 4
    public function markNotification($id)
69
    {
70 4
        Log::debug('Route ' . Route::currentRouteName() . ' visited by ' . Auth::user()->full_name);
71
72 4
        $notification = Auth::user()->notifications()->where('id', $id)->where('notifiable_id', Auth::user()->user_id)->first();
73 4
        if(!$notification)
74
        {
75 4
            Log::error('User '.Auth::user()->full_name.' tried to mark an invalid notification as read.  Notification ID: '.$id);
76 4
            return abort(404);
77
        }
78
        $notification->markAsRead();
79
80
        Log::info('User '.Auth::user()->full_name.' marked notification ID '.$id.' as read');
81
82
        return response()->json(['success' => true]);
83
    }
84
85
    //  Delte a user notification
86 4
    public function delNotification($id)
87
    {
88 4
        Log::debug('Route ' . Route::currentRouteName() . ' visited by ' . Auth::user()->full_name);
89
90 4
        $notification = Auth::user()->notifications()->where('id', $id)->where('notifiable_id', Auth::user()->user_id)->first();
91 4
        if(!$notification)
92
        {
93 4
            Log::error('User ' . Auth::user()->full_name . ' tried to delete an invalid notification.  Notification ID: ' . $id);
94 4
            return abort(404);
95
        }
96
97
        $notification->delete();
98
        Log::info('Notification ID - '.$id.' deleted for '.Auth::user()->full_name);
99
100
        return response()->json(['success' => true]);
101
    }
102
103
    //  About page
104
    public function about()
105
    {
106
        Log::debug('Route '.Route::currentRouteName().' visited by '.Auth::user()->full_name);
107
108
        return view('about', [
109
            'branch' => 'latest'
110
        ]);
111
    }
112
}
113