NotificationController::delete()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 1
dl 0
loc 16
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Xetaravel\Http\Controllers\User;
6
7
use Illuminate\Http\RedirectResponse;
8
use Illuminate\Support\Facades\Auth;
9
use Illuminate\View\View;
10
use Xetaravel\Http\Controllers\Controller;
11
use Xetaravel\Models\Newsletter;
12
use Xetaravel\Models\User;
13
14
class NotificationController extends Controller
15
{
16
    /**
17
     * Constructor
18
     */
19
    public function __construct()
20
    {
21
        parent::__construct();
22
23
        $this->breadcrumbs->addCrumb(
24
            '<svg class="inline w-5 h-5 mr-2" fill="currentColor" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 512"><path d="M224 256A128 128 0 1 0 224 0a128 128 0 1 0 0 256zm-45.7 48C79.8 304 0 383.8 0 482.3C0 498.7 13.3 512 29.7 512l388.6 0c10 0 18.8-4.9 24.2-12.5l-99.2-99.2c-14.9-14.9-23.3-35.1-23.3-56.1l0-33c-15.9-4.7-32.8-7.2-50.3-7.2l-91.4 0zM384 224c-17.7 0-32 14.3-32 32l0 82.7c0 17 6.7 33.3 18.7 45.3L478.1 491.3c18.7 18.7 49.1 18.7 67.9 0l73.4-73.4c18.7-18.7 18.7-49.1 0-67.9L512 242.7c-12-12-28.3-18.7-45.3-18.7L384 224zm24 80a24 24 0 1 1 48 0 24 24 0 1 1 -48 0z"></path></svg>
25
                          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 string $slug The notification ID.
57
     *
58
     * @return RedirectResponse
59
     */
60
    public function delete(string $slug)
61
    {
62
        $user = Auth::user();
63
        $notification = $user->notifications()
64
            ->where('id', $slug)
65
            ->first();
66
67
        if ($notification) {
68
            $notification->delete();
69
70
            return back()
71
                ->success('The notification has been deleted.');
72
        }
73
74
        return back()
75
            ->error('An error occurred while deleting the notification.');
76
    }
77
}
78