Completed
Push — badges-system ( 892f9c...b1c87e )
by Fèvre
03:09
created

NotificationController::index()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 1
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 Xetaravel\Models\User;
8
9
class NotificationController extends Controller
10
{
11
    /**
12
     * Undocumented function
13
     *
14
     * @return void
15
     */
16
    public function index()
17
    {
18
        //
19
    }
20
21
    /**
22
     * Mark a notification as read.
23
     *
24
     * @return \Illuminate\Http\JsonResponse
25
     */
26
    public function markAsRead(Request $request): JsonResponse
27
    {
28
        $user = Auth::user();
29
        $notification = $user->notifications()
30
            ->where('id', $request->input('id'))
31
            ->first();
32
        
33
        if ($notification) {
34
            $notification->markAsRead();
35
        }
36
        
37
        return response()->json([
38
            'error' => false
39
        ]);
40
    }
41
42
    /**
43
     * Mark all notifications as read.
44
     *
45
     * @return \Illuminate\Http\JsonResponse
46
     */
47
    public function markAllAsRead(): JsonResponse
48
    {
49
        $user = Auth::user();
50
        $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...
51
        
52
        return response()->json([
53
            'error' => false
54
        ]);
55
    }
56
}
57