Test Failed
Push — master ( c4920f...241378 )
by 世昌
02:23
created

SchemeAttribute::getPort()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 6
rs 10
1
<?php
2
namespace nebula\request\attribute;
3
4
/**
5
 * HTTP请求协议属性
6
 *
7
 */
8
trait SchemeAttribute
9
{
10
    protected $host;
11
    protected $port;
12
    protected $scheme;
13
14
    public function getScheme()
15
    {
16
        $server = $this->getServer();
17
        if (!isset($this->scheme)) {
18
            if (array_key_exists('HTTPS', $server) && strcasecmp($server['HTTPS'], 'off') != 0) {
19
                $scheme = 'https';
20
            } elseif (array_key_exists('REQUEST_SCHEME', $server)) {
21
                $scheme = $server['REQUEST_SCHEME'];
22
            } else {
23
                $scheme = 'http';
24
            }
25
            return  $this->scheme = $scheme;
26
        }
27
    }
28
   
29
    public function getHost()
30
    {
31
        if (isset($this->host)) {
32
            return $this->host;
33
        }
34
        return $this->host = $this->getServer()['HTTP_HOST'] ?? 'localhost';
35
    }
36
37
    public function getPort()
38
    {
39
        if (isset($this->port)) {
40
            return $this->port;
41
        }
42
        return $this->port = $this->getServer()['SERVER_PORT'] ?? 80;
43
    }
44
45
    /**
46
     * 获取服务器基础Uri
47
     *
48
     * @return string
49
     */
50
    public function getServerUri()
51
    {
52
        return $this->getScheme().'://'.$this->getHost();
53
    }
54
55
    /**
56
      * Get 服务器属性
57
      *
58
      * @return  array
59
      */
60
    abstract public function getServer();
61
}
62