HSTSMiddleware::process()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 10
ccs 0
cts 6
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Koded\Framework\Middleware;
4
5
use Koded\Http\Interfaces\HttpStatus;
6
use Koded\Http\ServerResponse;
7
use Koded\Stdlib\Configuration;
8
use Psr\Http\Message\{ResponseInterface, ServerRequestInterface};
9
use Psr\Http\Server\{MiddlewareInterface, RequestHandlerInterface};
10
11
class HSTSMiddleware implements MiddlewareInterface
12
{
13
    private int $maxAge = 0;
14
    private string $includeSubDomains = '';
15
16
    public function __construct(Configuration $settings)
17
    {
18
        $this->maxAge = (int)$settings->get('hsts.maxAge', $this->maxAge);
19
        if ($settings->get('hsts.includeSubdomains', $this->includeSubDomains)) {
20
            $this->includeSubDomains = ';includeSubDomains';
21
        }
22
    }
23
24
    public function process(
25
        ServerRequestInterface $request,
26
        RequestHandlerInterface $handler): ResponseInterface
27
    {
28
        if ('https' !== $request->getUri()->getScheme()) {
29
            return (new ServerResponse(null, HttpStatus::MOVED_PERMANENTLY))
30
                ->withHeader('Location', (string)$request->getUri()->withScheme('https'));
31
        }
32
        return $handler->handle($request)->withHeader(
0 ignored issues
show
Bug Best Practice introduced by
The expression return $handler->handle(...his->includeSubDomains) returns the type Psr\Http\Message\MessageInterface which includes types incompatible with the type-hinted return Psr\Http\Message\ResponseInterface.
Loading history...
33
            'Strict-Transport-Security', 'max-age=' . $this->maxAge . $this->includeSubDomains
34
        );
35
    }
36
}
37