2017-12-23 02:00:57 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Listeners;
|
|
|
|
|
2017-12-23 06:32:21 +08:00
|
|
|
use App\Events\UserStateChanged;
|
2017-12-23 02:00:57 +08:00
|
|
|
use Log;
|
2017-12-23 02:46:46 +08:00
|
|
|
use Illuminate\Support\Facades\Mail;
|
2017-12-23 02:00:57 +08:00
|
|
|
|
2017-12-23 02:46:46 +08:00
|
|
|
use App\Models\Enums\PilotState;
|
|
|
|
use \App\Events\UserRegistered;
|
2017-12-23 02:00:57 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle sending emails on different events
|
|
|
|
* @package App\Listeners
|
|
|
|
*/
|
|
|
|
class EmailEventListener
|
|
|
|
{
|
|
|
|
public function subscribe($events)
|
|
|
|
{
|
|
|
|
$events->listen(
|
|
|
|
UserRegistered::class,
|
|
|
|
'App\Listeners\EmailEventListener@onUserRegister'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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 '
|
|
|
|
. PilotState::label($event->user->state)
|
|
|
|
. ', sending active email');
|
|
|
|
|
|
|
|
if($event->user->state === PilotState::ACTIVE) {
|
2017-12-23 06:32:21 +08:00
|
|
|
$email = new \App\Mail\UserRegistered(
|
|
|
|
$event->user,
|
|
|
|
'Welcome to ' . config('app.name') . '!'
|
|
|
|
);
|
|
|
|
|
|
|
|
Mail::to($event->user->email)->send($email);
|
2017-12-23 02:46:46 +08:00
|
|
|
} else if($event->user->state === PilotState::PENDING) {
|
|
|
|
Mail::to($event->user->email)->send(new \App\Mail\UserPending($event->user));
|
|
|
|
}
|
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)
|
|
|
|
{
|
|
|
|
if ($event->old_state === PilotState::PENDING) {
|
|
|
|
if ($event->user->state === PilotState::ACTIVE)
|
|
|
|
{
|
|
|
|
$email = new \App\Mail\UserRegistered(
|
|
|
|
$event->user,
|
|
|
|
'Your registration has been accepted!'
|
|
|
|
);
|
|
|
|
|
|
|
|
Mail::to($event->user->email)->send($email);
|
|
|
|
}
|
|
|
|
|
|
|
|
else if ($event->user->state === PilotState::REJECTED)
|
|
|
|
{
|
|
|
|
$email = new \App\Mail\UserRejected($event->user);
|
|
|
|
Mail::to($event->user->email)->send($email);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# TODO: Other state transitions
|
|
|
|
elseif ($event->old_state === PilotState::ACTIVE)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2017-12-23 02:00:57 +08:00
|
|
|
}
|