Completed
Pull Request — master (#148)
by Alejandro
04:22
created

QrCodeAction   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 95.45%

Importance

Changes 0
Metric Value
dl 0
loc 72
ccs 21
cts 22
cp 0.9545
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getSizeParam() 0 9 3
A __construct() 0 9 2
A process() 0 19 2
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();
0 ignored issues
show
Documentation Bug introduced by
It seems like $logger ?: new \Psr\Log\NullLogger() can also be of type object<Psr\Log\NullLogger>. However, the property $logger is declared as type object<Psr\Log\LoggerInterface>. Maybe add an additional type check?

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 $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. 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.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
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