2017-12-23 02:00:57 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Listeners;
|
|
|
|
|
2018-01-01 01:09:56 +08:00
|
|
|
use App\Events\UserRegistered;
|
2018-01-01 13:15:12 +08:00
|
|
|
use App\Events\UserStateChanged;
|
2017-12-31 10:40:32 +08:00
|
|
|
use App\Models\Enums\UserState;
|
2018-02-21 12:33:09 +08:00
|
|
|
use Illuminate\Support\Facades\Mail;
|
|
|
|
use Log;
|
2017-12-23 02:00:57 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle sending emails on different events
|
|
|
|
* @package App\Listeners
|
|
|
|
*/
|
2018-03-02 06:20:13 +08:00
|
|
|
class NotificationEvents
|
2017-12-23 02:00:57 +08:00
|
|
|
{
|
|
|
|
public function subscribe($events)
|
|
|
|
{
|
|
|
|
$events->listen(
|
2017-12-31 10:39:17 +08:00
|
|
|
\App\Events\UserRegistered::class,
|
2018-03-09 22:52:55 +08:00
|
|
|
'App\Listeners\NotificationEvents@onUserRegister'
|
2017-12-31 10:39:17 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
$events->listen(
|
|
|
|
\App\Events\UserStateChanged::class,
|
2018-03-09 22:52:55 +08:00
|
|
|
'App\Listeners\NotificationEvents@onUserStateChange'
|
2017-12-23 02:00:57 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-01-01 13:15:12 +08:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function mailerActive()
|
|
|
|
{
|
|
|
|
if (empty(config('mail.host'))) {
|
|
|
|
Log::info('No mail host specified!');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-01-01 13:26:10 +08:00
|
|
|
/**
|
|
|
|
* @param $to
|
|
|
|
* @param $email
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
protected function sendEmail($to, $email)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
return Mail::to($to)->send($email);
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
Log::error('Error sending email!');
|
|
|
|
Log::error($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-23 02:46:46 +08:00
|
|
|
/**
|
|
|
|
* Send an email when the user registered
|
|
|
|
* @param UserRegistered $event
|
|
|
|
*/
|
2017-12-23 02:00:57 +08:00
|
|
|
public function onUserRegister(UserRegistered $event)
|
|
|
|
{
|
2017-12-23 02:46:46 +08:00
|
|
|
Log::info('onUserRegister: '
|
|
|
|
. $event->user->pilot_id . ' is '
|
2017-12-31 10:40:32 +08:00
|
|
|
. UserState::label($event->user->state)
|
2017-12-23 02:46:46 +08:00
|
|
|
. ', sending active email');
|
|
|
|
|
2018-01-01 13:15:12 +08:00
|
|
|
if(!$this->mailerActive()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-31 10:39:17 +08:00
|
|
|
# First send the admin a notification
|
|
|
|
$admin_email = setting('general.admin_email');
|
2018-01-01 01:09:56 +08:00
|
|
|
Log::info('Sending admin notification email to "'.$admin_email.'"');
|
|
|
|
|
2017-12-31 10:39:17 +08:00
|
|
|
if (!empty($admin_email)) {
|
|
|
|
$email = new \App\Mail\Admin\UserRegistered($event->user);
|
2018-01-01 13:26:10 +08:00
|
|
|
$this->sendEmail($admin_email, $email);
|
2017-12-31 10:39:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
# Then notify the user
|
2017-12-31 10:40:32 +08:00
|
|
|
if($event->user->state === UserState::ACTIVE) {
|
2018-01-01 13:26:10 +08:00
|
|
|
$email = new \App\Mail\UserRegistered($event->user);
|
2017-12-31 10:40:32 +08:00
|
|
|
} else if($event->user->state === UserState::PENDING) {
|
2018-01-01 13:26:10 +08:00
|
|
|
$email = new \App\Mail\UserPending($event->user);
|
2017-12-23 02:46:46 +08:00
|
|
|
}
|
2018-01-01 13:26:10 +08:00
|
|
|
|
|
|
|
$this->sendEmail($event->user->email, $email);
|
2017-12-23 02:00:57 +08:00
|
|
|
}
|
|
|
|
|
2017-12-23 06:32:21 +08:00
|
|
|
/**
|
|
|
|
* When a user's state changes, send an email out
|
|
|
|
* @param UserStateChanged $event
|
|
|
|
*/
|
|
|
|
public function onUserStateChange(UserStateChanged $event)
|
|
|
|
{
|
2018-01-01 13:15:12 +08:00
|
|
|
if (!$this->mailerActive()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-31 10:40:32 +08:00
|
|
|
if ($event->old_state === UserState::PENDING) {
|
2018-01-01 13:26:10 +08:00
|
|
|
if ($event->user->state === UserState::ACTIVE) {
|
|
|
|
$email = new \App\Mail\UserRegistered($event->user,
|
|
|
|
'Your registration has been accepted!');
|
|
|
|
} else if ($event->user->state === UserState::REJECTED) {
|
2017-12-23 06:32:21 +08:00
|
|
|
$email = new \App\Mail\UserRejected($event->user);
|
|
|
|
}
|
2018-01-01 13:26:10 +08:00
|
|
|
$this->sendEmail($event->user->email, $email);
|
2017-12-23 06:32:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
# TODO: Other state transitions
|
2017-12-31 10:40:32 +08:00
|
|
|
elseif ($event->old_state === UserState::ACTIVE)
|
2017-12-23 06:32:21 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2017-12-23 02:00:57 +08:00
|
|
|
}
|