Notification::render()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Xetaravel\Livewire\User;
6
7
use Illuminate\Contracts\View\Factory;
8
use Illuminate\Contracts\View\View;
9
use Illuminate\Foundation\Application;
10
use Illuminate\Notifications\DatabaseNotificationCollection;
11
use Livewire\Component;
12
13
class Notification extends Component
14
{
15
    /**
16
     * All notifications from the user.
17
     *
18
     * @var DatabaseNotificationCollection
19
     */
20
    public DatabaseNotificationCollection $notifications;
21
22
    /**
23
     * Whatever the user has un-read notifications.
24
     *
25
     * @var bool
26
     */
27
    public bool $hasUnreadNotifications = false;
28
29
    /**
30
     * The number of un-red notifications.
31
     *
32
     * @var int
33
     */
34
    public int $unreadNotificationsCount = 0;
35
36
    /**
37
     * Used to refresh the notifications and the unread count.
38
     *
39
     * @return void
40
     */
41
    private function fetchData(): void
42
    {
43
        $this->notifications = auth()->user()->notifications;
44
45
        // Filter only the unread notifications
46
        $unreadNotifications = $this->notifications->filter(function ($notification) {
47
            return $notification->read_at === null;
48
        });
49
        $this->unreadNotificationsCount = $unreadNotifications->count();
50
51
        $this->hasUnreadNotifications = $this->unreadNotificationsCount > 0;
52
    }
53
54
    /**
55
     * The mount function.
56
     *
57
     * @return void
58
     */
59
    public function mount(): void
60
    {
61
        $this->fetchData();
62
    }
63
64
    public function render(): Factory|Application|View|\Illuminate\View\View
65
    {
66
        return view('livewire.user.notification');
67
    }
68
69
    /**
70
     * Remove a notification by its id.
71
     *
72
     * @param string $notificationId The id of the notification to remove.
73
     *
74
     * @return void
75
     */
76
    public function remove(string $notificationId): void
77
    {
78
        $notification = auth()->user()->notifications()
79
            ->where('id', $notificationId)
80
            ->first();
81
82
        // That means the notification id has been modified in front-end.
83
        if (!$notification) {
84
            return;
85
        }
86
        $notification->delete();
87
88
        $this->fetchData();
89
    }
90
91
    /**
92
     * Mark the notification as read by its id.
93
     *
94
     * @param string $notificationId The id of the notification to mark as read.
95
     *
96
     * @return void
97
     */
98
    public function markAsRead(string $notificationId): void
99
    {
100
        $notification = auth()->user()->notifications()
101
            ->where('id', $notificationId)
102
            ->first();
103
104
        // That means the notification id has been modified in front-end.
105
        if (!$notification) {
106
            return;
107
        }
108
109
        $notification->markAsRead();
110
111
        $this->fetchData();
112
    }
113
114
    /**
115
     * Mark all notifications from the user as read.
116
     *
117
     * @return void
118
     */
119
    public function markAllNotificationsAsRead(): void
120
    {
121
        auth()->user()->unreadNotifications->markAsRead();
122
123
        $this->notifications = auth()->user()->notifications;
124
        $this->unreadNotificationsCount = 0;
125
        $this->hasUnreadNotifications = false;
126
    }
127
}
128