src/EventSubscriber/NotificationSubscriber.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Service\EsSocket;
  4. use App\Entity\Notification;
  5. use App\Event\NotificationPostPersistEvent;
  6. use Symfony\Component\Serializer\SerializerInterface;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. class NotificationSubscriber implements EventSubscriberInterface
  9. {
  10.     public function __construct(
  11.         private EsSocket $esSocket,
  12.         private SerializerInterface $serializer
  13.     ) {}
  14.     public static function getSubscribedEvents(): array
  15.     {
  16.         return [
  17.             NotificationPostPersistEvent::NAME => 'onPostCreate'
  18.         ];
  19.     }
  20.     public function onPostCreate(NotificationPostPersistEvent $event)
  21.     {
  22.         $object $event->getObject();
  23.         $notification $this->serializer->serialize(
  24.             $object,
  25.             'json',
  26.             ['groups' => ['Notification:Read']]
  27.         );
  28.         $notification json_decode($notificationtrue);
  29.         $this->esSocket->emit(
  30.             'backend-command',
  31.             [
  32.                 'type' => 'notification',
  33.                 'payload' => ['containerId' => $object->getContainer()->getId(), 'notification' => $notification]
  34.             ]
  35.         );
  36.     }
  37. }