<?php
namespace App\EventSubscriber;
use App\Entity\Logs\EventLog;
use App\Event\LogEvent;
use App\Repository\Logs\EventLogRepository;
use App\Service\NotificationService;
use Pusher\PusherException;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Serializer\SerializerInterface;
class LogSubscriber implements EventSubscriberInterface
{
/**
* @var SerializerInterface
*/
private $serializer;
/**
* @var EventLogRepository
*/
private $eventLogRepository;
/**
* @var NotificationService
*/
private $notificationService;
public function __construct(
SerializerInterface $serializer,
EventLogRepository $eventLogRepository,
NotificationService $notificationService
)
{
$this->serializer = $serializer;
$this->eventLogRepository = $eventLogRepository;
$this->notificationService = $notificationService;
}
/**
* @inheritDoc
*/
public static function getSubscribedEvents(): array
{
return [
LogEvent::class => 'onLogEvent',
];
}
/**
* @param LogEvent $event
*/
public function onLogEvent(LogEvent $event): void
{
$data = $this->serializer->serialize($event->getObject(), 'json', ['groups' => 'event:log']);
$eventLog = (new EventLog())
->setObjectId($event->getObject()->getId())
->setObjectFqcn(get_class($event->getObject()))
->setBlameObjectId($event->getBlameObject()->getId())
->setBlameObjectFqcn(get_class($event->getBlameObject()))
->setLogType($event->getLogType())
->setData($data);
$this->eventLogRepository->save($eventLog);
try {
$this->notificationService->sendEventLogNotification($eventLog);
} catch (PusherException $exception) {
//
}
}
}