src/Security/Listener/LoadUserListener.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Security\Listener;
  3. use App\Entity\User;
  4. use App\Repository\UserRepository;
  5. use App\Security\Event\LoadUser;
  6. class LoadUserListener
  7. {
  8.     private UserRepository $userRepository;
  9.     public function __construct(UserRepository $userRepository)
  10.     {
  11.         $this->userRepository $userRepository;
  12.     }
  13.     public function __invoke(LoadUser $event): void
  14.     {
  15.         $username $event->username();
  16.         $user $this->userRepository->loadUserByUsername($username);
  17.         if (!$user) {
  18.             $user User::create($username);
  19.             $this->userRepository->save($user);
  20.         }
  21.         $event->setUser($user);
  22.     }
  23. }