Completed
Pull Request — master (#72)
by Oliver
02:12
created

Notification::getNotifyBodyAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 1
Metric Value
c 4
b 1
f 1
dl 0
loc 6
rs 9.4286
cc 1
eloc 3
nc 1
nop 0
1
<?php namespace Fenos\Notifynder\Models;
2
3
use Fenos\Notifynder\Notifications\ExtraParams;
4
use Fenos\Notifynder\Parsers\NotifynderParser;
5
use Illuminate\Database\Eloquent\Model;
6
use Carbon\Carbon;
7
8
/**
9
 * Class Notification
10
 *
11
 * @property int to_id
12
 * @property string to_type
13
 * @property int from_id
14
 * @property string from_type
15
 * @property int category_id
16
 * @property int read
17
 * @property string url
18
 * @property string extra
19
 *
20
 * Php spec complain when model is mocked
21
 * if I turn them on as php doc block
22
 *
23
 * @method wherePolymorphic
24
 * @method withNotRead
25
 *
26
 * @package Fenos\Notifynder\Models
27
 */
28
class Notification extends Model
29
{
30
31
    /**
32
     * @var array
33
     */
34
    protected $fillable = [
35
        'to_id','to_type','from_id','from_type',
36
        'category_id','read','url','extra', 'expire_time',
37
    ];
38
39
    /**
40
     * Custom Collection
41
     *
42
     * @param  array                                                         $models
43
     * @return NotifynderCollection|\Illuminate\Database\Eloquent\Collection
44
     */
45
    public function newCollection(array $models = array())
46
    {
47
        return new NotifynderCollection($models);
48
    }
49
50
    /**
51
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
52
     */
53
    public function body()
54
    {
55
        return $this->belongsTo(NotificationCategory::class, 'category_id');
56
    }
57
58
    /**
59
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo|\Illuminate\Database\Eloquent\Relations\MorphTo
60
     */
61 View Code Duplication
    public function from()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
    {
63
        // check if on the configurations file there is the option
64
        // polymorphic setted to true, if so Notifynder will work
65
        // polymorphic.
66
        if (config('notifynder.polymorphic') == false) {
67
            return $this->belongsTo(config('notifynder.model'), 'from_id');
68
        } else {
69
            return $this->morphTo();
70
        }
71
    }
72
73
    /**
74
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo|\Illuminate\Database\Eloquent\Relations\MorphTo
75
     */
76 View Code Duplication
    public function to()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
77
    {
78
        // check if on the configurations file there is the option
79
        // polymorphic setted to true, if so Notifynder will work
80
        // polymorphic.
81
        if (config('notifynder.polymorphic') == false) {
82
            return $this->belongsTo(config('notifynder.model'), 'to_id');
83
        } else {
84
            return $this->morphTo();
85
        }
86
    }
87
88
    /**
89
     * Not read scope
90
     *
91
     * @param $query
92
     * @return mixed
93
     */
94
    public function scopeWithNotRead($query)
95
    {
96
        return $query->where('read', 0);
97
    }
98
99
    /**
100
     * Only Expired Notification scope
101
     *
102
     * @param $query
103
     * @return mixed
104
     */
105
    public function scopeOnlyExpired($query)
106
    {
107
        return $query->where('expire_time', '<', Carbon::now());
108
    }
109
110
    /**
111
     * Where Polymorphic
112
     *
113
     * @param $query
114
     * @param $id
115
     * @param $type
116
     * @return mixed
117
     */
118
    public function scopeWherePolymorphic($query, $id, $type)
119
    {
120
        if (! $type or config('notifynder.polymorphic') === false) {
121
            return $query->where('to_id', $id);
122
        } else {
123
            return $query->where('to_id', $id)
124
                ->where('to_type', $type);
125
        }
126
    }
127
128
    /**
129
     * @param $value
130
     * @return mixed|string
131
     */
132
    public function getExtraAttribute($value)
133
    {
134
        return new ExtraParams(json_decode($value));
135
    }
136
137
    /**
138
     * Filter Scope by category
139
     *
140
     * @param $query
141
     * @param $category
142
     * @return mixed
143
     */
144
    public function scopeByCategory($query,$category)
145
    {
146
        if (is_numeric($category)) {
147
148
            return $query->where('category_id',$category);
149
        }
150
151
        return $query->whereHas('body', function($categoryQuery) use ($category) {
152
            $categoryQuery->where('name',$category);
153
        });
154
    }
155
}