src/EventSubscriber/CollectionsSubscriber.php line 43

  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Collections;
  4. use App\Event\Collections\CollectionsCreatedEvent;
  5. use App\Event\Collections\CollectionsDeletedEvent;
  6. use App\Event\Collections\CollectionsUpdatedEvent;
  7. use App\Service\NotificationService;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. class CollectionsSubscriber implements EventSubscriberInterface
  10. {
  11.     public function __construct(private NotificationService $notificationService){}
  12.     public static function getSubscribedEvents(): array
  13.     {
  14.         return [
  15.             CollectionsCreatedEvent::class => 'onCreated',
  16.             CollectionsUpdatedEvent::class => 'onUpdated',
  17.             CollectionsDeletedEvent::class => 'onDeleted',
  18.         ];
  19.     }
  20.     public function onCreated(CollectionsCreatedEvent $event): void
  21.     {
  22.         $collections $event->getCollections();
  23.         if ($collections instanceof Collections) {
  24.             $this->notificationService->notifyUser(
  25.                 $collections->getEstablishment()->getUser(),
  26.                 'Collection créée',
  27.                 sprintf(
  28.                     "Vous venez de créer la collection <strong>%s</strong>",
  29.                     $collections->getName()
  30.                 ),
  31.                 $collections
  32.             );
  33.         }
  34.     }
  35.     public function onUpdated(CollectionsUpdatedEvent $event): void
  36.     {
  37.         $collections $event->getCollections();
  38.         if ($collections instanceof Collections) {
  39.             $this->notificationService->notifyUser(
  40.                 $collections->getEstablishment()->getUser(),
  41.                 'Collection modifiée',
  42.                 sprintf(
  43.                     "Vous venez de modifier la collection <strong>%s</strong>",
  44.                     $collections->getName()
  45.                 ),
  46.                 $collections
  47.             );
  48.         }
  49.     }
  50.     public function onDeleted(CollectionsDeletedEvent $event): void
  51.     {
  52.         $collections $event->getCollections();
  53.         if ($collections instanceof Collections) {
  54.             $this->notificationService->notifyUser(
  55.                 $collections->getEstablishment()->getUser(),
  56.                 'Collection supprimée',
  57.                 sprintf(
  58.                     "Vous venez de supprimer la collection <strong>%s</strong>",
  59.                     $collections->getName()
  60.                 ),
  61.                 $collections
  62.             );
  63.         }
  64.     }
  65. }