Use queues for notifications

This commit is contained in:
Hillel Coren 2017-01-11 15:40:13 +02:00
parent 71694d108d
commit 457dcf67bc
4 changed files with 131 additions and 69 deletions

View File

@ -0,0 +1,66 @@
<?php
namespace App\Jobs;
use App\Models\Payment;
use App\Ninja\Mailers\UserMailer;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Monolog\Logger;
use Carbon;
/**
* Class SendInvoiceEmail
*/
class SendNotificationEmail extends Job implements ShouldQueue
{
use InteractsWithQueue, SerializesModels;
/**
* @var User
*/
protected $user;
/**
* @var Invoice
*/
protected $invoice;
/**
* @var string
*/
protected $type;
/**
* @var Payment
*/
protected $payment;
/**
* Create a new job instance.
* @param UserMailer $userMailer
* @param ContactMailer $contactMailer
* @param PushService $pushService
*/
public function __construct($user, $invoice, $type, $payment)
{
$this->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);
}
}

View File

@ -4,11 +4,9 @@ namespace App\Jobs;
use App\Models\Payment; use App\Models\Payment;
use App\Ninja\Mailers\ContactMailer; use App\Ninja\Mailers\ContactMailer;
use App\Ninja\Mailers\UserMailer;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Queue\ShouldQueue;
use App\Services\PushService;
use Monolog\Logger; use Monolog\Logger;
use Carbon; use Carbon;
@ -28,9 +26,7 @@ class SendPaymentEmail extends Job implements ShouldQueue
/** /**
* Create a new job instance. * Create a new job instance.
* @param UserMailer $userMailer * @param Payment $payment
* @param ContactMailer $contactMailer
* @param PushService $pushService
*/ */
public function __construct($payment) public function __construct($payment)
{ {
@ -42,37 +38,10 @@ class SendPaymentEmail extends Job implements ShouldQueue
* *
* @param ContactMailer $mailer * @param ContactMailer $mailer
*/ */
public function handle(UserMailer $userMailer, ContactMailer $contactMailer, PushService $pushService) public function handle(ContactMailer $contactMailer)
{ {
$payment = $this->payment; $contactMailer->sendPaymentConfirmation($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');
} }
/**
* 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);
}
*/
} }

View File

@ -0,0 +1,52 @@
<?php
namespace App\Jobs;
use App\Models\Invoice;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Services\PushService;
use Monolog\Logger;
use Carbon;
/**
* Class SendInvoiceEmail
*/
class SendPushNotification extends Job implements ShouldQueue
{
use InteractsWithQueue, SerializesModels;
/**
* @var Invoice
*/
protected $invoice;
/**
* @var string
*/
protected $type;
/**
* Create a new job instance.
* @param Invoice $invoice
*/
public function __construct($invoice, $type)
{
$this->invoice = $invoice;
$this->type = $type;
}
/**
* Execute the job.
*
* @param PushService $pushService
*/
public function handle(PushService $pushService)
{
$pushService->sendNotification($this->invoice, $this->type);
}
}

View File

@ -1,47 +1,20 @@
<?php namespace App\Listeners; <?php namespace App\Listeners;
use App\Ninja\Mailers\UserMailer;
use App\Ninja\Mailers\ContactMailer;
use App\Events\InvoiceWasEmailed; use App\Events\InvoiceWasEmailed;
use App\Events\QuoteWasEmailed; use App\Events\QuoteWasEmailed;
use App\Events\InvoiceInvitationWasViewed; use App\Events\InvoiceInvitationWasViewed;
use App\Events\QuoteInvitationWasViewed; use App\Events\QuoteInvitationWasViewed;
use App\Events\QuoteInvitationWasApproved; use App\Events\QuoteInvitationWasApproved;
use App\Events\PaymentWasCreated; use App\Events\PaymentWasCreated;
use App\Services\PushService;
use App\Jobs\SendPaymentEmail; use App\Jobs\SendPaymentEmail;
use App\Jobs\SendNotificationEmail;
use App\Jobs\SendPushNotification;
/** /**
* Class NotificationListener * Class NotificationListener
*/ */
class NotificationListener class NotificationListener
{ {
/**
* @var UserMailer
*/
protected $userMailer;
/**
* @var ContactMailer
*/
protected $contactMailer;
/**
* @var PushService
*/
protected $pushService;
/**
* NotificationListener constructor.
* @param UserMailer $userMailer
* @param ContactMailer $contactMailer
* @param PushService $pushService
*/
public function __construct(UserMailer $userMailer, ContactMailer $contactMailer, PushService $pushService)
{
$this->userMailer = $userMailer;
$this->contactMailer = $contactMailer;
$this->pushService = $pushService;
}
/** /**
* @param $invoice * @param $invoice
* @param $type * @param $type
@ -53,7 +26,7 @@ class NotificationListener
{ {
if ($user->{"notify_{$type}"}) 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) public function emailedInvoice(InvoiceWasEmailed $event)
{ {
$this->sendEmails($event->invoice, 'sent'); $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) public function emailedQuote(QuoteWasEmailed $event)
{ {
$this->sendEmails($event->quote, 'sent'); $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->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->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) public function approvedQuote(QuoteInvitationWasApproved $event)
{ {
$this->sendEmails($event->quote, 'approved'); $this->sendEmails($event->quote, 'approved');
$this->pushService->sendNotification($event->quote, 'approved'); dispatch(new SendPushNotification($event->quote, 'approved'));
} }
/** /**
@ -121,7 +94,9 @@ class NotificationListener
return; return;
} }
$this->sendEmails($event->payment->invoice, 'paid', $event->payment);
dispatch(new SendPaymentEmail($event->payment)); dispatch(new SendPaymentEmail($event->payment));
dispatch(new SendPushNotification($event->payment->invoice, 'paid'));
} }
} }