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.

73 lines
2.3 KiB

  1. <?php
  2. namespace App\Utilities\HelperClass;
  3. use App\Notifications\DBNotification;
  4. use App\Notifications\FcmNotification;
  5. use App\Notifications\MailNotification;
  6. use App\Notifications\SocketNotification;
  7. use Illuminate\Support\Facades\Notification;
  8. class NotificationHelper
  9. {
  10. protected $notif;
  11. /**
  12. * Make notification object
  13. *
  14. * @param $payload
  15. * @param null $route
  16. * @param array $options
  17. * @return array
  18. */
  19. public function makeNotif($payload, $route = null, $options = []) {
  20. $route = $route == null ? "" : $route.'.';
  21. $this->notif = [
  22. 'greeting' => $this->getMessageLine($payload, $route.'greeting'),
  23. 'subject' => $this->getMessageLine($payload, $route.'subject'),
  24. 'title' => $this->getMessageLine($payload, $route.'title'),
  25. 'body' => $this->getMessageLine($payload, $route.'body', $options)
  26. ];
  27. return $this;
  28. }
  29. /**
  30. * Fetch message from notifications lang file
  31. *
  32. * @param $payload
  33. * @param $key
  34. * @param null $options
  35. * @return array|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Translation\Translator|string|null
  36. *
  37. */
  38. protected function getMessageLine($payload, $key, $options = [])
  39. {
  40. return __('notification.'.$payload->data->table_name.'.'.enum('cruds.inverse.'.$payload->data->crud_id.'.singular_name').'.'.$key, $options);
  41. }
  42. /**
  43. * Call notifications
  44. *
  45. * @param $users
  46. * @param $notif
  47. */
  48. public function sendNotifications($users, $level = null) {
  49. switch ($level) {
  50. case "emergency":
  51. // Notification::send($users, new SmsNotification($notif));
  52. case "critical":
  53. Notification::send($users, new MailNotification($this->notif));
  54. case "high":
  55. Notification::send($users, new DBNotification($this->notif));
  56. case "medium":
  57. Notification::send($users, new FcmNotification($this->notif));
  58. case "low":
  59. Notification::send($users, new SocketNotification($this->notif));
  60. break;
  61. default:
  62. Notification::send($users, new SocketNotification($this->notif));
  63. }
  64. }
  65. }