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

NotificationController   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 58
Duplicated Lines 24.14 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 5
Bugs 1 Features 1
Metric Value
wmc 11
c 5
b 1
f 1
lcom 1
cbo 5
dl 14
loc 58
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
C update() 14 31 8
A index() 0 11 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace App\Api\Controllers;
4
5
use Dingo\Api\Http;
6
use Dingo\Api\Routing\Helpers;
7
use App\Notification;
8
use App\NotificationAttrib;
9
use Illuminate\Http\Request;
10
11
class NotificationController extends Controller
12
{
13
14
    use Helpers;
15
16
    public function __construct() {
17
    }
18
19
    /**
20
     * Display a listing of all notifications
21
     *
22
     * @return \Illuminate\Http\Response
23
     */
24
    public function index(Request $request, $type = null)
25
    {
26
        if ($type === 'archive')
27
        {
28
            $notifications = Notification::IsArchived($request)->get();
0 ignored issues
show
Bug introduced by
The method IsArchived() does not exist on App\Notification. Did you maybe mean scopeIsArchived()?

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...
29
        }
30
        else {
31
            $notifications = Notification::IsUnread()->get();
0 ignored issues
show
Bug introduced by
The method IsUnread() does not exist on App\Notification. Did you maybe mean scopeIsUnread()?

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...
32
        }
33
        return $notifications;
34
    }
35
36
    public function update(Request $request, $id, $action)
37
    {
38
        if ($action === 'read' || $action === 'sticky')
39
        {
40
            if (NotificationAttrib::where('notifications_id', $id)->delete() >= 0)
41
            {
42
                $read = new NotificationAttrib;
43
                $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...
44
                $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...
45
                $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...
46
                $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...
47 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...
48
                {
49
                    return $this->response->array(array('statusText'=>'OK'));
50
                }
51
                else {
52
                    return $this->response->errorInternal();
53
                }
54
            }
55
        }
56
        elseif ($action === 'unread' || $action === 'unsticky')
57
        {
58 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...
59
            {
60
                return $this->response->array(array('statusText'=>'OK'));
61
            }
62
            else {
63
                return $this->response->errorInternal();
64
            }
65
        }
66
    }
67
68
}
69