<?php
declare(strict_types=1);
namespace App\Bundles\AuditBundle\EventListener;
use App\Bundles\AuditBundle\Event\ExportEvent;
use App\Bundles\AuditBundle\Factory\AuditLogEntryFactory;
use App\Bundles\UserBundle\Service\User\UserProvider;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
final class ExportEventListener implements EventSubscriberInterface
{
public function __construct(
private readonly UserProvider $userProvider,
private readonly AuditLogEntryFactory $factory,
private readonly EntityManagerInterface $entityManager,
) {
}
public static function getSubscribedEvents(): array
{
return [
ExportEvent::class => 'onExport',
];
}
public function onExport(ExportEvent $event): void
{
if (!$user = $this->userProvider->provideFromSession()) {
return;
}
$entry = $this->factory->createExportLogEntry($user, $event->getClassName());
$this->entityManager->persist($entry);
$this->entityManager->flush();
}
}