These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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\Collection; |
||
9 | use Illuminate\Database\Eloquent\Model; |
||
10 | use Carbon\Carbon; |
||
11 | use Illuminate\Support\Arr; |
||
12 | |||
13 | /** |
||
14 | * Class Notification. |
||
15 | * |
||
16 | * @property int to_id |
||
17 | * @property string to_type |
||
18 | * @property int from_id |
||
19 | * @property string from_type |
||
20 | * @property int category_id |
||
21 | * @property int read |
||
22 | * @property string url |
||
23 | * @property string extra |
||
24 | * |
||
25 | * Php spec complain when model is mocked |
||
26 | * if I turn them on as php doc block |
||
27 | * |
||
28 | * @method wherePolymorphic |
||
29 | * @method withNotRead |
||
30 | */ |
||
31 | class Notification extends Model |
||
32 | { |
||
33 | /** |
||
34 | * @var array |
||
35 | */ |
||
36 | protected $fillable = [ |
||
37 | 'to_id', |
||
38 | 'to_type', |
||
39 | 'from_id', |
||
40 | 'from_type', |
||
41 | 'category_id', |
||
42 | 'read', |
||
43 | 'url', |
||
44 | 'extra', |
||
45 | 'expire_time', |
||
46 | 'stack_id', |
||
47 | ]; |
||
48 | |||
49 | /** |
||
50 | * Notification constructor. |
||
51 | * |
||
52 | * @param array $attributes |
||
53 | */ |
||
54 | public function __construct(array $attributes = []) |
||
55 | { |
||
56 | $fillables = $this->mergeFillable(); |
||
57 | $this->fillable($fillables); |
||
58 | |||
59 | parent::__construct($attributes); |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * Custom Collection. |
||
64 | * |
||
65 | * @param array $models |
||
66 | * @return NotifynderCollection|\Illuminate\Database\Eloquent\Collection |
||
67 | */ |
||
68 | public function newCollection(array $models = []) |
||
69 | { |
||
70 | return new NotifynderCollection($models); |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
75 | */ |
||
76 | public function body() |
||
77 | { |
||
78 | return $this->belongsTo(NotificationCategory::class, 'category_id'); |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo|\Illuminate\Database\Eloquent\Relations\MorphTo |
||
83 | */ |
||
84 | public function from() |
||
85 | { |
||
86 | // check if on the configurations file there is the option |
||
87 | // polymorphic set to true, if so Notifynder will work |
||
88 | // polymorphic. |
||
89 | if (config('notifynder.polymorphic') == false) { |
||
90 | return $this->belongsTo(config('notifynder.model'), 'from_id'); |
||
91 | } |
||
92 | |||
93 | return $this->morphTo(); |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo|\Illuminate\Database\Eloquent\Relations\MorphTo |
||
98 | */ |
||
99 | public function to() |
||
100 | { |
||
101 | // check if on the configurations file there is the option |
||
102 | // polymorphic set to true, if so Notifynder will work |
||
103 | // polymorphic. |
||
104 | if (config('notifynder.polymorphic') == false) { |
||
105 | return $this->belongsTo(config('notifynder.model'), 'to_id'); |
||
106 | } |
||
107 | |||
108 | return $this->morphTo(); |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * Not read scope. |
||
113 | * |
||
114 | * @param $query |
||
115 | * @return mixed |
||
116 | */ |
||
117 | public function scopeWithNotRead($query) |
||
118 | { |
||
119 | return $query->where('read', 0); |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * Only Expired Notification scope. |
||
124 | * |
||
125 | * @param $query |
||
126 | * @return mixed |
||
127 | */ |
||
128 | public function scopeOnlyExpired($query) |
||
129 | { |
||
130 | return $query->where('expire_time', '<', Carbon::now()); |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * Where Polymorphic. |
||
135 | * |
||
136 | * @param $query |
||
137 | * @param $toId |
||
138 | * @param $type |
||
139 | * @return mixed |
||
140 | */ |
||
141 | public function scopeWherePolymorphic($query, $toId, $type) |
||
142 | { |
||
143 | if (! $type or config('notifynder.polymorphic') === false) { |
||
144 | return $query->where('to_id', $toId); |
||
145 | } |
||
146 | |||
147 | return $query->where('to_id', $toId) |
||
148 | ->where('to_type', $type); |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * Get parsed body attributes. |
||
153 | * |
||
154 | * @return string |
||
155 | */ |
||
156 | public function getNotifyBodyAttribute() |
||
157 | { |
||
158 | $notifynderParse = new NotifynderParser(); |
||
159 | |||
160 | return $notifynderParse->parse($this); |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * Get parsed body attributes. |
||
165 | * |
||
166 | * @return string |
||
167 | */ |
||
168 | public function getTextAttribute() |
||
169 | { |
||
170 | return $this->notify_body; |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * @param $value |
||
175 | * @return \Fenos\Notifynder\Notifications\ExtraParams |
||
176 | */ |
||
177 | public function getExtraAttribute($value) |
||
178 | { |
||
179 | if (! empty($value)) { |
||
180 | return new ExtraParams($value); |
||
181 | } |
||
182 | |||
183 | return new ExtraParams([]); |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * Filter Scope by category. |
||
188 | * |
||
189 | * @param $query |
||
190 | * @param $category |
||
191 | * @return mixed |
||
192 | */ |
||
193 | public function scopeByCategory($query, $category) |
||
194 | { |
||
195 | if (is_numeric($category)) { |
||
196 | return $query->where('category_id', $category); |
||
197 | } |
||
198 | |||
199 | return $query->whereHas('body', function ($categoryQuery) use ($category) { |
||
200 | $categoryQuery->where('name', $category); |
||
201 | }); |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * Get custom required fields from the configs |
||
206 | * so that we can automatically bind them to the model |
||
207 | * fillable property. |
||
208 | * |
||
209 | * @return mixed |
||
210 | */ |
||
211 | public function getCustomFillableFields() |
||
212 | { |
||
213 | if (function_exists('app') && app() instanceof Container) { |
||
214 | return Arr::flatten(config('notifynder.additional_fields', [])); |
||
215 | } |
||
216 | |||
217 | return []; |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * @return array |
||
222 | */ |
||
223 | protected function mergeFillable() |
||
224 | { |
||
225 | $fillables = array_unique($this->getFillable() + $this->getCustomFillableFields()); |
||
226 | |||
227 | return $fillables; |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * Filter Scope by stack. |
||
232 | * |
||
233 | * @param $query |
||
234 | * @param $stackId |
||
235 | * @return mixed |
||
236 | */ |
||
237 | public function scopeByStack($query, $stackId) |
||
238 | { |
||
239 | return $query->where('stack_id', $stackId); |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * Check if this notification is part of a stack. |
||
244 | * |
||
245 | * @return bool |
||
246 | */ |
||
247 | public function hasStack() |
||
248 | { |
||
249 | return ! is_null($this->stack_id); |
||
0 ignored issues
–
show
|
|||
250 | } |
||
251 | |||
252 | /** |
||
253 | * Get the full stack of notifications if this has one. |
||
254 | * |
||
255 | * @return null|Collection |
||
256 | */ |
||
257 | public function getStack() |
||
258 | { |
||
259 | if ($this->hasStack()) { |
||
260 | return static::byStack($this->stack_id)->get(); |
||
0 ignored issues
–
show
The property
stack_id 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 <?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...
The method
byStack() does not exist on Fenos\Notifynder\Models\Notification . Did you maybe mean scopeByStack() ?
This check marks calls to methods that do not seem to exist on an object. This is most likely the result of a method being renamed without all references to it being renamed likewise.
Loading history...
|
|||
261 | } |
||
262 | } |
||
263 | } |
||
264 |
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.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.