Completed
Push — master ( 2f25c9...4c0020 )
by
unknown
06:10
created

NotifableBasic::countUnreadNotifications()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
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
            if ($value) {
87
                return $notification->read();
88
            } else {
89
                return $notification->unread();
90
            }
91
        }
92
93
        return false;
94
    }
95
96
    /**
97
     * Read all Notifications.
98
     *
99
     * @return mixed
100
     */
101
    public function readAllNotifications()
102
    {
103
        return $this->getNotificationRelation()->update(['read' => 1]);
104
    }
105
106
    /**
107
     * Unread all Notifications.
108
     *
109
     * @return mixed
110
     */
111
    public function unreadAllNotifications()
112
    {
113
        return $this->getNotificationRelation()->update(['read' => 0]);
114
    }
115
116
    /**
117
     * Count unread notifications.
118
     *
119
     * @return int
120
     */
121
    public function countUnreadNotifications()
122
    {
123
        return $this->getNotificationRelation()->byRead(0)->count();
124
    }
125
126
    /**
127
     * Get all Notifications ordered by creation and optional limit.
128
     *
129
     * @param null|int $limit
130
     * @param string $order
131
     * @return \Illuminate\Database\Eloquent\Collection
132
     */
133
    public function getNotifications($limit = null, $order = 'desc')
134
    {
135
        $query = $this->getNotificationRelation()->orderBy('created_at', $order);
136
        if (! is_null($limit)) {
137
            $query->limit($limit);
138
        }
139
140
        return $query->get();
141
    }
142
}
143