You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
<?php
namespace App\Listeners;
use App\Events\TaskCreate; use App\Models\Task; use App\Models\User; use App\Notifications\DBNotification; use App\Notifications\FcmNotification; use App\Notifications\MailNotification; use Illuminate\Support\Facades\Notification;
class TaskCreateNotif { /** * Create the event listener. * * @return void */ public function __construct() { //
}
/** * Handle the event. * * @param TaskCreate $event * @return void */ public function handle(TaskCreate $event) { $payload = $event->message;
if ($payload->data->original->assignee_id !== null) { $this->assigneeNotifHandler($payload); } if ($payload->data->original->approver_id !== null) { $this->approverNotifHandler($payload); } }
public function assigneeNotifHandler($payload) { $user = User::findOrFail($payload->data->original->assignee_id); $task = Task::findOrFail($payload->data->original->id);
$notif = $this->makeNotif($payload, 'assignee', ['task' => $task->title]);
$this->sendNotifications($user, $notif); }
public function approverNotifHandler($payload) { $user = User::findOrFail($payload->data->original->approver_id); $task = Task::findOrFail($payload->data->original->id);
$notif = $this->makeNotif($payload, 'approver', ['task' => $task->title]);
$this->sendNotifications($user, $notif); }
/** * Make notification object * * @param $payload * @param null $route * @param array $options * @return array */ public function makeNotif($payload, $route = null, $options = []) { $route = $route == null ? "" : $route.'.'; return [ 'greeting' => $this->getMessageLine($payload, $route.'greeting'), 'subject' => $this->getMessageLine($payload, $route.'subject'), 'title' => $this->getMessageLine($payload, $route.'title'), 'body' => $this->getMessageLine($payload, $route.'body', $options) ]; }
/** * Fetch message from notifications lang file * * @param $payload * @param $key * @param null $options * @return array|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Translation\Translator|string|null * */ public function getMessageLine($payload, $key, $options = []) { return __('notification.'.$payload->data->table_name.'.'.enum('cruds.inverse.'.$payload->data->crud_id.'.singular_name').'.'.$key, $options); }
/** * Call notifications * * @param $users * @param $notif */ public function sendNotifications($users, $notif) { Notification::send($users, new MailNotification($notif)); Notification::send($users, new DBNotification($notif)); Notification::send($users, new FcmNotification($notif)); } }
|