Notifier.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. /* @phpstan-ignore-next-line Je ne sais pas d'où vient cette erreur, mais elle c'est un faux positif */
  29. $notification->setName($name)
  30. ->setRecipientAccess($access)
  31. ->setRecipientOrganization($access->getOrganization())
  32. ->setType($type)
  33. ->setMessage($message)
  34. ->setLink($link)
  35. ->setCreateDate($now)
  36. ->setUpdateDate($now);
  37. return $notification;
  38. }
  39. /**
  40. * @param array<string> $message
  41. */
  42. public function notify(
  43. Access $access,
  44. string $name,
  45. NotificationTypeEnum $type,
  46. array $message,
  47. ?string $link = null,
  48. ): Notification {
  49. $notification = $this->createNotification($access, $name, $type, $message, $link);
  50. $this->em->persist($notification);
  51. $this->em->flush();
  52. $this->mercureHub->publishCreate($access->getId(), $notification);
  53. return $notification;
  54. }
  55. public function notifyExport(
  56. Access $access,
  57. File $file,
  58. ): Notification {
  59. return $this->notify(
  60. $access,
  61. 'export',
  62. NotificationTypeEnum::FILE,
  63. ['fileName' => $file->getName()],
  64. '/api/files/'.$file->getId().'/download'
  65. );
  66. }
  67. /**
  68. * @param array<string> $message
  69. */
  70. public function notifyMessage(
  71. Access $access,
  72. array $message,
  73. ): Notification {
  74. return $this->notify(
  75. $access,
  76. 'message',
  77. NotificationTypeEnum::MESSAGE,
  78. $message
  79. );
  80. }
  81. /**
  82. * @param list<string> $message
  83. */
  84. public function notifySystem(
  85. Access $access,
  86. array $message,
  87. ): Notification {
  88. return $this->notify(
  89. $access,
  90. 'message',
  91. NotificationTypeEnum::SYSTEM,
  92. $message
  93. );
  94. }
  95. /**
  96. * @param list<string> $message
  97. */
  98. public function notifyError(
  99. Access $access,
  100. string $name,
  101. array $message,
  102. ): Notification {
  103. return $this->notify(
  104. $access,
  105. $name,
  106. NotificationTypeEnum::ERROR,
  107. $message
  108. );
  109. }
  110. }