<?php
namespace App\Services\EntityServices;
use App\Entity\{
Tenant,
User
};
use App\Repository\UserRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
class UserEntityService
{
use EntityServicesTrait;
private $hasher;
private $entityManager;
private $userRepository;
public function __construct(
UserPasswordHasherInterface $hasher,
EntityManagerInterface $entityManager,
UserRepository $userRepository
) {
$this->hasher = $hasher;
$this->entityManager = $entityManager;
$this->userRepository = $userRepository;
}
/**
* find the User by email and Tenant
*/
public function findUser(string $email, ?Tenant $tenant = null): ?User
{
return $this->userRepository->findOneBy([
'email' => $email,
'tenant' => $tenant
]);
}
/**
* create the User by email
*/
public function createUser(string $email, Tenant $tenant, bool $isAdmin = false): User
{
return $this->saveEntity((new User())
->setEmail($email)
->setTenant($tenant)
->setRoles([$isAdmin ? 'ROLE_ADMIN' : 'ROLE_USER'])
->setCreatedAt(new \DateTime())
->setUpdatedAt(new \DateTime())
);
}
/**
*
*/
public function setEmailConfirmed(User $user, bool $value = true): User
{
return $this->saveEntity( $user
->setIsEmailConfirmed( $value )
->setUpdatedAt(new \DateTime())
);
}
/**
* set the encoded password without any checks
*/
public function updatePassword(User $user, string $password): User
{
return $this->saveEntity( $user
->setPassword(
$this->hasher->hashPassword($user, $password)
)
->setUpdatedAt(new \DateTime())
);
}
}