-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotificationSender.php
More file actions
153 lines (130 loc) · 4.72 KB
/
NotificationSender.php
File metadata and controls
153 lines (130 loc) · 4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
declare(strict_types=1);
/**
* This file is part of friendsofhyperf/components.
*
* @link https://github.com/friendsofhyperf/components
* @document https://github.com/friendsofhyperf/components/blob/main/README.md
* @contact [email protected]
*/
namespace FriendsOfHyperf\Notification;
use Closure;
use FriendsOfHyperf\Notification\Event\NotificationSending;
use FriendsOfHyperf\Notification\Event\NotificationSent;
use Hyperf\Collection\Collection;
use Hyperf\Contract\TranslatorInterface;
use Hyperf\Database\Model\Collection as ModelCollection;
use Hyperf\Database\Model\Model;
use Hyperf\Stringable\Str;
use Psr\EventDispatcher\EventDispatcherInterface;
use function Hyperf\Support\value;
/**
* @property null|string $locale
*/
class NotificationSender
{
public function __construct(
public ChannelManager $channelManager,
public EventDispatcherInterface $dispatcher,
public TranslatorInterface $translator,
) {
}
/**
* Send the given notification immediately.
*/
public function send(mixed $notifiables, Notification $notification, ?array $channels = null): void
{
$notifiables = $this->formatNotifiables($notifiables);
$original = clone $notification;
foreach ($notifiables as $notifiable) {
$viaChannels = value(function ($channels, $notification, $notifiable) {
if ($channels) {
return $channels;
}
if (method_exists($notification, 'via')) {
return $notification->via($notifiable);
}
return null;
}, $channels, $notification, $notifiable);
if ($viaChannels === null) {
continue;
}
$this->withLocale($this->preferredLocale($notifiable, $notification), function () use ($viaChannels, $notifiable, $original) {
$notificationId = Str::uuid()->toString();
foreach ((array) $viaChannels as $channel) {
if (! ($notifiable instanceof AnonymousNotifiable && $channel === 'database')) {
$this->sendToNotifiable($notifiable, $notificationId, clone $original, $channel);
}
}
});
}
}
/**
* Run the callback with the given locale.
*/
public function withLocale(?string $locale, Closure $callback): mixed
{
if (! $locale) {
return $callback();
}
$original = $this->translator->getLocale();
try {
$this->translator->setLocale($locale);
return $callback();
} finally {
$this->translator->setLocale($original);
}
}
/**
* Send the given notification to the given notifiable via a channel.
*/
protected function sendToNotifiable(mixed $notifiable, string $id, Notification $notification, string $channel): void
{
if (! $notification->id) {
$notification->id = $id;
}
if (! $this->shouldSendNotification($notifiable, $notification, $channel)) {
return;
}
$response = $this->channelManager->channel($channel)->send($notifiable, $notification);
$this->dispatcher->dispatch(
new NotificationSent($notifiable, $notification, $channel, $response)
);
}
/**
* Get the notifiable's preferred locale for the notification.
*/
protected function preferredLocale(mixed $notifiable, Notification $notification): ?string
{
return $notification->locale ?? $this->locale ?? value(function () use ($notifiable) {
if (method_exists($notifiable, 'preferredLocale')) {
return $notifiable->preferredLocale();
}
return null;
});
}
/**
* Determines if the notification can be sent.
*/
protected function shouldSendNotification(mixed $notifiable, Notification $notification, string $channel): bool
{
if (method_exists($notification, 'shouldSend')
&& $notification->shouldSend($notifiable, $channel) === false) {
return false;
}
$event = new NotificationSending($notifiable, $notification, $channel);
$this->dispatcher->dispatch($event);
return $event->shouldSend();
}
/**
* Format the notifiables into a Collection / array if necessary.
*/
protected function formatNotifiables(mixed $notifiables): Collection|array
{
if (! $notifiables instanceof Collection && ! is_array($notifiables)) {
return $notifiables instanceof Model
? new ModelCollection([$notifiables]) : [$notifiables];
}
return $notifiables;
}
}