Completed
Pull Request — develop (#38)
by Neil
07:20
created

NotificationController::index()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 14
rs 9.4285
cc 3
eloc 7
nc 4
nop 2
1
<?php
2
3
namespace App\Api\Controllers;
4
5
use Dingo\Api\Http;
6
use Dingo\Api\Routing\Helpers;
7
use App\User;
8
use App\Notification;
9
use App\NotificationAttrib;
10
use Illuminate\Http\Request;
11
12
class NotificationController extends Controller
13
{
14
15
    use Helpers;
16
17
    public function __construct() {
18
    }
19
20
    /**
21
     * Display a listing of all notifications
22
     *
23
     * @return \Illuminate\Http\Response
24
     */
25
    public function index(Request $request, $type = null)
26
    {
27
        if ($type === 'archive')
28
        {
29
            $notifications = Notification::read([$request->user()->user_id])->limit()->get();
30
        }
31
        else {
32
            $notifications = Notification::unread()->limit()->get();
0 ignored issues
show
Bug introduced by
The method unread() does not exist on App\Notification. Did you maybe mean scopeUnread()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
33
        }
34
35
        if ($request->query('displayFormat') == 'human') {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
36
        }
37
        return $notifications;
38
    }
39
40
    public function update(Request $request, $id, $action)
41
    {
42
        if ($action === 'read' || $action === 'sticky')
43
        {
44
            if (NotificationAttrib::where('notifications_id', $id)->delete() >= 0)
45
            {
46
                $read = new NotificationAttrib;
47
                $read->notifications_id = $id;
0 ignored issues
show
Documentation introduced by
The property notifications_id does not exist on object<App\NotificationAttrib>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
48
                $read->user_id          = $request->user()->user_id;
0 ignored issues
show
Documentation introduced by
The property user_id does not exist on object<App\NotificationAttrib>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
49
                $read->key              = $action;
0 ignored issues
show
Documentation introduced by
The property key does not exist on object<App\NotificationAttrib>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
50
                $read->value            = 1;
0 ignored issues
show
Documentation introduced by
The property value does not exist on object<App\NotificationAttrib>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
51 View Code Duplication
                if ($read->save())
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
52
                {
53
                    return $this->response->array(array('statusText'=>'OK'));
54
                }
55
                else {
56
                    return $this->response->errorInternal();
57
                }
58
            }
59
        }
60
        elseif ($action === 'unread' || $action === 'unsticky')
61
        {
62 View Code Duplication
            if (NotificationAttrib::where('notifications_id', $id)->delete() >= 0)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
63
            {
64
                return $this->response->array(array('statusText'=>'OK'));
65
            }
66
            else {
67
                return $this->response->errorInternal();
68
            }
69
        }
70
    }
71
72
}
73