Completed
Push — master ( f7424d...b53e51 )
by Alejandro
07:43
created

QrCodeAction::__invoke()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 15
nc 4
nop 3
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
ccs 14
cts 14
cp 1
crap 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A QrCodeAction::getSizeParam() 0 9 3
1
<?php
2
namespace Shlinkio\Shlink\Core\Action;
3
4
use Acelaya\ZsmAnnotatedServices\Annotation\Inject;
5
use Endroid\QrCode\QrCode;
6
use Interop\Http\ServerMiddleware\DelegateInterface;
7
use Interop\Http\ServerMiddleware\MiddlewareInterface;
8
use Psr\Http\Message\ResponseInterface as Response;
9
use Psr\Http\Message\ServerRequestInterface as Request;
10
use Psr\Log\LoggerInterface;
11
use Psr\Log\NullLogger;
12
use Shlinkio\Shlink\Common\Response\QrCodeResponse;
13
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException;
14
use Shlinkio\Shlink\Core\Service\UrlShortener;
15
use Shlinkio\Shlink\Core\Service\UrlShortenerInterface;
16
use Zend\Expressive\Router\RouterInterface;
17
18
class QrCodeAction implements MiddlewareInterface
19
{
20
    /**
21
     * @var RouterInterface
22
     */
23
    private $router;
24
    /**
25
     * @var UrlShortenerInterface
26
     */
27
    private $urlShortener;
28
    /**
29
     * @var LoggerInterface
30
     */
31
    private $logger;
32
33
    /**
34
     * QrCodeAction constructor.
35
     * @param RouterInterface $router
36
     * @param UrlShortenerInterface $urlShortener
37
     * @param LoggerInterface $logger
38
     *
39
     * @Inject({RouterInterface::class, UrlShortener::class, "Logger_Shlink"})
40
     */
41 3
    public function __construct(
42
        RouterInterface $router,
43
        UrlShortenerInterface $urlShortener,
44
        LoggerInterface $logger = null
45
    ) {
46 3
        $this->router = $router;
47 3
        $this->urlShortener = $urlShortener;
48 3
        $this->logger = $logger ?: new NullLogger();
49 3
    }
50
51
    /**
52
     * Process an incoming server request and return a response, optionally delegating
53
     * to the next middleware component to create the response.
54
     *
55
     * @param Request $request
56
     * @param DelegateInterface $delegate
57
     *
58
     * @return Response
59
     */
60 3
    public function process(Request $request, DelegateInterface $delegate)
61
    {
62
        // Make sure the short URL exists for this short code
63 3
        $shortCode = $request->getAttribute('shortCode');
64
        try {
65 3
            $shortUrl = $this->urlShortener->shortCodeToUrl($shortCode);
66 2
            if ($shortUrl === null) {
67 1
                return $delegate->process($request);
68
            }
69 2
        } catch (InvalidShortCodeException $e) {
70 1
            $this->logger->warning('Tried to create a QR code with an invalid short code' . PHP_EOL . $e);
71 1
            return $delegate->process($request);
72
        }
73
74 1
        $path = $this->router->generateUri('long-url-redirect', ['shortCode' => $shortCode]);
75 1
        $size = $this->getSizeParam($request);
76
77 1
        $qrCode = new QrCode($request->getUri()->withPath($path)->withQuery(''));
78 1
        $qrCode->setSize($size)
79 1
               ->setPadding(0);
80 1
        return new QrCodeResponse($qrCode);
81
    }
82
83
    /**
84
     * @param Request $request
85
     * @return int
86
     */
87 1
    protected function getSizeParam(Request $request)
88
    {
89 1
        $size = (int) $request->getAttribute('size', 300);
90 1
        if ($size < 50) {
91
            return 50;
92
        }
93
94 1
        return $size > 1000 ? 1000 : $size;
95
    }
96
}
97