Completed
Push — master ( 9ab4b9...065cdd )
by Alejandro
09:44
created

QrCodeAction   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 9

Test Coverage

Coverage 92.31%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 97
ccs 24
cts 26
cp 0.9231
rs 10
wmc 8
lcom 0
cbo 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A __invoke() 0 22 3
A getSizeParam() 0 11 3
1
<?php
2
namespace Shlinkio\Shlink\Core\Action;
3
4
use Acelaya\ZsmAnnotatedServices\Annotation\Inject;
5
use Endroid\QrCode\QrCode;
6
use Psr\Http\Message\ResponseInterface as Response;
7
use Psr\Http\Message\ServerRequestInterface as Request;
8
use Psr\Log\LoggerInterface;
9
use Psr\Log\NullLogger;
10
use Shlinkio\Shlink\Common\Response\QrCodeResponse;
11
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException;
12
use Shlinkio\Shlink\Core\Service\UrlShortener;
13
use Shlinkio\Shlink\Core\Service\UrlShortenerInterface;
14
use Zend\Expressive\Router\RouterInterface;
15
use Zend\Stratigility\MiddlewareInterface;
16
17
class QrCodeAction implements MiddlewareInterface
18
{
19
    /**
20
     * @var RouterInterface
21
     */
22
    private $router;
23
    /**
24
     * @var UrlShortenerInterface
25
     */
26
    private $urlShortener;
27
    /**
28
     * @var LoggerInterface
29
     */
30
    private $logger;
31
32
    /**
33
     * QrCodeAction constructor.
34
     * @param RouterInterface $router
35
     * @param UrlShortenerInterface $urlShortener
36
     * @param LoggerInterface $logger
37
     *
38
     * @Inject({RouterInterface::class, UrlShortener::class, "Logger_Shlink"})
39
     */
40 3
    public function __construct(
41
        RouterInterface $router,
42
        UrlShortenerInterface $urlShortener,
43
        LoggerInterface $logger = null
44
    ) {
45 3
        $this->router = $router;
46 3
        $this->urlShortener = $urlShortener;
47 3
        $this->logger = $logger ?: new NullLogger();
48 3
    }
49
50
    /**
51
     * Process an incoming request and/or response.
52
     *
53
     * Accepts a server-side request and a response instance, and does
54
     * something with them.
55
     *
56
     * If the response is not complete and/or further processing would not
57
     * interfere with the work done in the middleware, or if the middleware
58
     * wants to delegate to another process, it can use the `$out` callable
59
     * if present.
60
     *
61
     * If the middleware does not return a value, execution of the current
62
     * request is considered complete, and the response instance provided will
63
     * be considered the response to return.
64
     *
65
     * Alternately, the middleware may return a response instance.
66
     *
67
     * Often, middleware will `return $out();`, with the assumption that a
68
     * later middleware will return a response.
69
     *
70
     * @param Request $request
71
     * @param Response $response
72
     * @param null|callable $out
73
     * @return null|Response
74
     */
75 3
    public function __invoke(Request $request, Response $response, callable $out = null)
76
    {
77
        // Make sure the short URL exists for this short code
78 3
        $shortCode = $request->getAttribute('shortCode');
79
        try {
80 3
            $shortUrl = $this->urlShortener->shortCodeToUrl($shortCode);
81 2
            if (! isset($shortUrl)) {
82 1
                return $out($request, $response->withStatus(404), 'Not Found');
83
            }
84 2
        } catch (InvalidShortCodeException $e) {
85 1
            $this->logger->warning('Tried to create a QR code with an invalid short code' . PHP_EOL . $e);
86 1
            return $out($request, $response->withStatus(404), 'Not Found');
87
        }
88
89 1
        $path = $this->router->generateUri('long-url-redirect', ['shortCode' => $shortCode]);
90 1
        $size = $this->getSizeParam($request);
91
92 1
        $qrCode = new QrCode($request->getUri()->withPath($path)->withQuery(''));
93 1
        $qrCode->setSize($size)
94 1
               ->setPadding(0);
95 1
        return new QrCodeResponse($qrCode);
96
    }
97
98
    /**
99
     * @param Request $request
100
     * @return int
101
     */
102 1
    protected function getSizeParam(Request $request)
103
    {
104 1
        $size = intval($request->getAttribute('size', 300));
105 1
        if ($size < 50) {
106
            return 50;
107 1
        } elseif ($size > 1000) {
108
            return 1000;
109
        }
110
111 1
        return $size;
112
    }
113
}
114