<?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\Entity\User;
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
{
/** @var User|null $user */
$user = $this->userProvider->provideFromSession();
if (!$user) {
return;
}
$entry = $this->factory->createExport($user, $event->getClassName());
$this->entityManager->persist($entry);
$this->entityManager->flush();
}
}