SendActionBrowserNotification.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace App\Listeners;
  3. use App\Events\ObjectActionCreate;
  4. use App\Models\Enums\ActionObjectType;
  5. use App\Models\Enums\NotificationObjectType;
  6. use App\Models\Enums\ObjectAction;
  7. use App\Models\Notification;
  8. use App\Models\NotificationRecord;
  9. use App\Repositories\ConfigRepository;
  10. use App\Services\Notification\ActionBrowser\NormalNotification;
  11. use App\Services\Notification\ActionBrowser\RequirementNotification;
  12. use App\Services\Notification\ActionBrowser\TaskNotification;
  13. use Illuminate\Contracts\Queue\ShouldQueue;
  14. use Illuminate\Queue\InteractsWithQueue;
  15. class SendActionBrowserNotification implements ShouldQueue
  16. {
  17. /**
  18. * Create the event listener.
  19. */
  20. public function __construct()
  21. {
  22. //
  23. }
  24. /**
  25. * Handle the event.
  26. */
  27. public function handle(ObjectActionCreate $event): void
  28. {
  29. if(! ConfigRepository::openBrowserNotification()){
  30. return;
  31. }
  32. $actionObjectType = ActionObjectType::tryFrom($event->action->object_type);
  33. $object = $actionObjectType->modelBuilder()->find($event->action->object_id);
  34. if (! $object) {
  35. return;
  36. }
  37. $notification = match ($actionObjectType) {
  38. ActionObjectType::PROJECT, ActionObjectType::CONTAINER => new NormalNotification($event->action, $object),
  39. ActionObjectType::REQUIREMENT => new RequirementNotification($event->action, $object),
  40. ActionObjectType::TASK => new TaskNotification($event->action, $object),
  41. default => null,
  42. };
  43. $notification?->handle();
  44. }
  45. public function shouldQueue(ObjectActionCreate $event): bool
  46. {
  47. $actionObjectType = ActionObjectType::tryFrom($event->action->object_type);
  48. $objectAction = ObjectAction::tryFrom($event->action->action);
  49. if (! $actionObjectType || !$objectAction) {
  50. return false;
  51. }
  52. return in_array($objectAction->value, $event->notificationSetting[$actionObjectType->value]['browser'] ?? []);
  53. }
  54. }