Completed
Push — fixes ( 6830e4...798510 )
by Tony
03:34
created

Notification   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 3
Bugs 0 Features 2
Metric Value
wmc 9
lcom 1
cbo 4
dl 0
loc 114
rs 10
c 3
b 0
f 2

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