Test Setup Failed
Push — master ( fc6567...89d4a6 )
by Gabriel
07:58
created

RequestContext::getHttpHost()   A

Complexity

Conditions 5
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
nc 2
nop 0
dl 0
loc 11
ccs 0
cts 6
cp 0
crap 30
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
namespace Nip\Router;
4
5
use Nip\Router\RequestContext\Traits\HasHeadersTrait;
6
use Symfony\Component\HttpFoundation\Request;
7
8
/**
9
 * Class RequestContext
10
 * @package Nip\Router
11
 */
12
class RequestContext extends \Symfony\Component\Routing\RequestContext
13
{
14
    use HasHeadersTrait;
15
16
    /**
17
     * @inheritdoc
18
     */
19
    public function fromRequest(Request $request)
20
    {
21
        $this->setHeaders($request->headers);
22
23
        return parent::fromRequest($request);
24
    }
25
26
27
    /**
28
     * Get the root URL for the application.
29
     *
30
     * @return string
31
     */
32
    public function root()
33
    {
34
        return rtrim($this->getSchemeAndHttpHost() . $this->getBaseUrl(), '/');
35
    }
36
37
38
    /**
39
     * Get the full URL for the request.
40
     *
41
     * @return string
42
     */
43
    public function fullUrl()
44
    {
45
        $query = $this->getQueryString();
46
        $question = $this->getBaseUrl() . $this->getPathInfo() == '/' ? '/?' : '?';
47
        return $query ? $this->url() . $question . $query : $this->url();
48
    }
49
50
    /**
51
     * Get the URL (no query string) for the request.
52
     *
53
     * @return string
54
     */
55
    public function url()
56
    {
57
        return rtrim(preg_replace('/\?.*/', '', $this->getUri()), '/');
58
    }
59
60
    /**
61
     * Generates a normalized URI (URL) for the Request.
62
     *
63
     * @return string A normalized URI (URL) for the Request
64
     *
65
     * @see getQueryString()
66
     */
67
    public function getUri()
68
    {
69
        if (null !== $qs = $this->getQueryString()) {
70
            $qs = '?' . $qs;
71
        }
72
73
        return $this->getSchemeAndHttpHost() . $this->getBaseUrl() . $this->getPathInfo() . $qs;
74
    }
75
76
    /**
77
     * @return string
78
     */
79
    public function getSchemeAndHttpHost()
80
    {
81
        return $this->getScheme() . '://' . $this->getHttpHost();
82
    }
83
84
    /**
85
     * @return string
86
     */
87
    public function getHttpHost()
88
    {
89
        $scheme = $this->getScheme();
90
        $port = $this->getHttpsPort();
91
92
        if (('http' == $scheme && 80 == $port) || ('https' == $scheme && 443 == $port)) {
93
            return $this->getHost();
94
        }
95
96
        return $this->getHost() . ':' . $port;
97
    }
98
}