From 724fc92930432452fac94ac5dd19db23d94ef00a Mon Sep 17 00:00:00 2001 From: Virgil-Adrian Teaca Date: Fri, 26 Jul 2019 01:13:47 +0300 Subject: [PATCH] Improve the Notifications Service --- src/Notifications/ChannelManager.php | 8 +++++--- src/Notifications/NotificationSender.php | 19 +++++++++++-------- .../NotificationServiceProvider.php | 11 +++-------- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/Notifications/ChannelManager.php b/src/Notifications/ChannelManager.php index 735617fb..c1d03874 100644 --- a/src/Notifications/ChannelManager.php +++ b/src/Notifications/ChannelManager.php @@ -35,14 +35,16 @@ class ChannelManager extends Manager implements DispatcherInterface * Create a new manager instance. * * @param \Nova\Foundation\Application $app - * @param \Nova\Notifications\NotificationSender $sender + * @param \Nova\Events\Dispatcher $events + * @param \Nova\Bus\Dispatcher $bus * @return void */ - public function __construct(Application $app, NotificationSender $sender) + public function __construct(Application $app, EventDispatcher $events, BusDispatcher $bus) { $this->app = $app; - $this->sender = $sender; + // + $this->sender = new NotificationSender($this, $events, $bus); } /** diff --git a/src/Notifications/NotificationSender.php b/src/Notifications/NotificationSender.php index 48a37aac..2a11a0af 100644 --- a/src/Notifications/NotificationSender.php +++ b/src/Notifications/NotificationSender.php @@ -10,6 +10,7 @@ use Nova\Notifications\Events\NotificationSending; use Nova\Notifications\Events\NotificationSent; +use Nova\Notifications\ChannelManager; use Nova\Notifications\SendQueuedNotifications; use Ramsey\Uuid\Uuid; @@ -27,18 +28,18 @@ class NotificationSender protected $manager; /** - * The command bus dispatcher instance. + * The events dispatcher instance. * - * @var \Nova\Bus\Dispatcher + * @var \Nova\Events\Dispatcher */ - protected $bus; + protected $events; /** - * The events dispatcher instance. + * The command bus dispatcher instance. * - * @var \Nova\Events\Dispatcher + * @var \Nova\Bus\Dispatcher */ - protected $events; + protected $bus; /** @@ -48,8 +49,10 @@ class NotificationSender * @param \Nova\Bus\Dispatcher $bus * @return void */ - public function __construct(EventDispatcher $events, BusDispatcher $bus) + public function __construct(ChannelManager $manager, EventDispatcher $events, BusDispatcher $bus) { + $this->manager = $manager; + $this->events = $events; $this->bus = $bus; @@ -122,7 +125,7 @@ protected function sendToNotifiable($notifiable, $id, $notification, $channel) } if ($this->shouldSendNotification($notifiable, $notification, $channel)) { - $response = $this->driver($channel)->send($notifiable, $notification); + $response = $this->manager->channel($channel)->send($notifiable, $notification); $this->events->dispatch( new NotificationSent($notifiable, $notification, $channel, $response) diff --git a/src/Notifications/NotificationServiceProvider.php b/src/Notifications/NotificationServiceProvider.php index 12c930f0..0c2c1d3e 100644 --- a/src/Notifications/NotificationServiceProvider.php +++ b/src/Notifications/NotificationServiceProvider.php @@ -25,16 +25,11 @@ class NotificationServiceProvider extends ServiceProvider */ public function register() { - $this->app->singleton('notifications.sender', function ($app) + $this->app->singleton('notifications', function ($app) { $bus = $app->make(BusDispatcher::class); - return new NotificationSender($app['events'], $bus); - }); - - $this->app->singleton('notifications', function ($app) - { - return new ChannelManager($app, $app['notifications.sender']); + return new ChannelManager($app, $app['events'], $bus); }); } @@ -45,6 +40,6 @@ public function register() */ public function provides() { - return array('notifications', 'notifications.sender'); + return array('notifications'); } }