src/EventSubscriber/ClientSubscriber.php line 27

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