|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Shlinkio\Shlink\Core\Action; |
|
5
|
|
|
|
|
6
|
|
|
use Endroid\QrCode\QrCode; |
|
7
|
|
|
use Psr\Http\Message\ResponseInterface as Response; |
|
8
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
|
9
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
|
10
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
|
11
|
|
|
use Psr\Log\LoggerInterface; |
|
12
|
|
|
use Psr\Log\NullLogger; |
|
13
|
|
|
use Shlinkio\Shlink\Common\Response\QrCodeResponse; |
|
14
|
|
|
use Shlinkio\Shlink\Core\Action\Util\ErrorResponseBuilderTrait; |
|
15
|
|
|
use Shlinkio\Shlink\Core\Exception\EntityDoesNotExistException; |
|
16
|
|
|
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException; |
|
17
|
|
|
use Shlinkio\Shlink\Core\Service\UrlShortenerInterface; |
|
18
|
|
|
use Zend\Expressive\Router\Exception\RuntimeException; |
|
19
|
|
|
use Zend\Expressive\Router\RouterInterface; |
|
20
|
|
|
|
|
21
|
|
|
class QrCodeAction implements MiddlewareInterface |
|
22
|
|
|
{ |
|
23
|
|
|
use ErrorResponseBuilderTrait; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var RouterInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
private $router; |
|
29
|
|
|
/** |
|
30
|
|
|
* @var UrlShortenerInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
private $urlShortener; |
|
33
|
|
|
/** |
|
34
|
|
|
* @var LoggerInterface |
|
35
|
|
|
*/ |
|
36
|
|
|
private $logger; |
|
37
|
|
|
|
|
38
|
3 |
|
public function __construct( |
|
39
|
|
|
RouterInterface $router, |
|
40
|
|
|
UrlShortenerInterface $urlShortener, |
|
41
|
|
|
LoggerInterface $logger = null |
|
42
|
|
|
) { |
|
43
|
3 |
|
$this->router = $router; |
|
44
|
3 |
|
$this->urlShortener = $urlShortener; |
|
45
|
3 |
|
$this->logger = $logger ?: new NullLogger(); |
|
|
|
|
|
|
46
|
3 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Process an incoming server request and return a response, optionally delegating |
|
50
|
|
|
* to the next middleware component to create the response. |
|
51
|
|
|
* |
|
52
|
|
|
* @param Request $request |
|
53
|
|
|
* @param RequestHandlerInterface $handler |
|
54
|
|
|
* |
|
55
|
|
|
* @return Response |
|
56
|
|
|
* @throws \InvalidArgumentException |
|
57
|
|
|
* @throws RuntimeException |
|
58
|
|
|
*/ |
|
59
|
3 |
|
public function process(Request $request, RequestHandlerInterface $handler): Response |
|
60
|
|
|
{ |
|
61
|
|
|
// Make sure the short URL exists for this short code |
|
62
|
3 |
|
$shortCode = $request->getAttribute('shortCode'); |
|
63
|
|
|
try { |
|
64
|
3 |
|
$this->urlShortener->shortCodeToUrl($shortCode); |
|
65
|
2 |
|
} catch (InvalidShortCodeException | EntityDoesNotExistException $e) { |
|
66
|
2 |
|
$this->logger->warning('An error occurred while creating QR code' . PHP_EOL . $e); |
|
67
|
2 |
|
return $this->buildErrorResponse($request, $handler); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
1 |
|
$path = $this->router->generateUri('long-url-redirect', ['shortCode' => $shortCode]); |
|
71
|
1 |
|
$size = $this->getSizeParam($request); |
|
72
|
|
|
|
|
73
|
1 |
|
$qrCode = new QrCode((string) $request->getUri()->withPath($path)->withQuery('')); |
|
74
|
1 |
|
$qrCode->setSize($size) |
|
75
|
1 |
|
->setPadding(0); |
|
76
|
1 |
|
return new QrCodeResponse($qrCode); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param Request $request |
|
81
|
|
|
* @return int |
|
82
|
|
|
*/ |
|
83
|
1 |
|
private function getSizeParam(Request $request): int |
|
84
|
|
|
{ |
|
85
|
1 |
|
$size = (int) $request->getAttribute('size', 300); |
|
86
|
1 |
|
if ($size < 50) { |
|
87
|
|
|
return 50; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
1 |
|
return $size > 1000 ? 1000 : $size; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.