Skip to content

Commit 5459402

Browse files
added ability to set middleware based on notifiable instance and channel (#44767)
* added ability to set middleware based on notifiable instance and channel * fixed style issues * Update NotificationSender.php Co-authored-by: Taylor Otwell <[email protected]>
1 parent 05ffb44 commit 5459402

File tree

2 files changed

+117
-6
lines changed

2 files changed

+117
-6
lines changed

src/Illuminate/Notifications/NotificationSender.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -211,17 +211,21 @@ protected function queueNotification($notifiables, $notification)
211211
$delay = $notification->withDelay($notifiable, $channel) ?? null;
212212
}
213213

214+
$middleware = $notification->middleware ?? [];
215+
216+
if (method_exists($notification, 'middleware')) {
217+
$middleware = array_merge(
218+
$notification->middleware($notifiable, $channel),
219+
$middleware
220+
);
221+
}
222+
214223
$this->bus->dispatch(
215224
(new SendQueuedNotifications($notifiable, $notification, [$channel]))
216225
->onConnection($notification->connection)
217226
->onQueue($queue)
218227
->delay(is_array($delay) ? ($delay[$channel] ?? null) : $delay)
219-
->through(
220-
array_merge(
221-
method_exists($notification, 'middleware') ? $notification->middleware() : [],
222-
$notification->middleware ?? []
223-
)
224-
)
228+
->through($middleware)
225229
);
226230
}
227231
}

tests/Notifications/NotificationSenderTest.php

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,49 @@ public function testItCannotSendNotificationsViaDatabaseForAnonymousNotifiables(
6161

6262
$sender->sendNow($notifiable, new DummyNotificationWithDatabaseVia);
6363
}
64+
65+
public function testItCanSendQueuedNotificationsThroughMiddleware()
66+
{
67+
$notifiable = m::mock(Notifiable::class);
68+
$manager = m::mock(ChannelManager::class);
69+
$bus = m::mock(BusDispatcher::class);
70+
$bus->shouldReceive('dispatch')
71+
->withArgs(function ($job) {
72+
return $job->middleware[0] instanceof TestNotificationMiddleware;
73+
});
74+
$events = m::mock(EventDispatcher::class);
75+
76+
$sender = new NotificationSender($manager, $bus, $events);
77+
78+
$sender->send($notifiable, new DummyNotificationWithMiddleware);
79+
}
80+
81+
public function testItCanSendQueuedMultiChannelNotificationsThroughDifferentMiddleware()
82+
{
83+
$notifiable = m::mock(Notifiable::class);
84+
$manager = m::mock(ChannelManager::class);
85+
$bus = m::mock(BusDispatcher::class);
86+
$bus->shouldReceive('dispatch')
87+
->once()
88+
->withArgs(function ($job) {
89+
return $job->middleware[0] instanceof TestMailNotificationMiddleware;
90+
});
91+
$bus->shouldReceive('dispatch')
92+
->once()
93+
->withArgs(function ($job) {
94+
return $job->middleware[0] instanceof TestDatabaseNotificationMiddleware;
95+
});
96+
$bus->shouldReceive('dispatch')
97+
->once()
98+
->withArgs(function ($job) {
99+
return empty($job->middleware);
100+
});
101+
$events = m::mock(EventDispatcher::class);
102+
103+
$sender = new NotificationSender($manager, $bus, $events);
104+
105+
$sender->send($notifiable, new DummyMultiChannelNotificationWithConditionalMiddlware);
106+
}
64107
}
65108

66109
class DummyQueuedNotificationWithStringVia extends Notification implements ShouldQueue
@@ -110,3 +153,67 @@ public function via($notifiable)
110153
return 'database';
111154
}
112155
}
156+
157+
class DummyNotificationWithMiddleware extends Notification implements ShouldQueue
158+
{
159+
use Queueable;
160+
161+
public function via($notifiable)
162+
{
163+
return 'mail';
164+
}
165+
166+
public function middleware()
167+
{
168+
return [
169+
new TestNotificationMiddleware,
170+
];
171+
}
172+
}
173+
174+
class DummyMultiChannelNotificationWithConditionalMiddlware extends Notification implements ShouldQueue
175+
{
176+
use Queueable;
177+
178+
public function via($notifiable)
179+
{
180+
return [
181+
'mail',
182+
'database',
183+
'broadcast',
184+
];
185+
}
186+
187+
public function middleware($notifiable, $channel)
188+
{
189+
return match ($channel) {
190+
'mail' => [new TestMailNotificationMiddleware],
191+
'database' => [new TestDatabaseNotificationMiddleware],
192+
default => []
193+
};
194+
}
195+
}
196+
197+
class TestNotificationMiddleware
198+
{
199+
public function handle($command, $next)
200+
{
201+
return $next($command);
202+
}
203+
}
204+
205+
class TestMailNotificationMiddleware
206+
{
207+
public function handle($command, $next)
208+
{
209+
return $next($command);
210+
}
211+
}
212+
213+
class TestDatabaseNotificationMiddleware
214+
{
215+
public function handle($command, $next)
216+
{
217+
return $next($command);
218+
}
219+
}

0 commit comments

Comments
 (0)