2016-08-09 11:24:42 +03:00
|
|
|
<?php
|
|
|
|
namespace Shlinkio\Shlink\Core\Action;
|
|
|
|
|
|
|
|
use Endroid\QrCode\QrCode;
|
2017-03-25 01:19:42 +03:00
|
|
|
use Interop\Http\ServerMiddleware\DelegateInterface;
|
|
|
|
use Interop\Http\ServerMiddleware\MiddlewareInterface;
|
2016-08-09 11:24:42 +03:00
|
|
|
use Psr\Http\Message\ResponseInterface as Response;
|
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
use Psr\Log\NullLogger;
|
|
|
|
use Shlinkio\Shlink\Common\Response\QrCodeResponse;
|
|
|
|
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException;
|
|
|
|
use Shlinkio\Shlink\Core\Service\UrlShortenerInterface;
|
|
|
|
use Zend\Expressive\Router\RouterInterface;
|
|
|
|
|
|
|
|
class QrCodeAction implements MiddlewareInterface
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var RouterInterface
|
|
|
|
*/
|
|
|
|
private $router;
|
|
|
|
/**
|
|
|
|
* @var UrlShortenerInterface
|
|
|
|
*/
|
|
|
|
private $urlShortener;
|
|
|
|
/**
|
|
|
|
* @var LoggerInterface
|
|
|
|
*/
|
|
|
|
private $logger;
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
RouterInterface $router,
|
|
|
|
UrlShortenerInterface $urlShortener,
|
|
|
|
LoggerInterface $logger = null
|
|
|
|
) {
|
|
|
|
$this->router = $router;
|
|
|
|
$this->urlShortener = $urlShortener;
|
|
|
|
$this->logger = $logger ?: new NullLogger();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-03-25 01:19:42 +03:00
|
|
|
* 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
|
2017-03-25 01:19:42 +03:00
|
|
|
* @param DelegateInterface $delegate
|
|
|
|
*
|
|
|
|
* @return Response
|
2016-08-09 11:24:42 +03:00
|
|
|
*/
|
2017-03-25 01:19:42 +03:00
|
|
|
public function process(Request $request, DelegateInterface $delegate)
|
2016-08-09 11:24:42 +03:00
|
|
|
{
|
|
|
|
// Make sure the short URL exists for this short code
|
|
|
|
$shortCode = $request->getAttribute('shortCode');
|
|
|
|
try {
|
|
|
|
$shortUrl = $this->urlShortener->shortCodeToUrl($shortCode);
|
2017-03-25 01:19:42 +03:00
|
|
|
if ($shortUrl === null) {
|
|
|
|
return $delegate->process($request);
|
2016-08-09 11:24:42 +03:00
|
|
|
}
|
|
|
|
} catch (InvalidShortCodeException $e) {
|
|
|
|
$this->logger->warning('Tried to create a QR code with an invalid short code' . PHP_EOL . $e);
|
2017-03-25 01:19:42 +03:00
|
|
|
return $delegate->process($request);
|
2016-08-09 11:24:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$path = $this->router->generateUri('long-url-redirect', ['shortCode' => $shortCode]);
|
|
|
|
$size = $this->getSizeParam($request);
|
|
|
|
|
|
|
|
$qrCode = new QrCode($request->getUri()->withPath($path)->withQuery(''));
|
|
|
|
$qrCode->setSize($size)
|
|
|
|
->setPadding(0);
|
|
|
|
return new QrCodeResponse($qrCode);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Request $request
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
protected function getSizeParam(Request $request)
|
|
|
|
{
|
2017-03-25 01:19:42 +03:00
|
|
|
$size = (int) $request->getAttribute('size', 300);
|
2016-08-09 11:24:42 +03:00
|
|
|
if ($size < 50) {
|
|
|
|
return 50;
|
|
|
|
}
|
|
|
|
|
2017-03-25 01:19:42 +03:00
|
|
|
return $size > 1000 ? 1000 : $size;
|
2016-08-09 11:24:42 +03:00
|
|
|
}
|
|
|
|
}
|