Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
/**
* @Route('/users', methods={'POST'})
*/
class RegisterUserController
{
private RegisterUserService $service;
public function __construct(RegisterUserService $service)
{
$this->service = $service;
}
public function __invoke(Request $request): JsonResponse
{
try {
$registerUser = new RegisterUser(
$request->get('name'),
new Email($request->get('email')),
new Password($request->get('password')),
);
} catch (InvalidName | InvalidEmail | WeakPassword $e) {
throw new HttpException(Response::HTTP_BAD_REQUEST, $e->getMessage(), $e);
}
try {
$userRegistered = $this->service->register($registerUser);
} catch (UserAlreadyRegistered $e) {
throw new HttpException(Response::HTTP_CONFLICT, $e->getMessage(), $e);
}
return new JsonResponse([
'id' => $userRegistered->getId(),
]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.