2013-12-25 22:34:42 +01:00
|
|
|
<?php
|
|
|
|
|
2014-01-29 11:41:38 +01:00
|
|
|
use ninja\mailers\UserMailer;
|
|
|
|
use ninja\mailers\ContactMailer;
|
2013-12-25 22:34:42 +01:00
|
|
|
|
|
|
|
class InvoiceEventHandler
|
|
|
|
{
|
2014-01-29 11:41:38 +01:00
|
|
|
protected $userMailer;
|
|
|
|
protected $contactMailer;
|
2013-12-25 22:34:42 +01:00
|
|
|
|
2014-01-29 11:41:38 +01:00
|
|
|
public function __construct(UserMailer $userMailer, ContactMailer $contactMailer)
|
2013-12-25 22:34:42 +01:00
|
|
|
{
|
2014-01-29 11:41:38 +01:00
|
|
|
$this->userMailer = $userMailer;
|
|
|
|
$this->contactMailer = $contactMailer;
|
2013-12-25 22:34:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function subscribe($events)
|
|
|
|
{
|
|
|
|
$events->listen('invoice.sent', 'InvoiceEventHandler@onSent');
|
|
|
|
$events->listen('invoice.viewed', 'InvoiceEventHandler@onViewed');
|
|
|
|
$events->listen('invoice.paid', 'InvoiceEventHandler@onPaid');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onSent($invoice)
|
|
|
|
{
|
|
|
|
$this->sendNotifications($invoice, 'sent');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onViewed($invoice)
|
|
|
|
{
|
2013-12-26 01:36:34 +01:00
|
|
|
$this->sendNotifications($invoice, 'viewed');
|
2013-12-25 22:34:42 +01:00
|
|
|
}
|
|
|
|
|
2014-01-29 11:41:38 +01:00
|
|
|
public function onPaid($payment)
|
2013-12-25 22:34:42 +01:00
|
|
|
{
|
2014-01-29 11:41:38 +01:00
|
|
|
$this->contactMailer->sendPaymentConfirmation($payment);
|
|
|
|
|
|
|
|
$this->sendNotifications($payment->invoice, 'paid', $payment);
|
2013-12-25 22:34:42 +01:00
|
|
|
}
|
|
|
|
|
2014-01-29 11:41:38 +01:00
|
|
|
private function sendNotifications($invoice, $type, $payment = null)
|
2013-12-25 22:34:42 +01:00
|
|
|
{
|
|
|
|
foreach ($invoice->account->users as $user)
|
|
|
|
{
|
|
|
|
if ($user->{'notify_' . $type})
|
|
|
|
{
|
2014-01-29 11:41:38 +01:00
|
|
|
$this->userMailer->sendNotification($user, $invoice, $type, $payment);
|
2013-12-25 22:34:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|