2017-12-23 02:00:57 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Listeners;
|
|
|
|
|
2019-07-16 03:51:35 +08:00
|
|
|
use App\Contracts\Listener;
|
2019-08-31 03:59:17 +08:00
|
|
|
use App\Events\PirepAccepted;
|
|
|
|
use App\Events\PirepFiled;
|
|
|
|
use App\Events\PirepRejected;
|
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;
|
2019-08-31 03:59:17 +08:00
|
|
|
use App\Models\User;
|
|
|
|
use App\Notifications\PirepSubmitted;
|
2018-03-20 09:50:40 +08:00
|
|
|
use Illuminate\Contracts\Events\Dispatcher;
|
2019-08-31 03:59:17 +08:00
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
use Illuminate\Support\Facades\Notification;
|
2017-12-23 02:00:57 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle sending emails on different events
|
|
|
|
*/
|
2018-03-20 09:50:40 +08:00
|
|
|
class NotificationEvents extends Listener
|
2017-12-23 02:00:57 +08:00
|
|
|
{
|
2018-03-20 09:50:40 +08:00
|
|
|
/**
|
|
|
|
* @param Dispatcher $events
|
|
|
|
*/
|
|
|
|
public function subscribe(Dispatcher $events): void
|
2017-12-23 02:00:57 +08:00
|
|
|
{
|
2019-08-31 03:59:17 +08:00
|
|
|
$events->listen(UserRegistered::class, 'App\Listeners\NotificationEvents@onUserRegister');
|
|
|
|
$events->listen(UserStateChanged::class, 'App\Listeners\NotificationEvents@onUserStateChange');
|
|
|
|
$events->listen(PirepFiled::class, 'App\Listeners\NotificationEvents@onPirepFile');
|
|
|
|
$events->listen(PirepAccepted::class, 'App\Listeners\NotificationEvents@onPirepAccepted');
|
|
|
|
$events->listen(PirepRejected::class, 'App\Listeners\NotificationEvents@onPirepRejected');
|
2017-12-23 02:00:57 +08:00
|
|
|
}
|
|
|
|
|
2018-01-01 13:15:12 +08:00
|
|
|
/**
|
2019-08-31 03:59:17 +08:00
|
|
|
* Send a notification to all of the admins
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2019-08-31 03:59:17 +08:00
|
|
|
* @param \Illuminate\Notifications\Notification $notification
|
2018-01-01 13:26:10 +08:00
|
|
|
*/
|
2019-08-31 03:59:17 +08:00
|
|
|
protected function notifyAdmins($notification)
|
2018-01-01 13:26:10 +08:00
|
|
|
{
|
2019-08-31 03:59:17 +08:00
|
|
|
$admin_users = User::whereRoleIs('admin')->get();
|
2019-09-18 07:11:02 +08:00
|
|
|
|
|
|
|
try {
|
|
|
|
Notification::send($admin_users, $notification);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
Log::emergency('Error emailing admins, malformed email='.$e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param User $user
|
|
|
|
* @param \Illuminate\Notifications\Notification $notification
|
|
|
|
*/
|
|
|
|
protected function notifyUser($user, $notification)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$user->notify($notification);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
Log::emergency('Error emailing admins, malformed email='.$e->getMessage());
|
|
|
|
}
|
2018-01-01 13:26:10 +08:00
|
|
|
}
|
|
|
|
|
2017-12-23 02:46:46 +08:00
|
|
|
/**
|
|
|
|
* Send an email when the user registered
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2017-12-23 02:46:46 +08:00
|
|
|
* @param UserRegistered $event
|
|
|
|
*/
|
2018-03-20 09:50:40 +08:00
|
|
|
public function onUserRegister(UserRegistered $event): void
|
2017-12-23 02:00:57 +08:00
|
|
|
{
|
2019-08-31 03:59:17 +08:00
|
|
|
Log::info('NotificationEvents::onUserRegister: '
|
2019-07-17 01:54:14 +08:00
|
|
|
.$event->user->ident.' is '
|
2018-03-20 09:50:40 +08:00
|
|
|
.UserState::label($event->user->state)
|
|
|
|
.', sending active email');
|
2017-12-23 02:46:46 +08:00
|
|
|
|
2019-08-31 03:59:17 +08:00
|
|
|
/*
|
|
|
|
* Send all of the admins a notification that a new user registered
|
|
|
|
*/
|
|
|
|
$this->notifyAdmins(new \App\Notifications\Admin\UserRegistered($event->user));
|
2017-12-31 10:39:17 +08:00
|
|
|
|
2019-08-31 03:59:17 +08:00
|
|
|
/*
|
|
|
|
* Send the user a confirmation email
|
|
|
|
*/
|
2018-03-20 09:50:40 +08:00
|
|
|
if ($event->user->state === UserState::ACTIVE) {
|
2019-09-18 07:11:02 +08:00
|
|
|
$this->notifyUser($event->user, new \App\Notifications\UserRegistered($event->user));
|
2018-08-27 00:40:04 +08:00
|
|
|
} elseif ($event->user->state === UserState::PENDING) {
|
2019-09-18 07:11:02 +08:00
|
|
|
$this->notifyUser($event->user, new \App\Notifications\UserPending($event->user));
|
2017-12-23 02:46:46 +08:00
|
|
|
}
|
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
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2017-12-23 06:32:21 +08:00
|
|
|
* @param UserStateChanged $event
|
|
|
|
*/
|
2018-03-20 09:50:40 +08:00
|
|
|
public function onUserStateChange(UserStateChanged $event): void
|
2017-12-23 06:32:21 +08:00
|
|
|
{
|
2019-08-31 03:59:17 +08:00
|
|
|
Log::info('NotificationEvents::onUserStateChange: New user state='.$event->user->state);
|
2018-01-01 13:15:12 +08:00
|
|
|
|
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) {
|
2019-09-18 07:11:02 +08:00
|
|
|
$this->notifyUser($event->user, new \App\Notifications\UserRegistered($event->user));
|
2018-08-27 00:40:04 +08:00
|
|
|
} elseif ($event->user->state === UserState::REJECTED) {
|
2019-09-18 07:11:02 +08:00
|
|
|
$this->notifyUser($event->user, new \App\Notifications\UserRejected($event->user));
|
2017-12-23 06:32:21 +08:00
|
|
|
}
|
2019-08-31 03:59:17 +08:00
|
|
|
} elseif ($event->old_state === UserState::ACTIVE) {
|
2018-03-20 09:50:40 +08:00
|
|
|
Log::info('User state change from active to ??');
|
2017-12-23 06:32:21 +08:00
|
|
|
}
|
|
|
|
}
|
2019-08-31 03:59:17 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Notify the admins that a new PIREP has been filed
|
|
|
|
*
|
|
|
|
* @param \App\Events\PirepFiled $event
|
|
|
|
*/
|
|
|
|
public function onPirepFile(PirepFiled $event): void
|
|
|
|
{
|
|
|
|
Log::info('NotificationEvents::onPirepFile: '.$event->pirep->id.' filed ');
|
|
|
|
$this->notifyAdmins(new PirepSubmitted($event->pirep));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notify the user that their PIREP has been accepted
|
|
|
|
*
|
|
|
|
* @param \App\Events\PirepAccepted $event
|
|
|
|
*/
|
|
|
|
public function onPirepAccepted(PirepAccepted $event): void
|
|
|
|
{
|
|
|
|
Log::info('NotificationEvents::onPirepAccepted: '.$event->pirep->id.' accepted');
|
2019-09-18 07:11:02 +08:00
|
|
|
$this->notifyUser($event->pirep->user, new \App\Notifications\PirepAccepted($event->pirep));
|
2019-08-31 03:59:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notify the user that their PIREP has been accepted
|
|
|
|
*
|
|
|
|
* @param \App\Events\PirepRejected $event
|
|
|
|
*/
|
|
|
|
public function onPirepRejected(PirepRejected $event): void
|
|
|
|
{
|
|
|
|
Log::info('NotificationEvents::onPirepRejected: '.$event->pirep->id.' rejected');
|
2019-09-18 07:11:02 +08:00
|
|
|
$this->notifyUser($event->pirep->user, new \App\Notifications\PirepRejected($event->pirep));
|
2019-08-31 03:59:17 +08:00
|
|
|
}
|
2017-12-23 02:00:57 +08:00
|
|
|
}
|