Completed
Push — badges-system ( 750538...a7f9ad )
by Fèvre
02:19
created

NotificationController::index()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 12
nc 1
nop 0
1
<?php
2
namespace Xetaravel\Http\Controllers;
3
4
use Illuminate\Http\JsonResponse;
5
use Illuminate\Http\Request;
6
use Illuminate\Support\Facades\Auth;
7
use Illuminate\View\View;
8
use Xetaravel\Models\User;
9
10
class NotificationController extends Controller
11
{
12
    /**
13
     * Constructor
14
     */
15
    public function __construct()
16
    {
17
        parent::__construct();
18
19
        $this->breadcrumbs->addCrumb('Notifications', route('users_notification_index'));
0 ignored issues
show
Bug introduced by
The property breadcrumbs does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
20
    }
21
22
    /**
23
     * Show the notifications.
24
     *
25
     * @return \Illuminate\View\View
26
     */
27
    public function index(): View
28
    {
29
        $user = User::find(Auth::user()->id);
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
30
31
        $this->breadcrumbs->setCssClasses('breadcrumb');
32
33
        $notifications = $user->notifications()
34
            ->paginate(config('xetaravel.pagination.notification.notification_per_page'));
35
        $hasUnreadNotifications = $user->unreadNotifications->isNotEmpty();
36
37
        return view(
38
            'notification.index',
39
            [
40
                'user' => $user,
41
                'breadcrumbs' => $this->breadcrumbs,
42
                'notifications' => $notifications,
43
                'hasUnreadNotifications' => $hasUnreadNotifications
44
            ]
45
        );
46
    }
47
48
    /**
49
     * Delete a notification by its id.
50
     *
51
     * @param \Illuminate\Http\Request $request The current request.
52
     *
53
     * @return \Illuminate\Http\JsonResponse
54
     */
55 View Code Duplication
    public function delete(Request $request): JsonResponse
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
    {
57
        $user = Auth::user();
58
        $notification = $user->notifications()
59
            ->where('id', $request->input('id'))
60
            ->first();
61
        
62
        if ($notification) {
63
            $notification->delete();
64
        }
65
        
66
        return response()->json([
67
            'error' => false
68
        ]);
69
    }
70
71
    /**
72
     * Mark a notification as read.
73
     *
74
     * @param \Illuminate\Http\Request $request The current request.
75
     *
76
     * @return \Illuminate\Http\JsonResponse
77
     */
78 View Code Duplication
    public function markAsRead(Request $request): JsonResponse
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79
    {
80
        $user = Auth::user();
81
        $notification = $user->notifications()
82
            ->where('id', $request->input('id'))
83
            ->first();
84
        
85
        if ($notification) {
86
            $notification->markAsRead();
87
        }
88
        
89
        return response()->json([
90
            'error' => false
91
        ]);
92
    }
93
94
    /**
95
     * Mark all notifications as read.
96
     *
97
     * @return \Illuminate\Http\JsonResponse
98
     */
99
    public function markAllAsRead(): JsonResponse
100
    {
101
        $user = Auth::user();
102
        $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?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
103
        
104
        return response()->json([
105
            'error' => false
106
        ]);
107
    }
108
}
109