Completed
Push — issue-105 ( 5d4395 )
by
unknown
02:22
created

Notification::getExtraAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
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
use Illuminate\Support\Arr;
8
9
/**
10
 * Class Notification
11
 *
12
 * @property int to_id
13
 * @property string to_type
14
 * @property int from_id
15
 * @property string from_type
16
 * @property int category_id
17
 * @property int read
18
 * @property string url
19
 * @property string extra
20
 *
21
 * Php spec complain when model is mocked
22
 * if I turn them on as php doc block
23
 *
24
 * @method wherePolymorphic
25
 * @method withNotRead
26
 *
27
 * @package Fenos\Notifynder\Models
28
 */
29
class Notification extends Model
30
{
31
32
    /**
33
     * @var array
34
     */
35
    protected $fillable = [
36
        'to_id','to_type','from_id','from_type',
37
        'category_id','read','url','extra', 'expire_time',
38
    ];
39
40
    public function __construct(array $attributes)
41
    {
42
        $fillables = array_unique($this->getFillable() + Arr::flatten(config('notifynder.additional_fields')));
43
        $this->fillable($fillables);
44
45
        parent::__construct($attributes);
46
    }
47
48
    /**
49
     * Custom Collection
50
     *
51
     * @param  array                                                         $models
52
     * @return NotifynderCollection|\Illuminate\Database\Eloquent\Collection
53
     */
54
    public function newCollection(array $models = array())
55
    {
56
        return new NotifynderCollection($models);
57
    }
58
59
    /**
60
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
61
     */
62
    public function body()
63
    {
64
        return $this->belongsTo(NotificationCategory::class, 'category_id');
65
    }
66
67
    /**
68
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo|\Illuminate\Database\Eloquent\Relations\MorphTo
69
     */
70 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...
71
    {
72
        // check if on the configurations file there is the option
73
        // polymorphic setted to true, if so Notifynder will work
74
        // polymorphic.
75
        if (config('notifynder.polymorphic') == false) {
76
            return $this->belongsTo(config('notifynder.model'), 'from_id');
77
        } else {
78
            return $this->morphTo();
79
        }
80
    }
81
82
    /**
83
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo|\Illuminate\Database\Eloquent\Relations\MorphTo
84
     */
85 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...
86
    {
87
        // check if on the configurations file there is the option
88
        // polymorphic setted to true, if so Notifynder will work
89
        // polymorphic.
90
        if (config('notifynder.polymorphic') == false) {
91
            return $this->belongsTo(config('notifynder.model'), 'to_id');
92
        } else {
93
            return $this->morphTo();
94
        }
95
    }
96
97
    /**
98
     * Not read scope
99
     *
100
     * @param $query
101
     * @return mixed
102
     */
103
    public function scopeWithNotRead($query)
104
    {
105
        return $query->where('read', 0);
106
    }
107
108
    /**
109
     * Only Expired Notification scope
110
     *
111
     * @param $query
112
     * @return mixed
113
     */
114
    public function scopeOnlyExpired($query)
115
    {
116
        return $query->where('expire_time', '<', Carbon::now());
117
    }
118
119
    /**
120
     * Where Polymorphic
121
     *
122
     * @param $query
123
     * @param $id
124
     * @param $type
125
     * @return mixed
126
     */
127
    public function scopeWherePolymorphic($query, $id, $type)
128
    {
129
        if (! $type or config('notifynder.polymorphic') === false) {
130
            return $query->where('to_id', $id);
131
        } else {
132
            return $query->where('to_id', $id)
133
                ->where('to_type', $type);
134
        }
135
    }
136
137
    /**
138
     * Get parsed body attributes
139
     *
140
     * @return mixed
141
     */
142
    public function getNotifyBodyAttribute()
143
    {
144
        $notifynderParse = new NotifynderParser();
145
146
        return $notifynderParse->parse($this);
147
    }
148
149
    /**
150
     * @param $value
151
     * @return mixed|string
152
     */
153
    public function getExtraAttribute($value)
154
    {
155
        return new ExtraParams(json_decode($value));
156
    }
157
158
    /**
159
     * Filter Scope by category
160
     *
161
     * @param $query
162
     * @param $category
163
     * @return mixed
164
     */
165
    public function scopeByCategory($query,$category)
166
    {
167
        if (is_numeric($category)) {
168
169
            return $query->where('category_id',$category);
170
        }
171
172
        return $query->whereHas('body', function($categoryQuery) use ($category) {
173
            $categoryQuery->where('name',$category);
174
        });
175
    }
176
}
177