src/EventSubscriber/ArticleDepartmentSubscriber.php line 61

  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\ArticleDepartment;
  4. use App\Event\ArticleDepartment\ArticleDepartmentDeletedEvent;
  5. use App\Event\ArticleDepartment\ArticleDepartmentUpdatedEvent;
  6. use App\Event\ArticleDepartment\ArticleDepartmentCreatedEvent;
  7. use App\Service\NotificationService;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. class ArticleDepartmentSubscriber implements EventSubscriberInterface
  10. {
  11.     public function __construct(private readonly NotificationService $notificationService)
  12.     {
  13.     }
  14.     public static function getSubscribedEvents(): array
  15.     {
  16.         return [
  17.             ArticleDepartmentCreatedEvent::class => 'onCreated',
  18.             ArticleDepartmentUpdatedEvent::class => 'onUpdated',
  19.             ArticleDepartmentDeletedEvent::class => 'onDeleted',
  20.         ];
  21.     }
  22.     public function onCreated(ArticleDepartmentCreatedEvent $event): void
  23.     {
  24.         $article $event->getArticle();
  25.         //if ($article instanceof ArticleDepartment) {
  26.         $this->notificationService->notifyUser(
  27.             $article->getAuthor(),
  28.             'Article ajouté au département',
  29.             sprintf(
  30.                 "Vous venez d'ajouter l'article <strong>%s</strong> au département <strong>%s</strong>",
  31.                 $article->getName(),
  32.                 $article->getDepartment()
  33.             ),
  34.             $article
  35.         );
  36.         // }
  37.     }
  38.     public function onUpdated(ArticleDepartmentUpdatedEvent $event): void
  39.     {
  40.         $article $event->getArticle();
  41.         $this->notificationService->notifyUser(
  42.             $article->getAuthor(),
  43.             'Article du département modifié',
  44.             sprintf(
  45.                 "Vous venez de modifier l'article <strong>%s</strong> du département <strong>%s</strong>",
  46.                 $article->getName(),
  47.                 $article->getDepartment()
  48.             ),
  49.             $article
  50.         );
  51.     }
  52.     public function onDeleted(ArticleDepartmentDeletedEvent $event): void
  53.     {
  54.         $article $event->getArticle();
  55.         $this->notificationService->notifyUser(
  56.             $article->getAuthor(),
  57.             'Article retiré du département',
  58.             sprintf(
  59.                 "Vous venez de retirer l'article <strong>%s</strong> du département <strong>%s</strong>",
  60.                 $article->getName(),
  61.                 $article->getDepartment()
  62.             ),
  63.             $article
  64.         );
  65.     }
  66. }