IpValidator::curl()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
nc 1
nop 1
dl 0
loc 10
c 1
b 0
f 0
cc 1
ccs 0
cts 7
cp 0
crap 2
rs 10
1
<?php
2
3
namespace Teca\IpValidator;
4
5
use Anax\Commons\ContainerInjectableInterface;
6
use Anax\Commons\ContainerInjectableTrait;
7
8
class IpValidator implements ContainerInjectableInterface
9
{
10
    use ContainerInjectableTrait;
11
12
    private $url = 'http://api.ipstack.com/';
13
    private $apiKey = '';
14
15 19
    public function setUrl(string $url) : void
16
    {
17 19
        $this->url = $url;
18 19
    }
19
20 19
    public function setApiKey(string $apiKey) : void
21
    {
22 19
        $this->apiKey = $apiKey;
23 19
    }
24
25 10
    public function verifyIpv4(string $ipAddress) : bool
26
    {
27 10
        return filter_var($ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
28
    }
29
30 10
    public function verifyIpv6(string $ipAddress) : bool
31
    {
32 10
        return filter_var($ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);
33
    }
34
35 7
    public function getDomain(string $ipAddress) : string
36
    {
37 7
        if (!$this->verifyIpv4($ipAddress) && !$this->verifyIpv6($ipAddress)) {
38 2
            return "";
39
        }
40
41 6
        $host = gethostbyaddr($ipAddress);
42 6
        return $host !== $ipAddress ? $host : "";
43
    }
44
45 8
    protected function noGeo() : array
46
    {
47
        return [
48 8
            "latitude" => null,
49
            "longitude" => null,
50
            "country_name" => null,
51
            "region_name" => null,
52
            "city" => null
53
        ];
54
    }
55
56 8
    public function getGeo(string $ipAddress) : array
57
    {
58 8
        if (!$this->verifyIpv4($ipAddress) && !$this->verifyIpv6($ipAddress)) {
59 3
            return $this->noGeo();
60
        }
61
62 5
        $url = $this->url . $ipAddress;
63
64 5
        if ($this->apiKey !== '') {
65
            $url .= '?access_key=' . $this->apiKey;
66
        }
67
68 5
        $data = $this->curl($url);
69
70 5
        return $data;
71
    }
72
73
    protected function curl(string $url) : array
74
    {
75
        $curl = curl_init();
76
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
0 ignored issues
show
Bug introduced by
It seems like $curl can also be of type false; however, parameter $ch of curl_setopt() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

76
        curl_setopt(/** @scrutinizer ignore-type */ $curl, CURLOPT_RETURNTRANSFER, true);
Loading history...
77
        curl_setopt($curl, CURLOPT_URL, $url);
78
79
        $data = curl_exec($curl);
0 ignored issues
show
Bug introduced by
It seems like $curl can also be of type false; however, parameter $ch of curl_exec() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

79
        $data = curl_exec(/** @scrutinizer ignore-type */ $curl);
Loading history...
80
        curl_close($curl);
0 ignored issues
show
Bug introduced by
It seems like $curl can also be of type false; however, parameter $ch of curl_close() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

80
        curl_close(/** @scrutinizer ignore-type */ $curl);
Loading history...
81
82
        return json_decode($data, true);
83
    }
84
}
85