urlShortener = $urlShortener; $this->translator = $translator; } /** * @param Request $request * @param Response $response * @param callable|null $out * @return null|Response */ public function dispatch(Request $request, Response $response, callable $out = null) { $shortCode = $request->getAttribute('shortCode'); try { $longUrl = $this->urlShortener->shortCodeToUrl($shortCode); if (! isset($longUrl)) { return new JsonResponse([ 'error' => RestUtils::INVALID_ARGUMENT_ERROR, 'message' => sprintf($this->translator->translate('No URL found for short code "%s"'), $shortCode), ], 404); } return new JsonResponse([ 'longUrl' => $longUrl, ]); } catch (InvalidShortCodeException $e) { return new JsonResponse([ 'error' => RestUtils::getRestErrorCodeFromException($e), 'message' => sprintf( $this->translator->translate('Provided short code "%s" has an invalid format'), $shortCode ), ], 400); } catch (\Exception $e) { return new JsonResponse([ 'error' => RestUtils::UNKNOWN_ERROR, 'message' => $this->translator->translate('Unexpected error occurred'), ], 500); } } }