From 1d155641f8c3d50043779f1842bdd7b29605ab56 Mon Sep 17 00:00:00 2001 From: Meysam Valiolahi Date: Mon, 29 Nov 2021 15:03:32 +0330 Subject: [PATCH 1/3] Customize type column in database notifications using databaseType method. --- .../Channels/DatabaseChannel.php | 17 +++++++++- .../NotificationDatabaseChannelTest.php | 31 +++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Notifications/Channels/DatabaseChannel.php b/src/Illuminate/Notifications/Channels/DatabaseChannel.php index bd8af623144f..a74dc9c854dd 100644 --- a/src/Illuminate/Notifications/Channels/DatabaseChannel.php +++ b/src/Illuminate/Notifications/Channels/DatabaseChannel.php @@ -55,9 +55,24 @@ protected function buildPayload($notifiable, Notification $notification) { return [ 'id' => $notification->id, - 'type' => get_class($notification), + 'type' => $this->chooseType($notification), 'data' => $this->getData($notifiable, $notification), 'read_at' => null, ]; } + + /** + * Use databaseType method or the notification class name for the type column. + * + * @param mixed $notification + * @return string + */ + private function chooseType($notification) + { + if (method_exists($notification, 'databaseType')) { + return $notification->databaseType(); + } + + return get_class($notification); + } } diff --git a/tests/Notifications/NotificationDatabaseChannelTest.php b/tests/Notifications/NotificationDatabaseChannelTest.php index 9abcedd5393a..3a45a65a3fc5 100644 --- a/tests/Notifications/NotificationDatabaseChannelTest.php +++ b/tests/Notifications/NotificationDatabaseChannelTest.php @@ -49,6 +49,24 @@ public function testCorrectPayloadIsSentToDatabase() $channel = new ExtendedDatabaseChannel; $channel->send($notifiable, $notification); } + + public function testCustomizeTypeIsSentToDatabase() + { + $notification = new NotificationDatabaseChannelCustomizeTypeTestNotification; + $notification->id = 1; + $notifiable = m::mock(); + + $notifiable->shouldReceive('routeNotificationFor->create')->with([ + 'id' => 1, + 'type' => 'MONTHLY', + 'data' => ['invoice_id' => 1], + 'read_at' => null, + 'something' => 'else', + ]); + + $channel = new ExtendedDatabaseChannel; + $channel->send($notifiable, $notification); + } } class NotificationDatabaseChannelTestNotification extends Notification @@ -59,6 +77,19 @@ public function toDatabase($notifiable) } } +class NotificationDatabaseChannelCustomizeTypeTestNotification extends Notification +{ + public function toDatabase($notifiable) + { + return new DatabaseMessage(['invoice_id' => 1]); + } + + public function databaseType() + { + return 'MONTHLY'; + } +} + class ExtendedDatabaseChannel extends DatabaseChannel { protected function buildPayload($notifiable, Notification $notification) From 2b92664cc5e66b52c7c5f8c8ca8bc8b543d4695b Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Mon, 29 Nov 2021 16:09:20 -0600 Subject: [PATCH 2/3] Update DatabaseChannel.php --- .../Notifications/Channels/DatabaseChannel.php | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/Illuminate/Notifications/Channels/DatabaseChannel.php b/src/Illuminate/Notifications/Channels/DatabaseChannel.php index a74dc9c854dd..5c4bce050aaf 100644 --- a/src/Illuminate/Notifications/Channels/DatabaseChannel.php +++ b/src/Illuminate/Notifications/Channels/DatabaseChannel.php @@ -55,24 +55,22 @@ protected function buildPayload($notifiable, Notification $notification) { return [ 'id' => $notification->id, - 'type' => $this->chooseType($notification), + 'type' => $this->storageType($notification), 'data' => $this->getData($notifiable, $notification), 'read_at' => null, ]; } /** - * Use databaseType method or the notification class name for the type column. + * Determine the identifier that should be used to represent the notification type in the database. * * @param mixed $notification * @return string */ - private function chooseType($notification) + protected function storageType($notification) { - if (method_exists($notification, 'databaseType')) { - return $notification->databaseType(); - } - - return get_class($notification); + return method_exists($notification, 'databaseType') + ? $notification->databaseType() + : get_class($notification); } } From 68398e104617ad69191c4571257df40bfc99a5f3 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Mon, 29 Nov 2021 16:13:33 -0600 Subject: [PATCH 3/3] formatting --- .../Channels/DatabaseChannel.php | 49 +++++++------------ 1 file changed, 19 insertions(+), 30 deletions(-) diff --git a/src/Illuminate/Notifications/Channels/DatabaseChannel.php b/src/Illuminate/Notifications/Channels/DatabaseChannel.php index 5c4bce050aaf..8b3167b01508 100644 --- a/src/Illuminate/Notifications/Channels/DatabaseChannel.php +++ b/src/Illuminate/Notifications/Channels/DatabaseChannel.php @@ -21,6 +21,25 @@ public function send($notifiable, Notification $notification) ); } + /** + * Build an array payload for the DatabaseNotification Model. + * + * @param mixed $notifiable + * @param \Illuminate\Notifications\Notification $notification + * @return array + */ + protected function buildPayload($notifiable, Notification $notification) + { + return [ + 'id' => $notification->id, + 'type' => method_exists($notification, 'databaseType') + ? $notification->databaseType($notifiable) + : get_class($notification), + 'data' => $this->getData($notifiable, $notification), + 'read_at' => null, + ]; + } + /** * Get the data for the notification. * @@ -43,34 +62,4 @@ protected function getData($notifiable, Notification $notification) throw new RuntimeException('Notification is missing toDatabase / toArray method.'); } - - /** - * Build an array payload for the DatabaseNotification Model. - * - * @param mixed $notifiable - * @param \Illuminate\Notifications\Notification $notification - * @return array - */ - protected function buildPayload($notifiable, Notification $notification) - { - return [ - 'id' => $notification->id, - 'type' => $this->storageType($notification), - 'data' => $this->getData($notifiable, $notification), - 'read_at' => null, - ]; - } - - /** - * Determine the identifier that should be used to represent the notification type in the database. - * - * @param mixed $notification - * @return string - */ - protected function storageType($notification) - { - return method_exists($notification, 'databaseType') - ? $notification->databaseType() - : get_class($notification); - } }