<?php
namespace App\Security\Listener;
use App\Entity\User;
use App\Repository\UserRepository;
use App\Security\Event\LoadUser;
class LoadUserListener
{
private UserRepository $userRepository;
public function __construct(UserRepository $userRepository)
{
$this->userRepository = $userRepository;
}
public function __invoke(LoadUser $event): void
{
$username = $event->username();
$user = $this->userRepository->loadUserByUsername($username);
if (!$user) {
$user = User::create($username);
$this->userRepository->save($user);
}
$event->setUser($user);
}
}