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