Completed
Pull Request — develop (#195)
by Tony
04:21
created

Notification::setAttrib()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 2
dl 0
loc 13
ccs 8
cts 8
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
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
    // ---- Helper Functions ----
53
54
    /**
55
     * Mark this notification as read or unread
56
     *
57
     * @param bool $enabled
58
     * @return bool
59
     */
60 1
    public function markRead($enabled = true)
61
    {
62 1
        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 1
    private function setAttrib($name, $enabled)
82
    {
83 1
        if ($enabled === true) {
84 1
            $read = new NotificationAttrib;
85 1
            $read->user_id = \Auth::user()->user_id;
86 1
            $read->key = $name;
87 1
            $read->value = 1;
88 1
            $this->attribs()->save($read);
89 1
            return true;
90
        } else {
91
            return $this->attribs()->where('key', $name)->delete();
92
        }
93
    }
94
95
    // ---- Define Scopes ----
96
97
    /**
98
     * @param Builder $query
99
     * @return mixed
100
     */
101 1
    public function scopeIsUnread(Builder $query)
102
    {
103 1
        return $query->leftJoin('notifications_attribs', 'notifications.notifications_id', '=', 'notifications_attribs.notifications_id')->source()->whereNull('notifications_attribs.user_id')->orWhere(['key' => 'sticky', 'value' => 1])->limit();
104
    }
105
106
    /**
107
     * @param Builder $query
108
     * @param User $user
109
     * @return mixed
110
     */
111 1
    public function scopeIsArchived(Builder $query, User $user)
112
    {
113 1
        $user_id = $user->user_id;
114 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();
115
    }
116
117
    /**
118
     * @param Builder $query
119
     * @return $this
120
     */
121 1
    public function scopeLimit(Builder $query)
122
    {
123 1
        return $query->select('notifications.*', 'key', 'users.username');
0 ignored issues
show
Bug introduced by
The method select() does not exist on Illuminate\Database\Eloquent\Builder. Did you maybe mean createSelectWithConstraint()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
124
    }
125
126
    /**
127
     * @param Builder $query
128
     * @return Builder|static
129
     */
130 1
    public function scopeSource(Builder $query)
131
    {
132 1
        return $query->leftJoin('users', 'notifications.source', '=', 'users.user_id');
133
    }
134
135
    // ---- Define Relationships ----
136
137
    /**
138
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
139
     */
140 1
    public function attribs()
141
    {
142 1
        return $this->hasMany('App\Models\NotificationAttrib', 'notifications_id', 'notifications_id');
143
    }
144
}
145