Passed
Push — master ( a3d508...a65292 )
by Alfred
03:35 queued 01:43
created

GeoIP2Country   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 13
c 1
b 0
f 0
dl 0
loc 47
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A isReservedAddress() 0 5 3
A resolve() 0 6 2
A __construct() 0 5 1
1
<?php
2
3
namespace iriven;
4
5
use \iriven\bin\GeoipDatabase;
0 ignored issues
show
Bug introduced by
The type \iriven\bin\GeoipDatabase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use \iriven\bin\GeoipNetwork;
0 ignored issues
show
Bug introduced by
The type \iriven\bin\GeoipNetwork was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
class GeoIP2Country
8
{
9
    /**
10
     * PDO SQLite3 database instance
11
     *
12
     * @var GeoipDatabase
13
    **/
14
    private $oDBInstance=null;
15
    /**
16
     * Network tools class instance
17
     *
18
     * @var GeoipNetwork
19
    **/
20
    private $oNetwork=null;
21
    /**
22
     * Class Constructor
23
     *
24
     * @param string $database
25
     */
26
    public function __construct(string $database = null)
27
    {
28
        $this->oDBInstance = new GeoipDatabase($database);
29
        $this->oNetwork = new GeoipNetwork();
30
        return $this;
31
    }
32
    /**
33
     * Retrieve country code from given IP address
34
     *
35
     * @param string|null $ipAddress
36
     * @return string
37
     */
38
    public function resolve(string $ipAddress= null): string
39
    {
40
        $ipAddress || $ipAddress = $this->oNetwork->getIPAddress();
41
        $ipVersion = $this->oNetwork->ipVersion($ipAddress);
42
        $start = $this->oNetwork->ip2Integer($ipAddress);
43
        return $this->oDBInstance->fetch($start, $ipVersion);
44
    }
45
    /**
46
     * @param mixed|null $ipAddress
47
     * @return bool
48
     */
49
    public function isReservedAddress($ipAddress=null): bool
50
    {
51
        $ipAddress || $ipAddress = $this->oNetwork->getIPAddress();
52
        $countryCode = $this->resolve($ipAddress);
53
        return !$countryCode || strcasecmp($countryCode, 'ZZ') == 0 ;
54
    }
55
}
56