RequestContext   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 3
eloc 13
c 2
b 0
f 0
dl 0
loc 26
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A fromPsrRequest() 0 15 3
1
<?php
2
3
/**
4
 * This file is part of web-stack
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Slick\WebStack\Infrastructure\Http\Routing;
13
14
use Psr\Http\Message\ServerRequestInterface;
15
use Symfony\Component\Routing\RequestContext as SymfonyRequestContext;
16
17
/**
18
 * RequestContext
19
 *
20
 * @package Slick\WebStack\Infrastructure\Http\Routing
21
 */
22
final class RequestContext extends SymfonyRequestContext
23
{
24
25
    /**
26
     * Create a RequestContext object from a PSR-7 ServerRequestInterface object.
27
     *
28
     * @param ServerRequestInterface $request The PSR-7 ServerRequestInterface object.
29
     * @param string $baseUrl The base URL for the request context (optional).
30
     *
31
     * @return RequestContext The created RequestContext object.
32
     */
33
    public function fromPsrRequest(ServerRequestInterface $request, string $baseUrl = ''): RequestContext
34
    {
35
        $requestUri = $request->getUri() ;
36
        $isSecure = strtoupper($requestUri->getScheme()) === 'HTTPS';
37
        $httpPort = (int) $requestUri->getPort();
38
39
        return new RequestContext(
40
            $baseUrl,
41
            $request->getMethod(),
42
            $requestUri->getHost(),
43
            $requestUri->getScheme(),
44
            $httpPort,
45
            $isSecure && $httpPort > 0 ? $httpPort : 443,
46
            $requestUri->getPath(),
47
            (string) $requestUri->getQuery()
48
        );
49
    }
50
}
51