feat: add image upload and start-match handlers
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace BattleForge\Http\Handlers;
|
||||
|
||||
use BattleForge\Application\ScenarioDraft;
|
||||
use BattleForge\Application\ScenarioSerializer;
|
||||
use BattleForge\Domain\ScenarioValidator;
|
||||
use BattleForge\Http\CsrfToken;
|
||||
use BattleForge\Http\Request;
|
||||
use BattleForge\Http\Response;
|
||||
|
||||
final class PostStartMatch
|
||||
{
|
||||
/** @param array<string, string> $params */
|
||||
public function handle(Request $request, array $params): Response
|
||||
{
|
||||
$submitted = (string) ($request->server['HTTP_X_CSRF_TOKEN'] ?? '');
|
||||
$expected = (string) ($request->cookies['__csrf'] ?? '');
|
||||
$secret = (string) ($request->server['__csrf_secret'] ?? '');
|
||||
|
||||
if ($secret === '' || $expected === '' || !CsrfToken::verify($submitted, $secret, $expected)) {
|
||||
return Response::json(403, ['error' => 'csrf']);
|
||||
}
|
||||
|
||||
try {
|
||||
$payload = json_decode($request->rawBody, true, flags: JSON_THROW_ON_ERROR);
|
||||
$draft = ScenarioDraft::fromPost($payload);
|
||||
$scenario = $draft->toScenario();
|
||||
ScenarioValidator::validate($scenario);
|
||||
$match = $scenario->startMatch('alpha');
|
||||
} catch (\JsonException $exception) {
|
||||
return Response::json(400, ['errors' => ['Malformed JSON.']]);
|
||||
} catch (\InvalidArgumentException $exception) {
|
||||
return Response::json(400, ['errors' => [$exception->getMessage()]]);
|
||||
}
|
||||
|
||||
return Response::json(200, ['match' => ScenarioSerializer::matchToArray($match)]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user