2021-03-24 12:27:07 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Core\Factory;
|
|
|
|
|
|
|
|
use App\Entity\User;
|
|
|
|
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
|
|
|
|
use Symfony\Component\Security\Csrf\TokenGenerator\TokenGeneratorInterface;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* class UserFactory.
|
|
|
|
*
|
|
|
|
* @author Simon Vieille <simon@deblan.fr>
|
|
|
|
*/
|
2021-05-12 10:17:39 +02:00
|
|
|
class UserFactory implements FactoryInterface
|
2021-03-24 12:27:07 +01:00
|
|
|
{
|
|
|
|
protected TokenGeneratorInterface $tokenGenerator;
|
|
|
|
protected UserPasswordEncoderInterface $encoder;
|
|
|
|
|
|
|
|
public function __construct(TokenGeneratorInterface $tokenGenerator, UserPasswordEncoderInterface $encoder)
|
|
|
|
{
|
|
|
|
$this->tokenGenerator = $tokenGenerator;
|
|
|
|
$this->encoder = $encoder;
|
|
|
|
}
|
|
|
|
|
2021-04-30 09:36:48 +02:00
|
|
|
public function create(?string $email = null, ?string $password = null): User
|
2021-03-24 12:27:07 +01:00
|
|
|
{
|
|
|
|
$entity = new User();
|
|
|
|
|
2021-04-30 09:36:48 +02:00
|
|
|
if (!empty($email)) {
|
|
|
|
$entity->setEmail($email);
|
|
|
|
}
|
|
|
|
|
2021-03-24 12:27:07 +01:00
|
|
|
$entity->setPassword($this->encoder->encodePassword(
|
|
|
|
$entity,
|
2021-04-30 09:36:48 +02:00
|
|
|
!empty($password) ? $password : $this->tokenGenerator->generateToken()
|
2021-03-24 12:27:07 +01:00
|
|
|
));
|
|
|
|
|
|
|
|
return $entity;
|
|
|
|
}
|
|
|
|
}
|