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