Test Setup Failed
Pull Request — master (#272)
by Zeeshan
07:55
created

NotifableBasic::getNotificationsNotRead()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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