DefaultShortCodesLengthMiddleware::process()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Shlinkio\Shlink\Rest\Middleware\ShortUrl;
6
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Http\Message\ServerRequestInterface;
9
use Psr\Http\Server\MiddlewareInterface;
10
use Psr\Http\Server\RequestHandlerInterface;
11
use Shlinkio\Shlink\Core\Validation\ShortUrlMetaInputFilter;
12
13
class DefaultShortCodesLengthMiddleware implements MiddlewareInterface
14
{
15
    private int $defaultShortCodesLength;
16
17 3
    public function __construct(int $defaultShortCodesLength)
18
    {
19 3
        $this->defaultShortCodesLength = $defaultShortCodesLength;
20 3
    }
21
22 3
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
23
    {
24 3
        $body = $request->getParsedBody();
25 3
        if (! isset($body[ShortUrlMetaInputFilter::SHORT_CODE_LENGTH])) {
26 2
            $body[ShortUrlMetaInputFilter::SHORT_CODE_LENGTH] = $this->defaultShortCodesLength;
27
        }
28
29 3
        return $handler->handle($request->withParsedBody($body));
30
    }
31
}
32