Completed
Pull Request — master (#10)
by Fèvre
03:30
created

NotificationController::markAllAsRead()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
namespace Xetaravel\Http\Controllers;
3
4
use Illuminate\Http\JsonResponse;
5
use Illuminate\Support\Facades\Auth;
6
use Xetaravel\Models\User;
7
8
class NotificationController extends Controller
9
{
10
    /**
11
     * Undocumented function
12
     *
13
     * @return void
14
     */
15
    public function index()
16
    {
17
        //
18
    }
19
20
    /**
21
     * Mark a notification as read.
22
     *
23
     * @return \Illuminate\Http\JsonResponse
24
     */
25
    public function markAsRead(): JsonResponse
26
    {
27
        $user = Auth::user();
28
        $notification = $user->notifications()
29
            ->where('id', $request->input('id'))
0 ignored issues
show
Bug introduced by
The variable $request does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
30
            ->first();
31
        
32
        if ($notification) {
33
            $notification->markAsRead();
34
        }
35
        
36
        return response()->json([
37
            'error' => false
38
        ]);
39
    }
40
41
    /**
42
     * Mark all notifications as read.
43
     *
44
     * @return \Illuminate\Http\JsonResponse
45
     */
46
    public function markAllAsRead(): JsonResponse
47
    {
48
        $user = Auth::user();
49
        $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...
50
        
51
        return response()->json([
52
            'error' => false
53
        ]);
54
    }
55
}
56