CurrentIpResolver   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 31
ccs 0
cts 25
cp 0
rs 10
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A isIpValid() 0 3 1
A getCurrentIp() 0 3 1
B getIpByServerGlobalArray() 0 19 7
1
<?php
2
3
namespace CodeblogPro\GeoLocation\Application\Services;
4
5
use CodeblogPro\GeoLocation\Application\Interfaces\CurrentIpResolverInterface;
6
use CodeblogPro\GeoLocation\Application\Models\IpAddress;
7
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
37
    {
38
        return (bool)(filter_var($ipAddress, FILTER_VALIDATE_IP));
39
    }
40
}
41