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.

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