Completed
Push — master ( bb59f2...5ee32e )
by Tony
11s
created

Notification   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 87.5%

Importance

Changes 5
Bugs 1 Features 2
Metric Value
wmc 9
c 5
b 1
f 2
lcom 1
cbo 4
dl 0
loc 114
ccs 21
cts 24
cp 0.875
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A markRead() 0 4 1
A markSticky() 0 4 1
A setAttrib() 0 14 2
A attribs() 0 4 1
A scopeIsUnread() 0 4 1
A scopeIsArchived() 0 5 1
A scopeLimit() 0 4 1
A scopeSource() 0 4 1
1
<?php
2
3
namespace App\Models;
4
5
6
use Illuminate\Database\Eloquent\Builder;
7
use Illuminate\Database\Eloquent\Model;
8
9
/**
10
 * App\Models\Notification
11
 *
12
 * @property integer $notifications_id
13
 * @property string $title
14
 * @property string $body
15
 * @property string $source
16
 * @property string $checksum
17
 * @property string $datetime
18
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\NotificationAttrib[] $attribs
19
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Notification whereNotificationsId($value)
20
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Notification whereTitle($value)
21
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Notification whereBody($value)
22
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Notification whereSource($value)
23
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Notification whereChecksum($value)
24
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Notification whereDatetime($value)
25
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Notification isUnread()
26
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Notification isArchived($request)
27
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Notification limit()
28
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Notification source()
29
 * @mixin \Eloquent
30
 */
31
class Notification extends Model
32
{
33
34
    /**
35
     * Indicates if the model should be timestamped.
36
     *
37
     * @var bool
38
     */
39
    public $timestamps = false;
40
    /**
41
     * The table associated with the model.
42
     *
43
     * @var string
44
     */
45
    protected $table = 'notifications';
46
    /**
47
     * The primary key column name.
48
     *
49
     * @var string
50
     */
51
    protected $primaryKey = 'notifications_id';
52
53
    // ---- Define Convenience Functions ---
54
55
    /**
56
     * Mark this notification as read or unread
57
     *
58
     * @param bool $enabled
59
     * @return bool
60
     */
61 1
    public function markRead($enabled = true)
62
    {
63 1
        return $this->setAttrib('read', $enabled);
64
    }
65
66
    /**
67
     * Mark this notification as sticky or unsticky
68
     *
69
     * @var bool $enabled
70
     * @return bool
71
     */
72
    public function markSticky($enabled = true)
73
    {
74
        return $this->setAttrib('sticky', $enabled);
75
    }
76
77
    /**
78
     * @param $name
79
     * @param $enabled
80
     * @return bool
81
     */
82 1
    private function setAttrib($name, $enabled)
83
    {
84 1
        if ($enabled === true) {
85 1
            $read = new NotificationAttrib;
86 1
            $read->user_id = \Auth::user()->user_id;
87 1
            $read->key = $name;
88 1
            $read->value = 1;
89 1
            $this->attribs()->save($read);
90 1
            return true;
91
        }
92
        else {
93
            return $this->attribs()->where('key', $name)->delete();
94
        }
95
    }
96
97
    // ---- Define Relationships ----
98
99 1
    public function attribs()
100
    {
101 1
        return $this->hasMany('App\Models\NotificationAttrib', 'notifications_id', 'notifications_id');
102
    }
103
104
    // ---- Define Scopes ----
105
106
    /**
107
     * @param Builder $query
108
     * @return mixed
109
     */
110 10
    public function scopeIsUnread(Builder $query)
111
    {
112 10
        return $query->leftJoin('notifications_attribs', 'notifications.notifications_id', '=', 'notifications_attribs.notifications_id')->source()->whereNull('notifications_attribs.user_id')->orWhere(['key' => 'sticky', 'value' => 1])->limit();
113
    }
114
115
    /**
116
     * @param Builder $query
117
     * @param User $user
118
     * @return mixed
119
     */
120 1
    public function scopeIsArchived(Builder $query, User $user)
121
    {
122 1
        $user_id = $user->user_id;
123 1
        return $query->leftJoin('notifications_attribs', 'notifications.notifications_id', '=', 'notifications_attribs.notifications_id')->source()->where('notifications_attribs.user_id', $user_id)->where(['key' => 'read', 'value' => 1])->limit();
124
    }
125
126
    /**
127
     * @param Builder $query
128
     * @return $this
129
     */
130 10
    public function scopeLimit(Builder $query)
131
    {
132 10
        return $query->select('notifications.*', 'key', 'users.username');
133
    }
134
135
    /**
136
     * @param Builder $query
137
     * @return Builder|static
138
     */
139 10
    public function scopeSource(Builder $query)
140
    {
141 10
        return $query->leftJoin('users', 'notifications.source', '=', 'users.user_id');
142
    }
143
144
}
145