shortUrlService = $shortUrlService; $this->translator = $translator; } /** * Process an incoming server request and return a response, optionally delegating * to the next middleware component to create the response. * * @param ServerRequestInterface $request * * @return ResponseInterface * @throws \InvalidArgumentException */ public function handle(ServerRequestInterface $request): ResponseInterface { $postData = (array) $request->getParsedBody(); $shortCode = $request->getAttribute('shortCode', ''); try { $this->shortUrlService->updateMetadataByShortCode( $shortCode, ShortUrlMeta::createFromRawData($postData) ); return new EmptyResponse(); } catch (Exception\InvalidShortCodeException $e) { $this->logger->warning('Provided data is invalid.' . PHP_EOL . $e); return new JsonResponse([ 'error' => RestUtils::getRestErrorCodeFromException($e), 'message' => \sprintf($this->translator->translate('No URL found for short code "%s"'), $shortCode), ], self::STATUS_NOT_FOUND); } catch (Exception\ValidationException $e) { $this->logger->warning('Provided data is invalid.' . PHP_EOL . $e); return new JsonResponse([ 'error' => RestUtils::getRestErrorCodeFromException($e), 'message' => $this->translator->translate('Provided data is invalid.'), ], self::STATUS_BAD_REQUEST); } } }