Completed
Pull Request — master (#106)
by Fabrizio
04:32 queued 02:13
created

Notification::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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