public function execute(InputInterface $input, OutputInterface $output) { $shortCode = $input->getArgument('shortCode'); try { $longUrl = $this->urlShortener->shortCodeToUrl($shortCode); if (!isset($longUrl)) { $output->writeln(sprintf('<error>' . $this->translator->translate('No URL found for short code "%s"') . '</error>', $shortCode)); return; } $output->writeln(sprintf('%s <info>%s</info>', $this->translator->translate('Long URL:'), $longUrl)); } catch (InvalidShortCodeException $e) { $output->writeln(sprintf('<error>' . $this->translator->translate('Provided short code "%s" has an invalid format.') . '</error>', $shortCode)); } }
/** * Process an incoming request and/or response. * * Accepts a server-side request and a response instance, and does * something with them. * * If the response is not complete and/or further processing would not * interfere with the work done in the middleware, or if the middleware * wants to delegate to another process, it can use the `$out` callable * if present. * * If the middleware does not return a value, execution of the current * request is considered complete, and the response instance provided will * be considered the response to return. * * Alternately, the middleware may return a response instance. * * Often, middleware will `return $out();`, with the assumption that a * later middleware will return a response. * * @param Request $request * @param Response $response * @param null|callable $out * @return null|Response */ public function __invoke(Request $request, Response $response, callable $out = null) { $shortCode = $request->getAttribute('shortCode'); try { $url = $this->urlShortener->shortCodeToUrl($shortCode); if (!isset($url)) { return $out($request, $response->withStatus(404), 'Not found'); } $imagePath = $this->previewGenerator->generatePreview($url); return $this->generateImageResponse($imagePath); } catch (InvalidShortCodeException $e) { return $out($request, $response->withStatus(404), 'Not found'); } catch (PreviewGenerationException $e) { return $out($request, $response->withStatus(500), 'Preview generation error'); } }
/** * @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) { $this->logger->warning('Provided short code with invalid format.' . PHP_EOL . $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) { $this->logger->error('Unexpected error while resolving the URL behind a short code.' . PHP_EOL . $e); return new JsonResponse(['error' => RestUtils::UNKNOWN_ERROR, 'message' => $this->translator->translate('Unexpected error occurred')], 500); } }
/** * Process an incoming request and/or response. * * Accepts a server-side request and a response instance, and does * something with them. * * If the response is not complete and/or further processing would not * interfere with the work done in the middleware, or if the middleware * wants to delegate to another process, it can use the `$out` callable * if present. * * If the middleware does not return a value, execution of the current * request is considered complete, and the response instance provided will * be considered the response to return. * * Alternately, the middleware may return a response instance. * * Often, middleware will `return $out();`, with the assumption that a * later middleware will return a response. * * @param Request $request * @param Response $response * @param null|callable $out * @return null|Response */ public function __invoke(Request $request, Response $response, callable $out = null) { // Make sure the short URL exists for this short code $shortCode = $request->getAttribute('shortCode'); try { $shortUrl = $this->urlShortener->shortCodeToUrl($shortCode); if (!isset($shortUrl)) { return $out($request, $response->withStatus(404), 'Not Found'); } } catch (InvalidShortCodeException $e) { $this->logger->warning('Tried to create a QR code with an invalid short code' . PHP_EOL . $e); return $out($request, $response->withStatus(404), 'Not Found'); } $path = $this->router->generateUri('long-url-redirect', ['shortCode' => $shortCode]); $size = $this->getSizeParam($request); $qrCode = new QrCode($request->getUri()->withPath($path)->withQuery('')); $qrCode->setSize($size)->setPadding(0); return new QrCodeResponse($qrCode); }
/** * Process an incoming request and/or response. * * Accepts a server-side request and a response instance, and does * something with them. * * If the response is not complete and/or further processing would not * interfere with the work done in the middleware, or if the middleware * wants to delegate to another process, it can use the `$out` callable * if present. * * If the middleware does not return a value, execution of the current * request is considered complete, and the response instance provided will * be considered the response to return. * * Alternately, the middleware may return a response instance. * * Often, middleware will `return $out();`, with the assumption that a * later middleware will return a response. * * @param Request $request * @param Response $response * @param null|callable $out * @return null|Response */ public function __invoke(Request $request, Response $response, callable $out = null) { $shortCode = $request->getAttribute('shortCode', ''); try { $longUrl = $this->urlShortener->shortCodeToUrl($shortCode); // If provided shortCode does not belong to a valid long URL, dispatch next middleware, which will trigger // a not-found error if (!isset($longUrl)) { return $this->notFoundResponse($request, $response, $out); } // Track visit to this short code $this->visitTracker->track($shortCode, $request); // Return a redirect response to the long URL. // Use a temporary redirect to make sure browsers always hit the server for analytics purposes return new RedirectResponse($longUrl); } catch (\Exception $e) { // In case of error, dispatch 404 error $this->logger->error('Error redirecting to long URL.' . PHP_EOL . $e); return $this->notFoundResponse($request, $response, $out); } }