slovocast/app/src/Controller/User/RegisterUserAction.php

42 lines
1.1 KiB
PHP

<?php
namespace Slovocast\Controller\User;
use Psr\Http\Message\ResponseInterface as Response;
use Slovocast\Controller\Controller;
use Slovocast\Domain\Entity\User;
use Slovocast\Domain\Repository\UserRepositoryInterface;
class RegisterUserAction extends Controller
{
public function __construct(
protected UserRepositoryInterface $userRepository
) { }
public function respond(): Response
{
// get user Data
$requestData = $this->request->getParsedBody();
if ($requestData['password'] !== $requestData['checked_password']) {
$response = $this->render('user/register.twig');
return $response->withStatus(400);
}
$user = User::fromArray([
'email' => $requestData['email'],
'name' => $requestData['name'],
'password' => $requestData['password'],
]);
$result = $this->userRepository->save($user);
if ($result) {
return $this->render('user/success.twig');
} else {
return $this->render('user/register.twig')->withStatus(400);
}
}
}