Notifier.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Service;
  4. use App\Entity\Access\Access;
  5. use App\Entity\Core\File;
  6. use App\Entity\Core\Notification;
  7. use App\Enum\Core\NotificationTypeEnum;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. class Notifier
  10. {
  11. public function __construct(
  12. private EntityManagerInterface $em,
  13. private MercureHub $mercureHub
  14. ) {
  15. }
  16. /**
  17. * @param list<string> $message
  18. */
  19. protected function createNotification(
  20. Access $access,
  21. string $name,
  22. NotificationTypeEnum $type,
  23. array $message,
  24. ?string $link = null
  25. ): Notification {
  26. $now = new \DateTime();
  27. $notification = new Notification();
  28. $notification->setName($name)
  29. ->setRecipientAccess($access)
  30. ->setRecipientOrganization($access->getOrganization())
  31. ->setType($type)
  32. ->setMessage($message)
  33. ->setLink($link)
  34. ->setCreateDate($now)
  35. ->setUpdateDate($now);
  36. return $notification;
  37. }
  38. /**
  39. * @param array<string> $message
  40. */
  41. public function notify(
  42. Access $access,
  43. string $name,
  44. NotificationTypeEnum $type,
  45. array $message,
  46. ?string $link = null
  47. ): Notification {
  48. $notification = $this->createNotification($access, $name, $type, $message, $link);
  49. $this->em->persist($notification);
  50. $this->em->flush();
  51. $this->mercureHub->publishCreate($access->getId(), $notification);
  52. return $notification;
  53. }
  54. public function notifyExport(
  55. Access $access,
  56. File $file
  57. ): Notification {
  58. return $this->notify(
  59. $access,
  60. 'export',
  61. NotificationTypeEnum::FILE,
  62. ['fileName' => $file->getName()],
  63. '/api/files/'.$file->getId().'/download'
  64. );
  65. }
  66. /**
  67. * @param array<string> $message
  68. */
  69. public function notifyMessage(
  70. Access $access,
  71. array $message
  72. ): Notification {
  73. return $this->notify(
  74. $access,
  75. 'message',
  76. NotificationTypeEnum::MESSAGE,
  77. $message
  78. );
  79. }
  80. /**
  81. * @param list<string> $message
  82. */
  83. public function notifySystem(
  84. Access $access,
  85. array $message
  86. ): Notification {
  87. return $this->notify(
  88. $access,
  89. 'message',
  90. NotificationTypeEnum::SYSTEM,
  91. $message
  92. );
  93. }
  94. /**
  95. * @param list<string> $message
  96. */
  97. public function notifyError(
  98. Access $access,
  99. string $name,
  100. array $message
  101. ): Notification {
  102. return $this->notify(
  103. $access,
  104. $name,
  105. NotificationTypeEnum::ERROR,
  106. $message
  107. );
  108. }
  109. }