<?php
namespace App\EventListener;
use App\Entity\EntityStatus;
use App\Entity\User;
use App\Security\Badge\Backend\UserStatusBadge;
use App\Security\Exception\UserNotEnableException;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Http\Event\CheckPassportEvent;
class UserStatusListener implements EventSubscriberInterface
{
public function checkPassport(CheckPassportEvent $event): void
{
$passport = $event->getPassport();
if (!$passport->hasBadge(UserStatusBadge::class)) {
return;
}
/** @var UserStatusBadge $badge */
$badge = $passport->getBadge(UserStatusBadge::class);
if ($badge->isResolved()) {
return;
}
$user = $passport->getUser();
if ($user->getStatus() !== EntityStatus::STATUS_ENABLED) {
throw new UserNotEnableException();
}
$badge->markResolved();
}
public static function getSubscribedEvents(): array
{
return [CheckPassportEvent::class => ['checkPassport', 513]];
}
}