NotificationsController::read()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 17
rs 9.2
c 1
b 0
f 0
cc 4
eloc 12
nc 8
nop 1
1
<?php
2
3
namespace Hechoenlaravel\JarvisFoundation\Http\Controllers;
4
5
use Auth;
6
use Carbon\Carbon;
7
use Hechoenlaravel\JarvisFoundation\Notifications\SendAppNotification\Notification;
8
use Hechoenlaravel\JarvisFoundation\Notifications\SendAppNotification\Events\NotificationWasReaded;
9
use Hechoenlaravel\JarvisFoundation\Notifications\SendAppNotification\Events\NotificationWasOpened;
10
11
/**
12
 * Class NotificationsController
13
 * @package Hechoenlaravel\JarvisFoundation\Http\Controllers
14
 */
15
class NotificationsController extends Controller
16
{
17
    /**
18
     * Lists notifications
19
     * @return $this
20
     */
21
    public function index()
22
    {
23
        $notifications = Notification::byUser(Auth::user())->orderBy('created_at', 'DESC')->paginate(20);
24
        return view('jarvisPlatform::notifications.index')->with('notifications', $notifications);
0 ignored issues
show
Bug introduced by
The method with() does not seem to exist on object<BladeView>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
25
    }
26
27
    /**
28
     * Read a notification
29
     * @param $id
30
     * @return \Illuminate\Http\RedirectResponse
31
     */
32
    public function read($id)
33
    {
34
        $notification = Notification::findOrFail($id);
35
        if ($notification->user_id != Auth::user()->id) {
36
            abort(403);
37
        }
38
        if (empty($notification->readed_at)) {
39
            $notification->readed_at = Carbon::now();
40
            $notification->save();
41
            event(new NotificationWasReaded($notification));
42
        }
43
        event(new NotificationWasOpened($notification));
44
        if (!empty($notification->link)) {
45
            return redirect()->to($notification->link);
46
        }
47
        return redirect()->route('notifications.index');
48
    }
49
}
50