| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- <?php
- declare(strict_types=1);
- namespace App\Service;
- use App\Entity\Access\Access;
- use App\Entity\Core\File;
- use App\Entity\Core\Notification;
- use App\Enum\Core\NotificationTypeEnum;
- use Doctrine\ORM\EntityManagerInterface;
- class Notifier
- {
- public function __construct(
- private EntityManagerInterface $em,
- private MercureHub $mercureHub
- ) {
- }
- /**
- * @param list<string> $message
- */
- protected function createNotification(
- Access $access,
- string $name,
- NotificationTypeEnum $type,
- array $message,
- ?string $link = null
- ): Notification {
- $now = new \DateTime();
- $notification = new Notification();
- $notification->setName($name)
- ->setRecipientAccess($access)
- ->setRecipientOrganization($access->getOrganization())
- ->setType($type)
- ->setMessage($message)
- ->setLink($link)
- ->setCreateDate($now)
- ->setUpdateDate($now);
- return $notification;
- }
- /**
- * @param array<string> $message
- */
- public function notify(
- Access $access,
- string $name,
- NotificationTypeEnum $type,
- array $message,
- ?string $link = null
- ): Notification {
- $notification = $this->createNotification($access, $name, $type, $message, $link);
- $this->em->persist($notification);
- $this->em->flush();
- $this->mercureHub->publishCreate($access->getId(), $notification);
- return $notification;
- }
- public function notifyExport(
- Access $access,
- File $file
- ): Notification {
- return $this->notify(
- $access,
- 'export',
- NotificationTypeEnum::FILE,
- ['fileName' => $file->getName()],
- '/api/files/'.$file->getId().'/download'
- );
- }
- /**
- * @param array<string> $message
- */
- public function notifyMessage(
- Access $access,
- array $message
- ): Notification {
- return $this->notify(
- $access,
- 'message',
- NotificationTypeEnum::MESSAGE,
- $message
- );
- }
- /**
- * @param list<string> $message
- */
- public function notifySystem(
- Access $access,
- array $message
- ): Notification {
- return $this->notify(
- $access,
- 'message',
- NotificationTypeEnum::SYSTEM,
- $message
- );
- }
- /**
- * @param list<string> $message
- */
- public function notifyError(
- Access $access,
- string $name,
- array $message
- ): Notification {
- return $this->notify(
- $access,
- $name,
- NotificationTypeEnum::ERROR,
- $message
- );
- }
- }
|