src/EventListener/UserCustomerStatusListener.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Entity\EntityStatus;
  4. use App\Entity\User;
  5. use App\Security\Badge\Frontend\UserCustomerStatusBadge;
  6. use App\Security\Exception\UserNotEnableException;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\Security\Http\Event\CheckPassportEvent;
  9. class UserCustomerStatusListener implements EventSubscriberInterface
  10. {
  11.     public function checkPassport(CheckPassportEvent $event): void
  12.     {
  13.         $passport $event->getPassport();
  14.         if (!$passport->hasBadge(UserCustomerStatusBadge::class)) {
  15.             return;
  16.         }
  17.         /** @var UserCustomerStatusBadge $badge */
  18.         $badge $passport->getBadge(UserCustomerStatusBadge::class);
  19.         if ($badge->isResolved()) {
  20.             return;
  21.         }
  22.         $user $passport->getUser();
  23.         if ($user->getStatus() !== EntityStatus::STATUS_ENABLED) {
  24.             throw new UserNotEnableException();
  25.         }
  26.         $badge->markResolved();
  27.     }
  28.     public static function getSubscribedEvents(): array
  29.     {
  30.         return [CheckPassportEvent::class => ['checkPassport'513]];
  31.     }
  32. }