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
|
|
|
use Fenos\Notifynder\Models\Notification as NotificationModel; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class OnceSender. |
14
|
|
|
*/ |
15
|
|
|
class OnceSender implements SenderContract |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
protected $notifications; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* OnceSender constructor. |
24
|
|
|
* |
25
|
|
|
* @param array $notifications |
26
|
|
|
*/ |
27
|
|
|
public function __construct(array $notifications) |
28
|
|
|
{ |
29
|
|
|
$this->notifications = $notifications; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Send the notification once. |
34
|
|
|
* |
35
|
|
|
* @param SenderManagerContract $sender |
36
|
|
|
* @return bool |
37
|
|
|
*/ |
38
|
|
|
public function send(SenderManagerContract $sender) |
39
|
|
|
{ |
40
|
|
|
$success = true; |
41
|
|
|
foreach ($this->notifications as $notification) { |
42
|
|
|
$query = $this->getQuery($notification); |
43
|
|
|
if (! $query->exists()) { |
|
|
|
|
44
|
|
|
$success = $sender->send([$notification]) ? $success : false; |
45
|
|
|
continue; |
46
|
|
|
} |
47
|
|
|
$success = $query->firstOrFail()->resend() ? $success : false; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $success; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get the base query. |
55
|
|
|
* |
56
|
|
|
* @param Notification $notification |
57
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
58
|
|
|
*/ |
59
|
|
|
protected function getQuery(Notification $notification) |
60
|
|
|
{ |
61
|
|
|
$query = $this->getQueryInstance(); |
62
|
|
|
$query |
63
|
|
|
->where('from_id', $notification->from_id) |
|
|
|
|
64
|
|
|
->where('from_type', $notification->from_type) |
|
|
|
|
65
|
|
|
->where('to_id', $notification->to_id) |
|
|
|
|
66
|
|
|
->where('to_type', $notification->to_type) |
|
|
|
|
67
|
|
|
->where('category_id', $notification->category_id); |
|
|
|
|
68
|
|
|
$extra = $notification->extra; |
|
|
|
|
69
|
|
|
if (! is_null($extra) && ! empty($extra)) { |
70
|
|
|
if (is_array($extra)) { |
71
|
|
|
$extra = json_encode($extra); |
72
|
|
|
} |
73
|
|
|
$query->where('extra', $extra); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $query; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
81
|
|
|
*/ |
82
|
|
|
protected function getQueryInstance() |
83
|
|
|
{ |
84
|
|
|
$model = app('notifynder.resolver.model')->getModel(NotificationModel::class); |
85
|
|
|
$query = $model::query(); |
86
|
|
|
if (! ($query instanceof EloquentBuilder)) { |
87
|
|
|
throw new BadMethodCallException("The query method hasn't return an instance of [".EloquentBuilder::class.'].'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $query; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
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.