Completed
Pull Request — master (#106)
by Fabrizio
02:14
created

Notification::mergeFillable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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