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.

74 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. 'business_id' => request('_business_info')['id']
  27. ];
  28. return $this;
  29. }
  30. /**
  31. * Fetch message from notifications lang file
  32. *
  33. * @param $payload
  34. * @param $key
  35. * @param null $options
  36. * @return array|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Translation\Translator|string|null
  37. *
  38. */
  39. protected function getMessageLine($payload, $key, $options = [])
  40. {
  41. return __('notification.'.$payload->data->table_name.'.'.enum('cruds.inverse.'.$payload->data->crud_id.'.singular_name').'.'.$key, $options);
  42. }
  43. /**
  44. * Call notifications
  45. *
  46. * @param $users
  47. * @param $notif
  48. */
  49. public function sendNotifications($users, $level = null) {
  50. switch ($level) {
  51. case "emergency":
  52. // Notification::send($users, new SmsNotification($notif));
  53. case "critical":
  54. Notification::send($users, new MailNotification($this->notif));
  55. case "high":
  56. Notification::send($users, new DBNotification($this->notif));
  57. case "medium":
  58. Notification::send($users, new FcmNotification($this->notif));
  59. case "low":
  60. Notification::send($users, new SocketNotification($this->notif));
  61. break;
  62. default:
  63. Notification::send($users, new SocketNotification($this->notif));
  64. }
  65. }
  66. }