src/Services/EntityServices/UserEntityService.php line 34

Open in your IDE?
  1. <?php
  2. namespace App\Services\EntityServices;
  3. use App\Entity\{
  4.     Tenant,
  5.     User
  6. };
  7. use App\Repository\UserRepository;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  10. class UserEntityService
  11. {
  12.     use EntityServicesTrait;
  13.     private $hasher;
  14.     private $entityManager;
  15.     private $userRepository;
  16.     public function __construct(
  17.         UserPasswordHasherInterface $hasher,
  18.         EntityManagerInterface $entityManager,
  19.         UserRepository $userRepository
  20.     ) {
  21.         $this->hasher         $hasher;
  22.         $this->entityManager  $entityManager;
  23.         $this->userRepository $userRepository;
  24.     }
  25.     /**
  26.      * find the User by email and Tenant
  27.      */
  28.     public function findUser(string $email, ?Tenant $tenant null): ?User
  29.     {
  30.         return $this->userRepository->findOneBy([
  31.             'email' => $email,
  32.             'tenant' => $tenant
  33.         ]);
  34.     }
  35.     /**
  36.      * create the User by email
  37.      */
  38.     public function createUser(string $emailTenant $tenantbool $isAdmin false): User
  39.     {
  40.         return $this->saveEntity((new User())
  41.             ->setEmail($email)
  42.             ->setTenant($tenant)
  43.             ->setRoles([$isAdmin 'ROLE_ADMIN' 'ROLE_USER'])
  44.             ->setCreatedAt(new \DateTime())
  45.             ->setUpdatedAt(new \DateTime())
  46.         );
  47.     }
  48.     /**
  49.      * 
  50.      */
  51.     public function setEmailConfirmed(User $userbool $value true): User
  52.     {
  53.         return $this->saveEntity$user
  54.             ->setIsEmailConfirmed$value )
  55.             ->setUpdatedAt(new \DateTime())
  56.         );
  57.     }
  58.     /**
  59.      * set the encoded password without any checks
  60.      */
  61.     public function updatePassword(User $userstring $password): User
  62.     {
  63.         return $this->saveEntity$user
  64.             ->setPassword(
  65.                 $this->hasher->hashPassword($user$password)
  66.             )
  67.             ->setUpdatedAt(new \DateTime())
  68.         );
  69.     }
  70. }