Completed
Push — version-4 ( 455d27...4e0b22 )
by
unknown
02:50
created

Notifable::readAllNotifications()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
cc 1
eloc 2
c 1
b 1
f 1
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Fenos\Notifynder\Traits;
4
5
use Fenos\Notifynder\Models\Notification;
6
7
trait Notifable
8
{
9
    public function notifications()
10
    {
11
        $model = notifynder_config()->getNotificationModel();
12
        if (notifynder_config()->isPolymorphic()) {
13
            return $this->morphMany($model, 'to');
0 ignored issues
show
Bug introduced by
It seems like morphMany() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
14
        }
15
16
        return $this->hasMany($model, 'to_id');
0 ignored issues
show
Bug introduced by
It seems like hasMany() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
17
    }
18
19
    public function notifynder($category)
20
    {
21
        return app('notifynder')->category($category);
22
    }
23
24
    public function sendNotificationFrom($category)
25
    {
26
        return $this->notifynder($category)->from($this);
27
    }
28
29
    public function sendNotificationTo($category)
30
    {
31
        return $this->notifynder($category)->to($this);
32
    }
33
34
    public function readNotification($notification)
35
    {
36
        if (! ($notification instanceof Notification)) {
37
            $notification = Notification::firstOrFail($notification);
38
        }
39
40
        return $notification->read();
41
    }
42
43
    public function readAllNotifications()
44
    {
45
        return $this->notifications()->update(['read' => 1]);
46
    }
47
48
    public function unreadNotification($notification)
49
    {
50
        if (! ($notification instanceof Notification)) {
51
            $notification = Notification::firstOrFail($notification);
52
        }
53
54
        return $notification->unread();
55
    }
56
57
    public function countUnreadNotifications()
58
    {
59
        return $this->notifications()->byRead(0)->count();
60
    }
61
62
    public function getNotifications($limit = null, $order = 'desc')
63
    {
64
        $query = $this->notifications()->orderBy('created_at', $order);
65
        if (! is_null($limit)) {
66
            $query->limit($limit);
67
        }
68
69
        return $query->get();
70
    }
71
}
72