NotifableBasic   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 168
Duplicated Lines 10.71 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 18
loc 168
rs 10
c 0
b 0
f 0
wmc 20
lcom 1
cbo 1

13 Methods

Rating   Name   Duplication   Size   Complexity  
A getNotificationRelation() 0 15 4
A notifynder() 0 4 1
A sendNotificationFrom() 0 4 1
A sendNotificationTo() 0 4 1
A readNotification() 0 4 1
A unreadNotification() 0 4 1
A updateSingleReadStatus() 0 12 4
A readAllNotifications() 0 4 1
A unreadAllNotifications() 0 4 1
A countUnreadNotifications() 0 4 1
A getNotifications() 9 9 2
A getNotificationsNotRead() 9 9 2
getLazyNotificationRelation() 0 1 ?

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 Fenos\Notifynder\Traits;
4
5
use Fenos\Notifynder\Helpers\TypeChecker;
6
7
/**
8
 * Class NotifableBasic.
9
 */
10
trait NotifableBasic
11
{
12
    /**
13
     * Get the notifications Relationship without any eager loading.
14
     *
15
     * @return \Illuminate\Database\Eloquent\Relations\HasMany|\Illuminate\Database\Eloquent\Relations\MorphMany
16
     */
17
    abstract public function getLazyNotificationRelation();
18
19
    /**
20
     * Get the notifications Relationship.
21
     *
22
     * @param array|bool $eagerLoad
23
     * @return \Illuminate\Database\Eloquent\Relations\HasMany|\Illuminate\Database\Eloquent\Relations\MorphMany
24
     */
25
    public function getNotificationRelation($eagerLoad = null)
26
    {
27
        $with = [];
28
        if (is_null($eagerLoad)) {
29
            $eagerLoad = notifynder_config('eager_load', false);
30
        }
31
32
        if ($eagerLoad === true) {
33
            $with = ['category', 'from', 'to']; // all relations
34
        } elseif (is_array($eagerLoad)) {
35
            $with = $eagerLoad;
36
        }
37
38
        return $this->getLazyNotificationRelation()->with($with);
39
    }
40
41
    /**
42
     * Get a new NotifynderManager instance with the given category.
43
     *
44
     * @param string|int|\Fenos\Notifynder\Models\NotificationCategory $category
45
     * @return \Fenos\Notifynder\Managers\NotifynderManager
46
     */
47
    public function notifynder($category)
48
    {
49
        return app('notifynder')->category($category);
50
    }
51
52
    /**
53
     * Get a new NotifynderManager instance with the given category and $this as the sender.
54
     *
55
     * @param string|int|\Fenos\Notifynder\Models\NotificationCategory $category
56
     * @return \Fenos\Notifynder\Managers\NotifynderManager
57
     */
58
    public function sendNotificationFrom($category)
59
    {
60
        return $this->notifynder($category)->from($this);
0 ignored issues
show
Documentation Bug introduced by
The method from does not exist on object<Fenos\Notifynder\...gers\NotifynderManager>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
61
    }
62
63
    /**
64
     * Get a new NotifynderManager instance with the given category and $this as the receiver.
65
     *
66
     * @param string|int|\Fenos\Notifynder\Models\NotificationCategory $category
67
     * @return \Fenos\Notifynder\Managers\NotifynderManager
68
     */
69
    public function sendNotificationTo($category)
70
    {
71
        return $this->notifynder($category)->to($this);
0 ignored issues
show
Documentation Bug introduced by
The method to does not exist on object<Fenos\Notifynder\...gers\NotifynderManager>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
72
    }
73
74
    /**
75
     * Read a single Notification.
76
     *
77
     * @param int $notification
78
     * @return bool
79
     */
80
    public function readNotification($notification)
81
    {
82
        return $this->updateSingleReadStatus($notification, 1);
83
    }
84
85
    /**
86
     * Unread a single Notification.
87
     *
88
     * @param int $notification
89
     * @return bool
90
     */
91
    public function unreadNotification($notification)
92
    {
93
        return $this->updateSingleReadStatus($notification, 0);
94
    }
95
96
    /**
97
     * @param int $notification
98
     * @param int $value
99
     * @return bool
100
     */
101
    protected function updateSingleReadStatus($notification, $value)
102
    {
103
        if (! TypeChecker::isNotification($notification, false)) {
104
            $notification = $this->getLazyNotificationRelation()->findOrFail($notification);
105
        }
106
107
        if ($this->getLazyNotificationRelation()->where($notification->getKeyName(), $notification->getKey())->exists()) {
108
            return $value ? $notification->read() : $notification->unread();
109
        }
110
111
        return false;
112
    }
113
114
    /**
115
     * Read all Notifications.
116
     *
117
     * @return mixed
118
     */
119
    public function readAllNotifications()
120
    {
121
        return $this->getLazyNotificationRelation()->update(['read' => 1]);
122
    }
123
124
    /**
125
     * Unread all Notifications.
126
     *
127
     * @return mixed
128
     */
129
    public function unreadAllNotifications()
130
    {
131
        return $this->getLazyNotificationRelation()->update(['read' => 0]);
132
    }
133
134
    /**
135
     * Count unread notifications.
136
     *
137
     * @return int
138
     */
139
    public function countUnreadNotifications()
140
    {
141
        return $this->getLazyNotificationRelation()->byRead(0)->count();
142
    }
143
144
    /**
145
     * Get all Notifications ordered by creation and optional limit.
146
     *
147
     * @param null|int $limit
148
     * @param string $order
149
     * @return \Illuminate\Database\Eloquent\Collection
150
     */
151 View Code Duplication
    public function getNotifications($limit = null, $order = 'desc')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
152
    {
153
        $query = $this->getNotificationRelation()->orderBy('created_at', $order);
154
        if (! is_null($limit)) {
155
            $query->limit($limit);
156
        }
157
158
        return $query->get();
159
    }
160
161
    /**
162
     * Get all unread Notifications.
163
     *
164
     * @param null|int $limit
165
     * @param string $order
166
     * @return \Illuminate\Database\Eloquent\Collection
167
     */
168 View Code Duplication
    public function getNotificationsNotRead($limit = null, $order = 'desc')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
169
    {
170
        $query = $this->getNotificationRelation()->byRead(0)->orderBy('created_at', $order);
171
        if (! is_null($limit)) {
172
            $query->limit($limit);
173
        }
174
175
        return $query->get();
176
    }
177
}
178