Skip to content

Commit

Permalink
feat(User): add email,username,password to DTO/Mapper
Browse files Browse the repository at this point in the history
  • Loading branch information
n3wborn committed Jan 14, 2024
1 parent 62a1120 commit 7efc65b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
28 changes: 27 additions & 1 deletion src/Service/User/UserDTO.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
final class UserDTO
{
public function __construct(
private string $email = '',
private ?string $slug = null,
private ?string $password = null,
private string $email,
private string $username,
private array $projects = [],
) {
}
Expand Down Expand Up @@ -35,6 +37,30 @@ public function setEmail(string $email): self
return $this;
}

public function getUsername(): string
{
return $this->username;
}

public function setUsername(string $username): self
{
$this->username = $username;

return $this;
}

public function getPassword(): ?string
{
return $this->password;
}

public function setPassword(?string $password): self
{
$this->password = $password;

return $this;
}

public function getProjects(): array
{
return $this->projects;
Expand Down
4 changes: 3 additions & 1 deletion src/Service/User/UserMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ public static function fromEntityToJson(User $user): mixed
public static function fromEntityToDTO(User $user): UserDTO
{
return new UserDTO(
$user->getEmail(),
$user->getSlug(),
$user->getPassword(),
$user->getEmail(),
$user->getUsername(),
UserHelper::getProjectsArrayFromUser($user)
);
}
Expand Down
9 changes: 8 additions & 1 deletion src/Service/User/UserPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;

final class UserPersister
{
Expand All @@ -20,6 +21,7 @@ public function __construct(
private EntityManagerInterface $em,
private UserHelper $helper,
private ExceptionLogger $logger,
private UserPasswordHasherInterface $passwordHasher,
) {
}

Expand Down Expand Up @@ -47,7 +49,12 @@ public function processRequest(User $project, UserDTO $dto, Request $request): J

public function persist(User $user, UserDTO $dto): void
{
$user->setEmail($dto->getEmail());
$user
->setEmail($dto->getEmail())
->setUsername($dto->getUsername());

$dto->getPassword()
&& $user->setPassword($this->passwordHasher->hashPassword($user, $dto->getPassword()));

foreach ($user->getProjects() as $project) {
$user->removeProject($project);
Expand Down

0 comments on commit 7efc65b

Please sign in to comment.