RequestUriFactory   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 18
c 1
b 0
f 0
dl 0
loc 69
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 4 1
A getServerParam() 0 7 2
A createUriFrom() 0 4 1
A generateUrl() 0 12 3
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of slick/http
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Slick\Http\Message\Server;
13
14
use Psr\Http\Message\ServerRequestInterface;
15
use Psr\Http\Message\UriInterface;
16
use Slick\Http\Message\Uri;
17
18
/**
19
 * RequestUriFactory
20
 *
21
 * @package Slick\Http\Message\Server
22
*/
23
class RequestUriFactory
24
{
25
    /**
26
     * @var ServerRequestInterface
27
     */
28
    private ServerRequestInterface $request;
29
30
    /**
31
     * Creates an URI based on provided request data
32
     *
33
     * @param ServerRequestInterface $request
34
     *
35
     * @return UriInterface|Uri
36
     */
37
    public function createUriFrom(ServerRequestInterface $request): UriInterface|Uri
38
    {
39
        $this->request = $request;
40
        return new Uri($this->generateUrl());
41
    }
42
43
    /**
44
     * Creates an URI based on provided request data
45
     *
46
     * @param ServerRequestInterface $request
47
     *
48
     * @return UriInterface|Uri
49
     */
50
    public static function create(ServerRequestInterface $request): UriInterface|Uri
51
    {
52
        $factory = new RequestUriFactory();
53
        return $factory->createUriFrom($request);
54
    }
55
56
    /**
57
     * Generates an URL from current request data
58
     *
59
     * @return string
60
     */
61
    private function generateUrl(): string
62
    {
63
        $hostHeader = $this->request->getHeaderLine('host');
64
        $defaultHost = \strlen($hostHeader) > 0 ? $hostHeader : 'unknown-host';
65
        $host = $this->getServerParam('SERVER_NAME', $defaultHost);
66
        $scheme = $this->getServerParam('REQUEST_SCHEME', 'http');
67
        $uri = $this->getServerParam('REQUEST_URI', '/');
68
        $port = $this->getServerParam('SERVER_PORT', '80');
69
70
        $port = \in_array($port, ['80', '443']) ? '' : ":$port";
71
72
        return "$scheme://$host$port$uri";
73
    }
74
75
    /**
76
     * Retrieve an request server parameter stored with provided name
77
     *
78
     * If no match is found the default value is returned instead
79
     *
80
     * @param string $name    $_SERVER super-global key name
81
     * @param mixed   $default
82
     *
83
     * @return null|string
84
     */
85
    private function getServerParam(string $name, mixed $default = null): ?string
86
    {
87
        $data = $this->request->getServerParams();
88
        if (\array_key_exists($name, $data)) {
89
            $default = trim($data[$name]);
90
        }
91
        return $default;
92
    }
93
}
94