From 457dcf67bcefe53c1331bfd847d0ab0224c461b4 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Wed, 11 Jan 2017 15:40:13 +0200 Subject: [PATCH] Use queues for notifications --- app/Jobs/SendNotificationEmail.php | 66 ++++++++++++++++++++++++++ app/Jobs/SendPaymentEmail.php | 37 ++------------- app/Jobs/SendPushNotification.php | 52 ++++++++++++++++++++ app/Listeners/NotificationListener.php | 45 ++++-------------- 4 files changed, 131 insertions(+), 69 deletions(-) create mode 100644 app/Jobs/SendNotificationEmail.php create mode 100644 app/Jobs/SendPushNotification.php diff --git a/app/Jobs/SendNotificationEmail.php b/app/Jobs/SendNotificationEmail.php new file mode 100644 index 000000000000..32eaa6c42de0 --- /dev/null +++ b/app/Jobs/SendNotificationEmail.php @@ -0,0 +1,66 @@ +user = $user; + $this->invoice = $invoice; + $this->type = $type; + $this->payment = $payment; + } + + /** + * Execute the job. + * + * @param ContactMailer $mailer + */ + public function handle(UserMailer $userMailer) + { + $userMailer->sendNotification($this->user, $this->invoice, $this->type, $this->payment); + } + +} diff --git a/app/Jobs/SendPaymentEmail.php b/app/Jobs/SendPaymentEmail.php index 23a1d41ee8e8..c27c0f5f44ad 100644 --- a/app/Jobs/SendPaymentEmail.php +++ b/app/Jobs/SendPaymentEmail.php @@ -4,11 +4,9 @@ namespace App\Jobs; use App\Models\Payment; use App\Ninja\Mailers\ContactMailer; -use App\Ninja\Mailers\UserMailer; use Illuminate\Queue\SerializesModels; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Contracts\Queue\ShouldQueue; -use App\Services\PushService; use Monolog\Logger; use Carbon; @@ -28,9 +26,7 @@ class SendPaymentEmail extends Job implements ShouldQueue /** * Create a new job instance. - * @param UserMailer $userMailer - * @param ContactMailer $contactMailer - * @param PushService $pushService + * @param Payment $payment */ public function __construct($payment) { @@ -42,37 +38,10 @@ class SendPaymentEmail extends Job implements ShouldQueue * * @param ContactMailer $mailer */ - public function handle(UserMailer $userMailer, ContactMailer $contactMailer, PushService $pushService) + public function handle(ContactMailer $contactMailer) { - $payment = $this->payment; - $invoice = $payment->invoice; - - $contactMailer->sendPaymentConfirmation($payment); - - $payment->account->load('users'); - foreach ($payment->account->users as $user) - { - if ($user->notify_paid) - { - $userMailer->sendNotification($user, $invoice, 'paid', $payment); - } - } - - $pushService->sendNotification($invoice, 'paid'); + $contactMailer->sendPaymentConfirmation($this->payment); } - /** - * Handle a job failure. - * - * @param ContactMailer $mailer - * @param Logger $logger - */ - /* - public function failed(ContactMailer $mailer, Logger $logger) - { - $this->jobName = $this->job->getName(); - parent::failed($mailer, $logger); - } - */ } diff --git a/app/Jobs/SendPushNotification.php b/app/Jobs/SendPushNotification.php new file mode 100644 index 000000000000..a9774e1f084c --- /dev/null +++ b/app/Jobs/SendPushNotification.php @@ -0,0 +1,52 @@ +invoice = $invoice; + $this->type = $type; + } + + /** + * Execute the job. + * + * @param PushService $pushService + */ + public function handle(PushService $pushService) + { + $pushService->sendNotification($this->invoice, $this->type); + } + +} diff --git a/app/Listeners/NotificationListener.php b/app/Listeners/NotificationListener.php index bd993a3701d7..8772ef02e0ff 100644 --- a/app/Listeners/NotificationListener.php +++ b/app/Listeners/NotificationListener.php @@ -1,47 +1,20 @@ userMailer = $userMailer; - $this->contactMailer = $contactMailer; - $this->pushService = $pushService; - } - /** * @param $invoice * @param $type @@ -53,7 +26,7 @@ class NotificationListener { if ($user->{"notify_{$type}"}) { - $this->userMailer->sendNotification($user, $invoice, $type, $payment); + dispatch(new SendNotificationEmail($user, $invoice, $type, $payment)); } } } @@ -64,7 +37,7 @@ class NotificationListener public function emailedInvoice(InvoiceWasEmailed $event) { $this->sendEmails($event->invoice, 'sent'); - $this->pushService->sendNotification($event->invoice, 'sent'); + dispatch(new SendPushNotification($event->invoice, 'sent')); } /** @@ -73,7 +46,7 @@ class NotificationListener public function emailedQuote(QuoteWasEmailed $event) { $this->sendEmails($event->quote, 'sent'); - $this->pushService->sendNotification($event->quote, 'sent'); + dispatch(new SendPushNotification($event->quote, 'sent')); } /** @@ -86,7 +59,7 @@ class NotificationListener } $this->sendEmails($event->invoice, 'viewed'); - $this->pushService->sendNotification($event->invoice, 'viewed'); + dispatch(new SendPushNotification($event->invoice, 'viewed')); } /** @@ -99,7 +72,7 @@ class NotificationListener } $this->sendEmails($event->quote, 'viewed'); - $this->pushService->sendNotification($event->quote, 'viewed'); + dispatch(new SendPushNotification($event->quote, 'viewed')); } /** @@ -108,7 +81,7 @@ class NotificationListener public function approvedQuote(QuoteInvitationWasApproved $event) { $this->sendEmails($event->quote, 'approved'); - $this->pushService->sendNotification($event->quote, 'approved'); + dispatch(new SendPushNotification($event->quote, 'approved')); } /** @@ -121,7 +94,9 @@ class NotificationListener return; } + $this->sendEmails($event->payment->invoice, 'paid', $event->payment); dispatch(new SendPaymentEmail($event->payment)); + dispatch(new SendPushNotification($event->payment->invoice, 'paid')); } }