This commit is contained in:
Alejandro Celaya 2020-11-27 17:05:13 +01:00
parent 74108a19e5
commit a72dc16d85
2 changed files with 18 additions and 5 deletions

View File

@ -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,
],

View File

@ -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;
}