Completed
Pull Request — develop (#653)
by Alejandro
05:03
created

__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 1
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
12
class DropDefaultDomainFromRequestMiddleware implements MiddlewareInterface
13
{
14
    private string $defaultDomain;
15
16 5
    public function __construct(string $defaultDomain)
17
    {
18 5
        $this->defaultDomain = $defaultDomain;
19
    }
20
21 5
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
22
    {
23 5
        $request = $request->withQueryParams($this->sanitizeDomainFromPayload($request->getQueryParams()))
24 5
                           ->withParsedBody($this->sanitizeDomainFromPayload($request->getParsedBody()));
0 ignored issues
show
Bug introduced by
It seems like $request->getParsedBody() can also be of type null and object; however, parameter $payload of Shlinkio\Shlink\Rest\Mid...tizeDomainFromPayload() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

24
                           ->withParsedBody($this->sanitizeDomainFromPayload(/** @scrutinizer ignore-type */ $request->getParsedBody()));
Loading history...
25
26 5
        return $handler->handle($request);
27
    }
28
29 5
    private function sanitizeDomainFromPayload(array $payload): array
30
    {
31 5
        if (isset($payload['domain']) && $payload['domain'] === $this->defaultDomain) {
32 2
            unset($payload['domain']);
33
        }
34
35 5
        return $payload;
36
    }
37
}
38