Completed
Push — master ( 6cebc9...e22e92 )
by
unknown
02:56
created

Notification::getTextAttribute()   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 0
1
<?php
2
3
namespace Fenos\Notifynder\Models;
4
5
use Fenos\Notifynder\Notifications\ExtraParams;
6
use Fenos\Notifynder\Parsers\NotifynderParser;
7
use Illuminate\Contracts\Container\Container;
8
use Illuminate\Database\Eloquent\Model;
9
use Carbon\Carbon;
10
use Illuminate\Support\Arr;
11
12
/**
13
 * Class Notification.
14
 *
15
 * @property int to_id
16
 * @property string to_type
17
 * @property int from_id
18
 * @property string from_type
19
 * @property int category_id
20
 * @property int read
21
 * @property string url
22
 * @property string extra
23
 *
24
 * Php spec complain when model is mocked
25
 * if I turn them on as php doc block
26
 *
27
 * @method wherePolymorphic
28
 * @method withNotRead
29
 */
30
class Notification extends Model
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
    /**
41
     * Notification constructor.
42
     *
43
     * @param array $attributes
44
     */
45
    public function __construct(array $attributes = [])
46
    {
47
        $fillables = $this->mergeFillable();
48
        $this->fillable($fillables);
49
50
        parent::__construct($attributes);
51
    }
52
53
    /**
54
     * Custom Collection.
55
     *
56
     * @param  array                                                         $models
57
     * @return NotifynderCollection|\Illuminate\Database\Eloquent\Collection
58
     */
59
    public function newCollection(array $models = [])
60
    {
61
        return new NotifynderCollection($models);
62
    }
63
64
    /**
65
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
66
     */
67
    public function body()
68
    {
69
        return $this->belongsTo(NotificationCategory::class, 'category_id');
70
    }
71
72
    /**
73
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo|\Illuminate\Database\Eloquent\Relations\MorphTo
74
     */
75
    public function from()
76
    {
77
        // check if on the configurations file there is the option
78
        // polymorphic set to true, if so Notifynder will work
79
        // polymorphic.
80
        if (config('notifynder.polymorphic') == false) {
81
            return $this->belongsTo(config('notifynder.model'), 'from_id');
82
        }
83
84
        return $this->morphTo();
85
    }
86
87
    /**
88
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo|\Illuminate\Database\Eloquent\Relations\MorphTo
89
     */
90
    public function to()
91
    {
92
        // check if on the configurations file there is the option
93
        // polymorphic set to true, if so Notifynder will work
94
        // polymorphic.
95
        if (config('notifynder.polymorphic') == false) {
96
            return $this->belongsTo(config('notifynder.model'), 'to_id');
97
        }
98
99
        return $this->morphTo();
100
    }
101
102
    /**
103
     * Not read scope.
104
     *
105
     * @param $query
106
     * @return mixed
107
     */
108
    public function scopeWithNotRead($query)
109
    {
110
        return $query->where('read', 0);
111
    }
112
113
    /**
114
     * Only Expired Notification scope.
115
     *
116
     * @param $query
117
     * @return mixed
118
     */
119
    public function scopeOnlyExpired($query)
120
    {
121
        return $query->where('expire_time', '<', Carbon::now());
122
    }
123
124
    /**
125
     * Where Polymorphic.
126
     *
127
     * @param $query
128
     * @param $toId
129
     * @param $type
130
     * @return mixed
131
     */
132
    public function scopeWherePolymorphic($query, $toId, $type)
133
    {
134
        if (! $type or config('notifynder.polymorphic') === false) {
135
            return $query->where('to_id', $toId);
136
        }
137
138
        return $query->where('to_id', $toId)
139
            ->where('to_type', $type);
140
    }
141
142
    /**
143
     * Get parsed body attributes.
144
     *
145
     * @return string
146
     */
147
    public function getNotifyBodyAttribute()
148
    {
149
        $notifynderParse = new NotifynderParser();
150
151
        return $notifynderParse->parse($this);
152
    }
153
154
    /**
155
     * Get parsed body attributes.
156
     *
157
     * @return string
158
     */
159
    public function getTextAttribute()
160
    {
161
        return $this->notify_body;
0 ignored issues
show
Documentation introduced by
The property notify_body does not exist on object<Fenos\Notifynder\Models\Notification>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
162
    }
163
164
    /**
165
     * @param $value
166
     * @return \Fenos\Notifynder\Notifications\ExtraParams
167
     */
168
    public function getExtraAttribute($value)
169
    {
170
        if (! empty($value)) {
171
            return new ExtraParams($value);
172
        }
173
174
        return new ExtraParams([]);
175
    }
176
177
    /**
178
     * Filter Scope by category.
179
     *
180
     * @param $query
181
     * @param $category
182
     * @return mixed
183
     */
184
    public function scopeByCategory($query, $category)
185
    {
186
        if (is_numeric($category)) {
187
            return $query->where('category_id', $category);
188
        }
189
190
        return $query->whereHas('body', function ($categoryQuery) use ($category) {
191
            $categoryQuery->where('name', $category);
192
        });
193
    }
194
195
    /**
196
     * Get custom required fields from the configs
197
     * so that we can automatically bind them to the model
198
     * fillable property.
199
     *
200
     * @return mixed
201
     */
202
    public function getCustomFillableFields()
203
    {
204
        if (function_exists('app') && app() instanceof Container) {
205
            return Arr::flatten(config('notifynder.additional_fields', []));
206
        }
207
208
        return [];
209
    }
210
211
    /**
212
     * @return array
213
     */
214
    protected function mergeFillable()
215
    {
216
        $fillables = array_unique($this->getFillable() + $this->getCustomFillableFields());
217
218
        return $fillables;
219
    }
220
}
221