Completed
Push — version-4 ( f39f28...cf59bd )
by
unknown
07:07
created

Notifable::updateSingleReadStatus()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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