*/ protected array $args; public function __construct(LoggerInterface $logger) { $this->logger = $logger; } /** * @param array $args * * @throws HttpNotFoundException * @throws HttpBadRequestException */ public function __invoke(Request $request, Response $response, array $args): Response { $this->request = $request; $this->response = $response; $this->args = $args; try { return $this->action(); } catch (DomainRecordNotFoundException $e) { throw new HttpNotFoundException($this->request, $e->getMessage()); } } /** * @throws DomainRecordNotFoundException * @throws HttpBadRequestException */ abstract protected function action(): Response; /** * @return array|object|null */ protected function getFormData() { /** @var array|object|null $body */ $body = $this->request->getParsedBody(); return $body; } /** * @return mixed * @throws HttpBadRequestException */ protected function resolveArg(string $name) { if (!isset($this->args[$name])) { throw new HttpBadRequestException($this->request, "Could not resolve argument `{$name}`."); } return $this->args[$name]; } /** * @param array|object|null $data */ protected function respondWithData($data = null, int $statusCode = 200): Response { $payload = new ActionPayload($statusCode, $data); return $this->respond($payload); } protected function respond(ActionPayload $payload): Response { $json = (string) json_encode($payload, JSON_PRETTY_PRINT); $this->response->getBody()->write($json); return $this->response ->withHeader('Content-Type', 'application/json') ->withStatus($payload->getStatusCode()); } }