Last active
December 25, 2022 20:06
-
-
Save ismail1432/37df494c0badb0f986a95d981522f44f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Application; | |
use App\Domain\CreateAUserHandler; | |
use App\Domain\CreateAUserInput; | |
use Symfony\Component\HttpFoundation\JsonResponse; | |
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; | |
use Symfony\Component\Routing\Annotation\Route; | |
use Symfony\Component\HttpFoundation\Request; | |
use Symfony\Component\Validator\Validator\ValidatorInterface; | |
final class CreateAUserAction | |
{ | |
private ValidatorInterface $validator; | |
private CreateAUserHandler $handler; | |
public function __construct(ValidatorInterface $validator, CreateAUserHandler $handler) | |
{ | |
$this->validator = $validator; | |
$this->handler = $handler; | |
} | |
/** | |
* @Route("/users", name="user_post", methods={"POST"})) | |
*/ | |
public function __invoke(Request $request) | |
{ | |
$payload = new CreateAUserPayload($request->request('name'), $request->request('age')); | |
$errors = $this->validator->validate($payload); | |
if ($errors->count() > 0) { | |
throw new BadRequestHttpException("Invalid payload blabla..."); | |
} | |
$input = new CreateAUserInput($payload->name, $payload->age); | |
$output = $this->handler->execute($input); | |
return new JsonResponse([ | |
'id' => $output->getId(), | |
'name' => $output->getName(), | |
'age' => $output->getAge(), | |
], 201); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment