Completed
Push — master ( bb59f2...5ee32e )
by Tony
11s
created

NotificationController   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 22.58%

Importance

Changes 10
Bugs 3 Features 2
Metric Value
wmc 9
c 10
b 3
f 2
lcom 1
cbo 5
dl 0
loc 68
ccs 7
cts 31
cp 0.2258
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 11 2
B update() 0 22 5
A create() 0 14 2
1
<?php
2
3
namespace App\Api\Controllers;
4
5
use App\Api\Transformers\NotificationTransformer;
6
use App\Models\Notification;
7
use Dingo\Api\Http;
8
use Dingo\Api\Routing\Helpers;
9
use Illuminate\Http\Request;
10
11
class NotificationController extends Controller
12
{
13
14
    use Helpers;
15
16
    /**
17
     * Display a listing of all notifications
18
     *
19
     * @param Request $request
20
     * @param null $type archive or null (all)
21
     * @return \Illuminate\Http\Response
22
     */
23 10
    public function index(Request $request, $type = null)
24
    {
25 10
        if ($type === 'archive')
26 10
        {
27 1
            $notifications = Notification::isArchived($request->user())->get();
0 ignored issues
show
Bug introduced by
The method get does only exist in Illuminate\Database\Query\Builder, but not in App\Models\Notification.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
28 1
        }
29
        else {
30 10
            $notifications = Notification::isUnread()->get();
31
        }
32 10
        return $this->response->collection($notifications, NotificationTransformer::class);
33
    }
34
35
    public function update(Request $request, $id, $action)
1 ignored issue
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
36
    {
37
        $notification = Notification::find($id);
38
        $enable = strpos($action, 'un') === false;
39
        if (!$enable) {
40
            $action = substr($action, 2);
41
        }
42
43
        if ($action == 'read') {
44
            $result = $notification->markRead($enable);
45
        }
46
        elseif ($action == 'sticky') {
47
            $result = $notification->markSticky(false);
48
        }
49
50
        if ($result === false) {
0 ignored issues
show
Bug introduced by
The variable $result does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
51
            return $this->response->errorInternal();
52
        }
53
        else {
54
            return $this->response->array(array('statusText' => 'OK'));
55
        }
56
    }
57
58
    /**
59
     * Create a new notification
60
     *
61
     * @return \Illuminate\Http\Response
62
     */
63
    public function create(Request $request)
64
    {
65
        $notification = new Notification;
66
        $notification->title = $request->title;
1 ignored issue
show
Bug introduced by
The property title does not seem to exist in Illuminate\Http\Request.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
67
        $notification->body = $request->body;
1 ignored issue
show
Bug introduced by
The property body does not seem to exist in Illuminate\Http\Request.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
68
        $notification->checksum = hash('sha512', $request->user()->user_id.'.LOCAL.'.$request->title);
69
        $notification->source = $request->user()->user_id;
70
        if ($notification->save()) {
71
            return $this->response->array(array('statusText' => 'OK'));
72
        }
73
        else {
74
            return $this->response->errorInternal();
75
        }
76
    }
77
78
}
79