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