Skip to content

Commit

Permalink
refactor registration controller
Browse files Browse the repository at this point in the history
  • Loading branch information
ClaudioVarandas committed Apr 16, 2024
1 parent 58cceb8 commit 7721594
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 21 deletions.
18 changes: 2 additions & 16 deletions stocks-api/src/Controller/RegistrationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,19 @@ class RegistrationController extends AbstractController
{
public function __invoke(
#[MapRequestPayload] RegistrationDto $createUserDto,
ManagerRegistry $doctrine,
Request $request,
UserPasswordHasherInterface $passwordHasher,
UserRepository $userRepository
): JsonResponse {
$em = $doctrine->getManager();

$user = $userRepository->findOneBy(['email' => $createUserDto->email]);

if ($user) {
return $this->json(
['message' => 'User already exist with that criteria.'],
['message' => 'Account already exist.'],
Response::HTTP_BAD_REQUEST
);
}

$user = new User();
$hashedPassword = $passwordHasher->hashPassword(
$user,
$createUserDto->password
);
$user->setPassword($hashedPassword);
$user->setEmail($createUserDto->email);
$user->setName($createUserDto->name);

$em->persist($user);
$em->flush();
$userRepository->createUser($createUserDto);

return $this->json(['message' => 'Registered Successfully']);
}
Expand Down
31 changes: 27 additions & 4 deletions stocks-api/src/Repository/UserRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

namespace App\Repository;

use App\Dto\RegistrationDto;
use App\Entity\User;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Persistence\ObjectManager;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
Expand All @@ -19,9 +22,14 @@
*/
class UserRepository extends ServiceEntityRepository implements PasswordUpgraderInterface
{
public function __construct(ManagerRegistry $registry)
{
private ObjectManager $em;
public function __construct(
ManagerRegistry $registry,
private readonly UserPasswordHasherInterface $passwordHasher,
) {
parent::__construct($registry, User::class);

$this->em = $this->getEntityManager();
}

/**
Expand All @@ -34,7 +42,22 @@ public function upgradePassword(PasswordAuthenticatedUserInterface $user, string
}

$user->setPassword($newHashedPassword);
$this->getEntityManager()->persist($user);
$this->getEntityManager()->flush();
$this->em->persist($user);
$this->em->flush();
}

public function createUser(RegistrationDto $createUserDto): void
{
$user = new User();
$hashedPassword = $this->passwordHasher->hashPassword(
$user,
$createUserDto->password
);
$user->setPassword($hashedPassword);
$user->setEmail($createUserDto->email);
$user->setName($createUserDto->name);

$this->em->persist($user);
$this->em->flush();
}
}
3 changes: 2 additions & 1 deletion stocks-api/tests/Controller/RegistrationControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function testItCanRegisterUser(): void

$this->assertResponseIsSuccessful();


$response = $client->getResponse();
$data = $response->getContent();

Expand Down Expand Up @@ -50,6 +51,6 @@ public function testItShouldReturnBadRequestIfEmailAlreadyExists(): void

$this->assertResponseStatusCodeSame(400);

$this->assertStringContainsString('{"message":"User already exist with that criteria."}', $data);
$this->assertStringContainsString('{"message":"Account already exist."}', $data);
}
}

0 comments on commit 7721594

Please sign in to comment.