phpvms/app/Listeners/NotificationEvents.php

125 lines
3.3 KiB
PHP
Raw Normal View History

<?php
namespace App\Listeners;
2019-07-16 03:51:35 +08:00
use App\Contracts\Listener;
use App\Events\UserRegistered;
use App\Events\UserStateChanged;
2017-12-31 10:40:32 +08:00
use App\Models\Enums\UserState;
use Illuminate\Contracts\Events\Dispatcher;
2018-02-21 12:33:09 +08:00
use Illuminate\Support\Facades\Mail;
use Log;
/**
* Handle sending emails on different events
*/
class NotificationEvents extends Listener
{
/**
* @param Dispatcher $events
*/
public function subscribe(Dispatcher $events): void
{
$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'
);
}
/**
* @return bool
*/
protected function mailerActive(): bool
{
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
2018-08-27 00:40:04 +08:00
*
2018-01-01 13:26:10 +08:00
* @return mixed
*/
protected function sendEmail($to, $email)
{
try {
return Mail::to($to)->send($email);
} catch (\Exception $e) {
2018-01-01 13:26:10 +08:00
Log::error('Error sending email!');
Log::error($e);
}
}
/**
* Send an email when the user registered
2018-08-27 00:40:04 +08:00
*
* @param UserRegistered $event
*/
public function onUserRegister(UserRegistered $event): void
{
Log::info('onUserRegister: '
.$event->user->ident.' is '
.UserState::label($event->user->state)
.', sending active email');
if (!$this->mailerActive()) {
return;
}
2018-08-27 00:40:04 +08:00
// First send the admin a notification
2017-12-31 10:39:17 +08:00
$admin_email = setting('general.admin_email');
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
}
2018-08-27 00:40:04 +08:00
// Then notify the user
if ($event->user->state === UserState::ACTIVE) {
2018-01-01 13:26:10 +08:00
$email = new \App\Mail\UserRegistered($event->user);
2018-08-27 00:40:04 +08:00
} elseif ($event->user->state === UserState::PENDING) {
2018-01-01 13:26:10 +08:00
$email = new \App\Mail\UserPending($event->user);
}
2018-01-01 13:26:10 +08:00
$this->sendEmail($event->user->email, $email);
}
/**
* When a user's state changes, send an email out
2018-08-27 00:40:04 +08:00
*
* @param UserStateChanged $event
*/
public function onUserStateChange(UserStateChanged $event): void
{
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) {
2019-05-13 02:26:44 +08:00
$email = new \App\Mail\UserRegistered(
$event->user,
'Your registration has been accepted!'
);
2018-08-27 00:40:04 +08:00
} elseif ($event->user->state === UserState::REJECTED) {
$email = new \App\Mail\UserRejected($event->user);
}
2018-01-01 13:26:10 +08:00
$this->sendEmail($event->user->email, $email);
2018-08-27 00:40:04 +08:00
} // TODO: Other state transitions
elseif ($event->old_state === UserState::ACTIVE) {
Log::info('User state change from active to ??');
}
}
}