2017-12-23 02:00:57 +08:00
|
|
|
<?php
|
|
|
|
|
2020-02-09 02:29:34 +08:00
|
|
|
namespace App\Notifications;
|
2017-12-23 02:00:57 +08:00
|
|
|
|
2019-07-16 03:51:35 +08:00
|
|
|
use App\Contracts\Listener;
|
2020-02-09 02:29:34 +08:00
|
|
|
use App\Events\NewsAdded;
|
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;
|
2020-02-09 02:29:34 +08:00
|
|
|
use App\Notifications\Messages\PirepSubmitted;
|
|
|
|
use App\Notifications\Messages\UserPending;
|
|
|
|
use App\Notifications\Messages\UserRejected;
|
|
|
|
use App\Notifications\Notifiables\Broadcast;
|
|
|
|
use Exception;
|
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
|
|
|
|
|
|
|
/**
|
2020-02-09 02:29:34 +08:00
|
|
|
* Listen for different events and map them to different notifications
|
2017-12-23 02:00:57 +08:00
|
|
|
*/
|
2020-02-09 02:29:34 +08:00
|
|
|
class EventHandler extends Listener
|
2017-12-23 02:00:57 +08:00
|
|
|
{
|
2020-02-09 02:29:34 +08:00
|
|
|
private static $broadcastNotifyable;
|
|
|
|
|
|
|
|
public static $callbacks = [
|
|
|
|
PirepAccepted::class => 'onPirepAccepted',
|
|
|
|
PirepFiled::class => 'onPirepFile',
|
|
|
|
PirepRejected::class => 'onPirepRejected',
|
|
|
|
UserRegistered::class => 'onUserRegister',
|
|
|
|
UserStateChanged::class => 'onUserStateChange',
|
|
|
|
];
|
|
|
|
|
|
|
|
public function __construct()
|
2017-12-23 02:00:57 +08:00
|
|
|
{
|
2020-02-09 02:29:34 +08:00
|
|
|
static::$broadcastNotifyable = app(Broadcast::class);
|
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);
|
2020-02-09 02:29:34 +08:00
|
|
|
} catch (Exception $e) {
|
2019-09-18 07:11:02 +08:00
|
|
|
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);
|
2020-02-09 02:29:34 +08:00
|
|
|
} catch (Exception $e) {
|
|
|
|
Log::emergency('Error emailing admins, malformed email='.$e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a notification to all users
|
|
|
|
*
|
|
|
|
* @param $notification
|
|
|
|
*/
|
|
|
|
protected function notifyAllUsers($notification)
|
|
|
|
{
|
|
|
|
$users = User::all()->get();
|
|
|
|
|
|
|
|
try {
|
|
|
|
Notification::send($users, $notification);
|
|
|
|
} catch (Exception $e) {
|
2019-09-18 07:11:02 +08:00
|
|
|
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 '
|
2020-02-09 02:29:34 +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
|
|
|
|
*/
|
2020-02-09 02:29:34 +08:00
|
|
|
$this->notifyAdmins(new Messages\AdminUserRegistered($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) {
|
2020-02-09 02:29:34 +08:00
|
|
|
$this->notifyUser($event->user, new Messages\UserRegistered($event->user));
|
2018-08-27 00:40:04 +08:00
|
|
|
} elseif ($event->user->state === UserState::PENDING) {
|
2020-02-09 02:29:34 +08:00
|
|
|
$this->notifyUser($event->user, new 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) {
|
2020-02-09 02:29:34 +08:00
|
|
|
$this->notifyUser($event->user, new Messages\UserRegistered($event->user));
|
2018-08-27 00:40:04 +08:00
|
|
|
} elseif ($event->user->state === UserState::REJECTED) {
|
2020-02-09 02:29:34 +08:00
|
|
|
$this->notifyUser($event->user, new 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');
|
2020-02-09 02:29:34 +08:00
|
|
|
$this->notifyUser($event->pirep->user, new Messages\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');
|
2020-02-09 02:29:34 +08:00
|
|
|
$this->notifyUser($event->pirep->user, new Messages\PirepRejected($event->pirep));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notify all users of a news event
|
|
|
|
*
|
|
|
|
* @param \App\Events\NewsAdded $event
|
|
|
|
*/
|
|
|
|
public function onNewsAdded(NewsAdded $event): void
|
|
|
|
{
|
|
|
|
Log::info('NotificationEvents::onNewsAdded');
|
|
|
|
$this->notifyAllUsers(new Messages\NewsAdded($event->news));
|
2019-08-31 03:59:17 +08:00
|
|
|
}
|
2017-12-23 02:00:57 +08:00
|
|
|
}
|