<?php
declare(strict_types=1);
namespace App\Platform\EventListener;
use App\Bundles\AuditBundle\Factory\AuditLogEntryFactory;
use App\Bundles\UserBundle\Entity\User;
use App\Bundles\UserBundle\Service\User\UserProvider;
use App\Platform\Event\ArchiveEvent;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
final class ArchiveEventListener implements EventSubscriberInterface
{
public function __construct(
private readonly UserProvider $userProvider,
private readonly AuditLogEntryFactory $factory,
private readonly EntityManagerInterface $entityManager,
) {
}
public static function getSubscribedEvents(): array
{
return [
ArchiveEvent::class => 'onArchive',
];
}
public function onArchive(ArchiveEvent $event): void
{
/** @var User|null $user */
$user = $this->userProvider->provideFromSession();
if (!$user) {
return;
}
$entry = $this->factory->createArchive($user, $event);
$this->entityManager->persist($entry);
$this->entityManager->flush();
}
}