Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
/**
* @Route("", name="", methods={"POST"})
* @Permissions(adminPermissions=true)
*
* @param Task $task
* @param Request $request
* @param Publish\Handler $handler
* @return JsonResponse
* @throws \Symfony\Component\Serializer\Exception\ExceptionInterface
*/
public function publish(Task $task, Request $request, Publish\Handler $handler): JsonResponse
{
/** @var Publish\Command $command */
$command = $this->denormalizer->denormalize(
$request->request->all(),
Publish\Command::class,
'array',
[
'ignored_attributes' => ['user', 'task']
]
);
$command->user = $this->getUser();
$command->task = $task;
$violations = $this->validator->validate($command);
$errors = [];
if (\count($violations) > 0) {
/** @var ConstraintViolation $violation */
foreach ($violations as $violation) {
$errors[] = $violation->getMessage();
}
return $this->json(['error' => $errors], 200);
}
try {
$handler->handle($command);
return $this->json(['success' => 1], 200);
} catch (\DomainException $e) {
$this->logger->warning($e->getMessage());
return $this->json(['success' => 0], 500);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment