diff --git a/module/Core/config/routes.config.php b/module/Core/config/routes.config.php index 82abef30..a95e8e96 100644 --- a/module/Core/config/routes.config.php +++ b/module/Core/config/routes.config.php @@ -29,7 +29,17 @@ return [ ], [ 'name' => Action\QrCodeAction::class, - 'path' => '/{shortCode}/qr-code[/{size:[0-9]+}]', + 'path' => '/{shortCode}/qr-code', + 'middleware' => [ + Action\QrCodeAction::class, + ], + 'allowed_methods' => [RequestMethod::METHOD_GET], + ], + + // Deprecated + [ + 'name' => 'old_' . Action\QrCodeAction::class, + 'path' => '/{shortCode}/qr-code/{size:[0-9]+}', 'middleware' => [ Action\QrCodeAction::class, ], diff --git a/module/Core/src/Action/QrCodeAction.php b/module/Core/src/Action/QrCodeAction.php index 4a8b7db5..0fb5089b 100644 --- a/module/Core/src/Action/QrCodeAction.php +++ b/module/Core/src/Action/QrCodeAction.php @@ -48,11 +48,15 @@ class QrCodeAction implements MiddlewareInterface return $handler->handle($request); } + $query = $request->getQueryParams(); + // Size attribute is deprecated + $size = $this->normalizeSize($request->getAttribute('size', $query['size'] ?? self::DEFAULT_SIZE)); + $qrCode = new QrCode($shortUrl->toString($this->domainConfig)); - $qrCode->setSize($this->getSizeParam($request)); + $qrCode->setSize($size); $qrCode->setMargin(0); - $format = $request->getQueryParams()['format'] ?? 'png'; + $format = $query['format'] ?? 'png'; if ($format === 'svg') { $qrCode->setWriter(new SvgWriter()); } @@ -60,9 +64,8 @@ class QrCodeAction implements MiddlewareInterface return new QrCodeResponse($qrCode); } - private function getSizeParam(Request $request): int + private function normalizeSize(int $size): int { - $size = (int) $request->getAttribute('size', self::DEFAULT_SIZE); if ($size < self::MIN_SIZE) { return self::MIN_SIZE; }