urlShortener = $urlShortener; $this->translator = $translator; $this->domainConfig = $domainConfig; } /** * @param Request $request * @param Response $response * @param callable|null $out * @return null|Response */ public function dispatch(Request $request, Response $response, callable $out = null) { $postData = $request->getParsedBody(); if (! isset($postData['longUrl'])) { return new JsonResponse([ 'error' => RestUtils::INVALID_ARGUMENT_ERROR, 'message' => $this->translator->translate('A URL was not provided'), ], 400); } $longUrl = $postData['longUrl']; $tags = isset($postData['tags']) && is_array($postData['tags']) ? $postData['tags'] : []; try { $shortCode = $this->urlShortener->urlToShortCode(new Uri($longUrl), $tags); $shortUrl = (new Uri())->withPath($shortCode) ->withScheme($this->domainConfig['schema']) ->withHost($this->domainConfig['hostname']); return new JsonResponse([ 'longUrl' => $longUrl, 'shortUrl' => $shortUrl->__toString(), 'shortCode' => $shortCode, ]); } catch (InvalidUrlException $e) { $this->logger->warning('Provided Invalid URL.' . PHP_EOL . $e); return new JsonResponse([ 'error' => RestUtils::getRestErrorCodeFromException($e), 'message' => sprintf( $this->translator->translate('Provided URL %s is invalid. Try with a different one.'), $longUrl ), ], 400); } catch (\Exception $e) { $this->logger->error('Unexpected error creating shortcode.' . PHP_EOL . $e); return new JsonResponse([ 'error' => RestUtils::UNKNOWN_ERROR, 'message' => $this->translator->translate('Unexpected error occurred'), ], 500); } } }