previewGenerator = $previewGenerator; $this->urlShortener = $urlShortener; $this->logger = $logger ?: new NullLogger(); } /** * Process an incoming server request and return a response, optionally delegating * to the next middleware component to create the response. * * @param Request $request * @param RequestHandlerInterface $handler * * @return Response */ public function process(Request $request, RequestHandlerInterface $handler): Response { $shortCode = $request->getAttribute('shortCode'); try { $url = $this->urlShortener->shortCodeToUrl($shortCode); $imagePath = $this->previewGenerator->generatePreview($url->getLongUrl()); return $this->generateImageResponse($imagePath); } catch (InvalidShortCodeException | EntityDoesNotExistException | PreviewGenerationException $e) { $this->logger->warning('An error occurred while generating preview image.' . PHP_EOL . $e); return $this->buildErrorResponse($request, $handler); } } }