Completed
Push — version-4 ( 855c86...c55f2f )
by
unknown
09:40
created

Notifable::unreadAllNotifications()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
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\Helpers\TypeChecker;
6
7
/**
8
 * Class Notifable
9
 * @package Fenos\Notifynder\Traits
10
 */
11
trait Notifable
12
{
13
    /**
14
     * Get the notifications Relationship.
15
     *
16
     * @return \Illuminate\Database\Eloquent\Relations\HasMany|\Illuminate\Database\Eloquent\Relations\MorphMany
17
     */
18
    public function notifications()
19
    {
20
        $model = notifynder_config()->getNotificationModel();
21
        if (notifynder_config()->isPolymorphic()) {
22
            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...
23
        }
24
25
        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...
26
    }
27
28
    /**
29
     * Get a new NotifynderManager instance with the given category.
30
     *
31
     * @param string|int|\Fenos\Notifynder\Models\NotificationCategory $category
32
     * @return \Fenos\Notifynder\Managers\NotifynderManager
33
     */
34
    public function notifynder($category)
35
    {
36
        return app('notifynder')->category($category);
37
    }
38
39
    /**
40
     * Get a new NotifynderManager instance with the given category and $this as the sender.
41
     *
42
     * @param string|int|\Fenos\Notifynder\Models\NotificationCategory $category
43
     * @return \Fenos\Notifynder\Managers\NotifynderManager
44
     */
45
    public function sendNotificationFrom($category)
46
    {
47
        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...
48
    }
49
50
    /**
51
     * Get a new NotifynderManager instance with the given category and $this as the receiver.
52
     *
53
     * @param string|int|\Fenos\Notifynder\Models\NotificationCategory $category
54
     * @return \Fenos\Notifynder\Managers\NotifynderManager
55
     */
56
    public function sendNotificationTo($category)
57
    {
58
        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...
59
    }
60
61
    /**
62
     * Read a single Notification.
63
     *
64
     * @param int $notification
65
     * @return bool
66
     */
67 View Code Duplication
    public function readNotification($notification)
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...
68
    {
69
        if (! TypeChecker::isNotification($notification, false)) {
70
            $notification = $this->notifications()->firstOrFail($notification);
71
        }
72
73
        if ($this->notifications()->where($notification->getKeyName(), $notification->getKey())->exists()) {
74
            return $notification->read();
75
        }
76
77
        return false;
78
    }
79
80
    /**
81
     * Read all Notifications.
82
     *
83
     * @return mixed
84
     */
85
    public function readAllNotifications()
86
    {
87
        return $this->notifications()->update(['read' => 1]);
88
    }
89
90
    /**
91
     * Unread a single Notification.
92
     *
93
     * @param int $notification
94
     * @return bool
95
     */
96 View Code Duplication
    public function unreadNotification($notification)
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...
97
    {
98
        if (! TypeChecker::isNotification($notification, false)) {
99
            $notification = $this->notifications()->firstOrFail($notification);
100
        }
101
102
        if ($this->notifications()->where($notification->getKeyName(), $notification->getKey())->exists()) {
103
            return $notification->unread();
104
        }
105
106
        return false;
107
    }
108
109
    /**
110
     * Unread all Notifications.
111
     *
112
     * @return mixed
113
     */
114
    public function unreadAllNotifications()
115
    {
116
        return $this->notifications()->update(['read' => 0]);
117
    }
118
119
    /**
120
     * Count unread notifications.
121
     *
122
     * @return int
123
     */
124
    public function countUnreadNotifications()
125
    {
126
        return $this->notifications()->byRead(0)->count();
127
    }
128
129
    /**
130
     * Get all Notifications ordered by creation and optional limit.
131
     * 
132
     * @param null|int $limit
133
     * @param string $order
134
     * @return \Illuminate\Database\Eloquent\Collection
135
     */
136
    public function getNotifications($limit = null, $order = 'desc')
137
    {
138
        $query = $this->notifications()->orderBy('created_at', $order);
139
        if (! is_null($limit)) {
140
            $query->limit($limit);
141
        }
142
143
        return $query->get();
144
    }
145
}
146