Passed
Pull Request — master (#16)
by
unknown
03:22
created

Server::getProtocolPrefix()   A

Complexity

Conditions 6
Paths 10

Size

Total Lines 22
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 6.0061

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 18
c 1
b 0
f 0
nc 10
nop 0
dl 0
loc 22
ccs 17
cts 18
cp 0.9444
crap 6.0061
rs 9.0444
1
<?php
2
3
namespace Aoe\Asdis\Domain\Model;
4
5
/**
6
 * Represents a server.
7
 */
8
class Server
9
{
10
    /**
11
     * @var string
12
     */
13
    public const PROTOCOL_WILDCARD = 'wildcard';
14
15
    /**
16
     * @var string
17
     */
18
    public const PROTOCOL_MARKER = 'marker';
19
20
    /**
21
     * @var string
22
     */
23
    public const PROTOCOL_DYNAMIC = 'dynamic';
24
25
    /**
26
     * @var string
27
     */
28
    public const PROTOCOL_HTTP = 'http';
29
30
    /**
31
     * @var string
32
     */
33
    public const PROTOCOL_HTTPS = 'https';
34
35
    private ?string $domain = null;
36
37
    private ?string $protocol = null;
38
39
    private ?string $identifier = null;
40
41
    private ?string $protocolMarker = null;
42
43 6
    public function setDomain(string $domain): void
44
    {
45 6
        $this->domain = $domain;
46 6
    }
47
48 2
    public function getDomain(): ?string
49
    {
50 2
        return $this->domain;
51
    }
52
53 4
    public function setIdentifier(string $identifier): void
54
    {
55 4
        $this->identifier = $identifier;
56 4
    }
57
58 4
    public function getIdentifier(): ?string
59
    {
60 4
        return $this->identifier;
61
    }
62
63 6
    public function setProtocol(string $protocol): void
64
    {
65 6
        if (!in_array(
66 6
            $protocol,
67 6
            [self::PROTOCOL_WILDCARD,
68 6
                self::PROTOCOL_MARKER,
69 6
                self::PROTOCOL_HTTP,
70 6
                self::PROTOCOL_HTTPS,
71 6
                self::PROTOCOL_DYNAMIC,
72
            ]
73
        )) {
74 1
            return;
75
        }
76 6
        $this->protocol = $protocol;
77 6
    }
78
79 2
    public function getProtocol(): ?string
80
    {
81 2
        return $this->protocol;
82
    }
83
84 2
    public function setProtocolMarker(string $protocolMarker): void
85
    {
86 2
        $this->protocolMarker = $protocolMarker;
87 2
    }
88
89 4
    public function getUri(): string
90
    {
91 4
        return $this->getProtocolPrefix() . $this->domain . '/';
92
    }
93
94
    protected function getRequestProtocol(): string
95
    {
96
        if (strlen($_SERVER['HTTPS']) > 0) {
97
            return self::PROTOCOL_HTTPS;
98
        }
99
        if (strtolower($_SERVER['HTTPS']) !== 'off') {
100
            return self::PROTOCOL_HTTPS;
101
        }
102
        return self::PROTOCOL_HTTP;
103
    }
104
105 4
    private function getProtocolPrefix(): string
106
    {
107 4
        $protocolPrefix = '';
108 4
        $protocol = $this->protocol;
109 4
        if ($protocol === self::PROTOCOL_DYNAMIC) {
110
            $protocol = $this->getRequestProtocol();
111
        }
112
        switch ($protocol) {
113 4
            case self::PROTOCOL_MARKER:
114 1
                $protocolPrefix = $this->protocolMarker;
115 1
                break;
116 3
            case self::PROTOCOL_WILDCARD:
117 1
                $protocolPrefix = '//';
118 1
                break;
119 2
            case self::PROTOCOL_HTTP:
120 1
                $protocolPrefix = 'http://';
121 1
                break;
122 1
            case self::PROTOCOL_HTTPS:
123 1
                $protocolPrefix = 'https://';
124 1
                break;
125
        }
126 4
        return $protocolPrefix;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $protocolPrefix could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
127
    }
128
}
129