Notification   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 174
Duplicated Lines 0 %

Coupling/Cohesion

Components 4
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 174
rs 10
c 0
b 0
f 0
wmc 18
lcom 4
cbo 5

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 3
A category() 0 4 1
A to() 0 8 2
A getCustomFillableFields() 0 4 1
A mergeFillables() 0 6 1
A getTextAttribute() 0 9 2
A read() 0 4 1
A unread() 0 4 1
A resend() 0 7 1
A isAnonymous() 0 4 1
A scopeByCategory() 0 6 1
A scopeByRead() 0 4 1
A from() 0 8 2
1
<?php
2
3
namespace Fenos\Notifynder\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Database\Eloquent\Builder;
7
use Fenos\Notifynder\Parsers\NotificationParser;
8
use Fenos\Notifynder\Builder\Notification as BuilderNotification;
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 bool $read
19
 * @property string $url
20
 * @property array $extra
21
 * @property string $expires_at
22
 * @property int $stack_id
23
 */
24
class Notification extends Model
25
{
26
    /**
27
     * @var array
28
     */
29
    protected $fillable = [
30
        'to_id',
31
        'to_type',
32
        'from_id',
33
        'from_type',
34
        'category_id',
35
        'read',
36
        'url',
37
        'extra',
38
        'expires_at',
39
        'stack_id',
40
    ];
41
42
    /**
43
     * @var array
44
     */
45
    protected $appends = [
46
        'text',
47
    ];
48
49
    /**
50
     * @var array
51
     */
52
    protected $casts = [
53
        'extra' => 'array',
54
    ];
55
56
    /**
57
     * Notification constructor.
58
     *
59
     * @param array|\Fenos\Notifynder\Builder\Notification $attributes
60
     */
61
    public function __construct($attributes = [])
62
    {
63
        $table = app('notifynder.resolver.model')->getTable(get_class($this));
64
        if (! empty($table)) {
65
            $this->setTable($table);
66
        }
67
68
        $this->fillable($this->mergeFillables());
69
70
        if ($attributes instanceof BuilderNotification) {
71
            $attributes = $attributes->toArray();
72
        }
73
74
        parent::__construct($attributes);
75
    }
76
77
    /**
78
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
79
     */
80
    public function category()
81
    {
82
        return $this->belongsTo(NotificationCategory::class, 'category_id');
83
    }
84
85
    /**
86
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo|\Illuminate\Database\Eloquent\Relations\MorphTo
87
     */
88
    public function from()
89
    {
90
        if (notifynder_config()->isPolymorphic()) {
91
            return $this->morphTo('from');
92
        }
93
94
        return $this->belongsTo(notifynder_config()->getNotifiedModel(), 'from_id');
95
    }
96
97
    /**
98
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo|\Illuminate\Database\Eloquent\Relations\MorphTo
99
     */
100
    public function to()
101
    {
102
        if (notifynder_config()->isPolymorphic()) {
103
            return $this->morphTo('to');
104
        }
105
106
        return $this->belongsTo(notifynder_config()->getNotifiedModel(), 'to_id');
107
    }
108
109
    /**
110
     * @return array
111
     */
112
    public function getCustomFillableFields()
113
    {
114
        return notifynder_config()->getAdditionalFields();
115
    }
116
117
    /**
118
     * @return array
119
     */
120
    protected function mergeFillables()
121
    {
122
        $fillables = array_unique(array_merge($this->getFillable(), $this->getCustomFillableFields()));
123
124
        return $fillables;
125
    }
126
127
    /**
128
     * @return string
129
     * @throws \Fenos\Notifynder\Exceptions\ExtraParamsException
130
     */
131
    public function getTextAttribute()
132
    {
133
        if (! array_key_exists('text', $this->attributes)) {
134
            $notifynderParse = new NotificationParser();
135
            $this->attributes['text'] = $notifynderParse->parse($this);
136
        }
137
138
        return $this->attributes['text'];
139
    }
140
141
    /**
142
     * @return bool|int
143
     */
144
    public function read()
145
    {
146
        return $this->update(['read' => 1]);
147
    }
148
149
    /**
150
     * @return bool|int
151
     */
152
    public function unread()
153
    {
154
        return $this->update(['read' => 0]);
155
    }
156
157
    /**
158
     * @return bool
159
     */
160
    public function resend()
161
    {
162
        $this->updateTimestamps();
163
        $this->read = 0;
164
165
        return $this->save();
166
    }
167
168
    /**
169
     * @return bool
170
     */
171
    public function isAnonymous()
172
    {
173
        return is_null($this->from_id);
174
    }
175
176
    /**
177
     * @param Builder $query
178
     * @param $category
179
     * @return Builder
180
     */
181
    public function scopeByCategory(Builder $query, $category)
182
    {
183
        $categoryId = NotificationCategory::getIdByCategory($category);
184
185
        return $query->where('category_id', $categoryId);
186
    }
187
188
    /**
189
     * @param Builder $query
190
     * @param int $read
191
     * @return Builder
192
     */
193
    public function scopeByRead(Builder $query, $read = 1)
194
    {
195
        return $query->where('read', $read);
196
    }
197
}
198