|
1
|
|
|
<?php |
|
2
|
|
|
namespace Fenos\Notifynder\Senders; |
|
3
|
|
|
|
|
4
|
|
|
use BadMethodCallException; |
|
5
|
|
|
use Fenos\Notifynder\Contracts\SenderContract; |
|
6
|
|
|
use Fenos\Notifynder\Contracts\SenderManagerContract; |
|
7
|
|
|
use Illuminate\Database\Eloquent\Builder as EloquentBuilder; |
|
8
|
|
|
|
|
9
|
|
|
class OnceSender implements SenderContract |
|
10
|
|
|
{ |
|
11
|
|
|
protected $notifications; |
|
12
|
|
|
|
|
13
|
|
|
public function __construct(array $notifications) |
|
14
|
|
|
{ |
|
15
|
|
|
$this->notifications = $notifications; |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
public function send(SenderManagerContract $sender) |
|
19
|
|
|
{ |
|
20
|
|
|
$model = notifynder_config()->getNotificationModel(); |
|
21
|
|
|
|
|
22
|
|
|
$success = true; |
|
23
|
|
|
foreach($this->notifications as $notification) { |
|
24
|
|
|
$query = $model::query(); |
|
25
|
|
|
if (!($query instanceof EloquentBuilder)) { |
|
26
|
|
|
throw new BadMethodCallException("The query method hasn't return an instance of the eloquent query builder."); |
|
27
|
|
|
} |
|
28
|
|
|
$query |
|
29
|
|
|
->where('from_id', $notification->from_id) |
|
30
|
|
|
->where('from_type', $notification->from_type) |
|
31
|
|
|
->where('to_id', $notification->to_id) |
|
32
|
|
|
->where('to_type', $notification->to_type) |
|
33
|
|
|
->where('category_id', $notification->category_id); |
|
34
|
|
|
if (isset($notification->extra) && !empty($notification->extra)) { |
|
35
|
|
|
$extra = $notification->extra; |
|
36
|
|
|
if(is_array($extra)) { |
|
37
|
|
|
$extra = json_encode($extra); |
|
38
|
|
|
} |
|
39
|
|
|
$query->where('extra', $extra); |
|
40
|
|
|
} |
|
41
|
|
|
if (!$query->exists()) { |
|
|
|
|
|
|
42
|
|
|
$success = $sender->send([$notification]) ? $success : false; |
|
43
|
|
|
} else { |
|
44
|
|
|
$notification = $query->first(); |
|
45
|
|
|
$notification->touch(); |
|
|
|
|
|
|
46
|
|
|
$notification->unread(); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
return $success; |
|
50
|
|
|
} |
|
51
|
|
|
} |
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.