previewGenerator = $previewGenerator; $this->urlShortener = $urlShortener; } /** * Process an incoming server request and return a response, optionally delegating * to the next middleware component to create the response. * * @param Request $request * @param DelegateInterface $delegate * * @return Response */ public function process(Request $request, DelegateInterface $delegate) { $shortCode = $request->getAttribute('shortCode'); try { $url = $this->urlShortener->shortCodeToUrl($shortCode); if (! isset($url)) { return $delegate->process($request); } $imagePath = $this->previewGenerator->generatePreview($url); return $this->generateImageResponse($imagePath); } catch (InvalidShortCodeException $e) { return $delegate->process($request); } } }