Notification::scopeUnread()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Hechoenlaravel\JarvisFoundation\Notifications\SendAppNotification;
4
5
use Illuminate\Database\Eloquent\Model;
6
7
/**
8
 * Class Notification
9
 * @package Hechoenlaravel\JarvisFoundation\Notifications\SendAppNotification
10
 */
11
class Notification extends Model
12
{
13
    /**
14
     * @var string
15
     */
16
    protected $table = "app_notifications";
17
18
    /**
19
     * @var array
20
     */
21
    protected $fillable = ['user_id', 'type', 'message', 'link', 'readed_at'];
22
23
    /**
24
     * @var array
25
     */
26
    protected $dates = ['created_at', 'updated_at', 'readed_at'];
27
28
    protected $types = [
29
        'success' => 'check',
30
        'info' => 'info',
31
        'danger' => 'times',
32
        'warning' => 'warning',
33
    ];
34
35
    /**
36
     * Filter by User
37
     * @param $query
38
     * @param $user
39
     * @return mixed
40
     */
41
    public function scopeByUser($query, $user)
42
    {
43
        return $query->where('user_id', $user->id);
44
    }
45
46
    /**
47
     * Filter the Unread Ones
48
     * @param $query
49
     */
50
    public function scopeUnread($query)
51
    {
52
        $query->where('readed_at');
53
    }
54
55
    /**
56
     * @return mixed
57
     */
58
    public function getTimeAgo()
59
    {
60
        return $this->created_at->diffForHumans();
61
    }
62
63
    /**
64
     * @return string
65
     */
66
    public function getLink()
67
    {
68
        return route('notifications.read', ['id' => $this->id]);
69
    }
70
71
    /**
72
     * @return string
73
     */
74
    public function getIconByType()
75
    {
76
        if (!isset($this->types[$this->type])) {
77
            return null;
78
        }
79
        return $this->types[$this->type];
80
    }
81
}
82