src/App/Security/Http/Logout/TwoFactorLogoutSubscriber.php line 24

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace App\Security\Http\Logout;
  3. use App\Security\TwoFactor\AllowedDevice\ReferenceStorage;
  4. use App\Security\TwoFactor\TwoFactorManager;
  5. use App\Security\TwoFactor\TwoFactorUserInterface;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\Security\Http\Event\LogoutEvent;
  8. class TwoFactorLogoutSubscriber implements EventSubscriberInterface
  9. {
  10.     private TwoFactorManager $twoFactorManager;
  11.     private ReferenceStorage $referenceStorage;
  12.     public function __construct(TwoFactorManager $twoFactorManagerReferenceStorage $referenceStorage)
  13.     {
  14.         $this->twoFactorManager $twoFactorManager;
  15.         $this->referenceStorage $referenceStorage;
  16.     }
  17.     public function onLogout(LogoutEvent $event): void
  18.     {
  19.         $token $event->getToken();
  20.         if (null === $token) {
  21.             return;
  22.         }
  23.         $user $token->getUser();
  24.         if ($user instanceof TwoFactorUserInterface) {
  25.             $this->twoFactorManager->unauthenticate($event->getRequest());
  26.         }
  27.         if (null === $this->referenceStorage->fetch()) {
  28.             return;
  29.         }
  30.         $this->referenceStorage->forget();
  31.     }
  32.     /**
  33.      * @return array<string, array<int|string, array<int|string, int|string>|int|string>|string>
  34.      */
  35.     public static function getSubscribedEvents(): array
  36.     {
  37.         return [
  38.             LogoutEvent::class => 'onLogout',
  39.         ];
  40.     }
  41. }