Total Complexity | 9 |
Total Lines | 31 |
Duplicated Lines | 0 % |
Coverage | 0% |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
8 | class CurrentIpResolver implements CurrentIpResolverInterface |
||
9 | { |
||
10 | public function getCurrentIp(): IpAddress |
||
11 | { |
||
12 | return $this->getIpByServerGlobalArray(); |
||
13 | } |
||
14 | |||
15 | private function getIpByServerGlobalArray(): IpAddress |
||
16 | { |
||
17 | $ipAddressString = ''; |
||
18 | |||
19 | if (!empty($_SERVER['REMOTE_ADDR']) |
||
20 | && $this->isIpValid($remoteAddrIpAddressString = trim($_SERVER['REMOTE_ADDR'])) |
||
21 | ) { |
||
22 | $ipAddressString = $remoteAddrIpAddressString; |
||
23 | } elseif (!empty($_SERVER['HTTP_CLIENT_IP']) |
||
24 | && $this->isIpValid($httpClientIpAddressString = trim($_SERVER['HTTP_CLIENT_IP'])) |
||
25 | ) { |
||
26 | $ipAddressString = $httpClientIpAddressString; |
||
27 | } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']) |
||
28 | && $this->isIpValid($httpForwardedIpAddressString = trim($_SERVER['HTTP_X_FORWARDED_FOR'])) |
||
29 | ) { |
||
30 | $ipAddressString = $httpForwardedIpAddressString; |
||
31 | } |
||
32 | |||
33 | return new IpAddress($ipAddressString); |
||
34 | } |
||
35 | |||
36 | private function isIpValid(string $ipAddress = ''): bool |
||
39 | } |
||
40 | } |
||
41 |