<?php
namespace App\EventSubscriber;
use App\Service\EsSocket;
use App\Entity\Notification;
use App\Event\NotificationPostPersistEvent;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class NotificationSubscriber implements EventSubscriberInterface
{
public function __construct(
private EsSocket $esSocket,
private SerializerInterface $serializer
) {}
public static function getSubscribedEvents(): array
{
return [
NotificationPostPersistEvent::NAME => 'onPostCreate'
];
}
public function onPostCreate(NotificationPostPersistEvent $event)
{
$object = $event->getObject();
$notification = $this->serializer->serialize(
$object,
'json',
['groups' => ['Notification:Read']]
);
$notification = json_decode($notification, true);
$this->esSocket->emit(
'backend-command',
[
'type' => 'notification',
'payload' => ['containerId' => $object->getContainer()->getId(), 'notification' => $notification]
]
);
}
}