RequestUri   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
c 1
b 0
f 0
dl 0
loc 76
rs 10
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
B createUri() 0 32 7
A create() 0 4 1
A __toString() 0 4 2
A __construct() 0 3 1
1
<?php
2
/**
3
 * Webino™ (http://webino.sk)
4
 *
5
 * @link        https://github.com/webino/request
6
 * @copyright   Copyright (c) 2019 Webino, s.r.o. (http://webino.sk)
7
 * @author      Peter Bačinský <[email protected]>
8
 * @license     BSD-3-Clause
9
 */
10
11
namespace Webino;
12
13
/**
14
 * Class RequestUri
15
 * @package request
16
 */
17
class RequestUri implements RequestUriInterface
18
{
19
    /**
20
     * HTTP request URI
21
     *
22
     * @var string
23
     */
24
    private $uri;
25
26
    /**
27
     * @var HttpRequestInterface
28
     */
29
    private $request;
30
31
    /**
32
     * @param CreateInstanceEventInterface $event
33
     * @return RequestUri
34
     */
35
    public static function create(CreateInstanceEventInterface $event): RequestUri
36
    {
37
        $params = $event->getParameters();
38
        return new static(...$params);
0 ignored issues
show
Bug introduced by
$params is expanded, but the parameter $request of Webino\RequestUri::__construct() does not expect variable arguments. ( Ignorable by Annotation )

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

38
        return new static(/** @scrutinizer ignore-type */ ...$params);
Loading history...
39
    }
40
41
    /**
42
     * @param HttpRequestInterface $request
43
     */
44
    public function __construct(HttpRequestInterface $request)
45
    {
46
        $this->request = $request;
47
    }
48
49
    /**
50
     * @return string
51
     */
52
    public function __toString(): string
53
    {
54
        $this->uri or $this->uri = $this->createUri();
55
        return $this->uri;
56
    }
57
58
    /**
59
     * @return string
60
     */
61
    protected function createUri(): string
62
    {
63
        $request = $this->request;
64
65
        // IIS7 with URL Rewrite: make sure we get the unencoded url
66
        // (double slash problem).
67
        $iisUrlRewritten = $request['IIS_WasUrlRewritten'] ?? '';
68
        $unencodedUrl = (string) $request['UNENCODED_URL'] ?? '';
69
70
        if ('1' == $iisUrlRewritten && '' !== $unencodedUrl) {
71
            return $unencodedUrl;
72
        }
73
74
        $requestUri = $request['REQUEST_URI'] ?? '';
75
76
        // HTTP proxy requests setup request URI with scheme and host [and port]
77
        // + the URL path, only use URL path.
78
        if ($requestUri) {
79
            $uri = preg_replace('#^[^/:]+://[^/]+#', '', $requestUri);
80
            return is_string($uri) ? $uri : '';
81
        }
82
83
        // IIS 5.0, PHP as CGI.
84
        $origPathInfo = (string) $request['ORIG_PATH_INFO'] ?? '';
85
        if ($origPathInfo) {
86
            $queryString = $request[$request::QUERY_STRING] ?? '';
87
            $queryString or $origPathInfo .= '?' . $queryString;
88
            return $origPathInfo;
89
        }
90
91
        // last resort
92
        return '/';
93
    }
94
}
95