Passed
Push — 5.0.0 ( f10804...44b42e )
by Fèvre
15:04 queued 07:40
created

NotificationController::index()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 15
rs 9.9666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Xetaravel\Http\Controllers\User;
6
7
use Illuminate\Http\JsonResponse;
8
use Illuminate\Http\Request;
9
use Illuminate\Support\Facades\Auth;
10
use Illuminate\View\View;
11
use Xetaravel\Http\Controllers\Controller;
12
use Xetaravel\Models\Newsletter;
13
use Xetaravel\Models\User;
14
15
class NotificationController extends Controller
16
{
17
    /**
18
     * Constructor
19
     */
20
    public function __construct()
21
    {
22
        parent::__construct();
23
24
        $this->breadcrumbs->addCrumb(
25
            '<i class="fa-solid fa-user-tag mr-2"></i> Notifications',
26
            route('user.notification.index')
27
        );
28
    }
29
30
    /**
31
     * Show the notifications & newsletter.
32
     *
33
     * @return View
34
     */
35
    public function index(): View
36
    {
37
        $user = User::find(Auth::id());
38
39
        $notifications = $user->notifications()
40
            ->paginate(config('xetaravel.pagination.notification.notification_per_page'));
41
        $hasUnreadNotifications = $user->unreadNotifications->isNotEmpty();
42
43
        $breadcrumbs = $this->breadcrumbs;
44
45
        $newsletter = Newsletter::where('email', $user->email)->first();
46
47
        return view(
48
            'notification.index',
49
            compact('user', 'breadcrumbs', 'notifications', 'hasUnreadNotifications', 'newsletter')
50
        );
51
    }
52
53
    /**
54
     * Delete a notification by its ID.
55
     *
56
     * @param Request $request The current request.
57
     * @param string $slug The notification ID.
58
     *
59
     * @return JsonResponse
60
     */
61
    public function delete(Request $request, string $slug): JsonResponse
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

61
    public function delete(/** @scrutinizer ignore-unused */ Request $request, string $slug): JsonResponse

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
62
    {
63
        $user = Auth::user();
64
        $notification = $user->notifications()
65
            ->where('id', $slug)
66
            ->first();
67
68
        if ($notification) {
69
            $notification->delete();
70
        }
71
72
        return response()->json([
73
            'error' => false
74
        ]);
75
    }
76
77
    /**
78
     * Mark a notification as read.
79
     *
80
     * @param Request $request The current request.
81
     *
82
     * @return JsonResponse
83
     */
84
    public function markAsRead(Request $request): JsonResponse
85
    {
86
        $user = Auth::user();
87
        $notification = $user->notifications()
88
            ->where('id', $request->input('id'))
89
            ->first();
90
91
        if ($notification) {
92
            $notification->markAsRead();
93
        }
94
95
        return response()->json([
96
            'error' => false
97
        ]);
98
    }
99
100
    /**
101
     * Mark all notifications as read.
102
     *
103
     * @return JsonResponse
104
     */
105
    public function markAllAsRead(): JsonResponse
106
    {
107
        $user = Auth::user();
108
        $user->unreadNotifications->markAsRead();
0 ignored issues
show
Bug introduced by
Accessing unreadNotifications on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
109
110
        return response()->json([
111
            'error' => false
112
        ]);
113
    }
114
}
115