shlink/module/Core/src/Action/QrCodeAction.php

91 lines
2.9 KiB
PHP
Raw Normal View History

2016-08-09 11:24:42 +03:00
<?php
2017-10-12 11:13:20 +03:00
declare(strict_types=1);
2016-08-09 11:24:42 +03:00
namespace Shlinkio\Shlink\Core\Action;
use Endroid\QrCode\QrCode;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
2018-03-26 19:49:28 +03:00
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
2016-08-09 11:24:42 +03:00
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Shlinkio\Shlink\Common\Response\QrCodeResponse;
use Shlinkio\Shlink\Core\Action\Util\ErrorResponseBuilderTrait;
use Shlinkio\Shlink\Core\Exception\EntityDoesNotExistException;
2016-08-09 11:24:42 +03:00
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException;
use Shlinkio\Shlink\Core\Service\UrlShortenerInterface;
2018-05-07 11:58:49 +03:00
use Zend\Expressive\Router\Exception\RuntimeException;
2016-08-09 11:24:42 +03:00
use Zend\Expressive\Router\RouterInterface;
class QrCodeAction implements MiddlewareInterface
{
use ErrorResponseBuilderTrait;
2018-09-15 13:56:17 +03:00
private const DEFAULT_SIZE = 300;
private const MIN_SIZE = 50;
private const MAX_SIZE = 1000;
/** @var RouterInterface */
2016-08-09 11:24:42 +03:00
private $router;
/** @var UrlShortenerInterface */
2016-08-09 11:24:42 +03:00
private $urlShortener;
/** @var LoggerInterface */
2016-08-09 11:24:42 +03:00
private $logger;
public function __construct(
RouterInterface $router,
UrlShortenerInterface $urlShortener,
?LoggerInterface $logger = null
2016-08-09 11:24:42 +03:00
) {
$this->router = $router;
$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.
2016-08-09 11:24:42 +03:00
*
* @param Request $request
2018-03-26 19:49:28 +03:00
* @param RequestHandlerInterface $handler
*
* @return Response
2018-05-07 11:58:49 +03:00
* @throws \InvalidArgumentException
* @throws RuntimeException
2016-08-09 11:24:42 +03:00
*/
2018-03-26 19:49:28 +03:00
public function process(Request $request, RequestHandlerInterface $handler): Response
2016-08-09 11:24:42 +03:00
{
// Make sure the short URL exists for this short code
$shortCode = $request->getAttribute('shortCode');
try {
$this->urlShortener->shortCodeToUrl($shortCode);
2018-05-07 11:58:49 +03:00
} catch (InvalidShortCodeException | EntityDoesNotExistException $e) {
$this->logger->warning('An error occurred while creating QR code. {e}', ['e' => $e]);
2018-03-26 19:49:28 +03:00
return $this->buildErrorResponse($request, $handler);
2016-08-09 11:24:42 +03:00
}
$path = $this->router->generateUri('long-url-redirect', ['shortCode' => $shortCode]);
$size = $this->getSizeParam($request);
$qrCode = new QrCode((string) $request->getUri()->withPath($path)->withQuery(''));
2016-08-09 11:24:42 +03:00
$qrCode->setSize($size)
->setPadding(0);
return new QrCodeResponse($qrCode);
}
/**
* @param Request $request
* @return int
*/
2018-03-28 00:56:55 +03:00
private function getSizeParam(Request $request): int
2016-08-09 11:24:42 +03:00
{
2018-09-15 13:56:17 +03:00
$size = (int) $request->getAttribute('size', self::DEFAULT_SIZE);
if ($size < self::MIN_SIZE) {
return self::MIN_SIZE;
2016-08-09 11:24:42 +03:00
}
2018-09-15 13:56:17 +03:00
return $size > self::MAX_SIZE ? self::MAX_SIZE : $size;
2016-08-09 11:24:42 +03:00
}
}