IPStackAPI   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 55
c 0
b 0
f 0
ccs 17
cts 17
cp 1
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A request() 0 19 1
A isValid() 0 6 3
A __construct() 0 3 1
1
<?php
2
3
namespace daib17\Model;
4
5
use Anax\Commons\ContainerInjectableInterface;
6
use Anax\Commons\ContainerInjectableTrait;
7
8
class IPStackAPI implements ContainerInjectableInterface
9
{
10
    use ContainerInjectableTrait;
11
12
    /**
13
    * @var string  $ip IP address to locate.
14
    */
15
    private $ipadd;
16
17
18
    /**
19
    * Constructor
20
    */
21 6
    public function __construct($ipadd)
22
    {
23 6
        $this->ipadd = $ipadd;
24 6
    }
25
26
27
    /**
28
    * Make API request with instance variable 'ipadd' and access key.
29
    */
30 3
    public function request()
31
    {
32
        // Get API key from configuration file
33 3
        $cfg = $this->di->get("configuration");
34 3
        $config = $cfg->load("apikeys.php");
35 3
        $apikey = $config["config"]["ipstack"];
36
37
        // Initialize curl
38 3
        $curl = curl_init('http://api.ipstack.com/' . $this->ipadd . '?access_key='. $apikey . '');
39 3
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
40
41
        // Store the data
42 3
        $json = curl_exec($curl);
43 3
        curl_close($curl);
44
45
        // Decode JSON response
46 3
        $res = json_decode($json, true);
47
48 3
        return $res;
49
    }
50
51
52
    /**
53
    * Validate IP
54
    *
55
    * @return boolean true if valid IP, false otherwise
56
    */
57 6
    public function isValid($ipadd)
58
    {
59
        // Validate IP
60 6
        $isIPv4 = filter_var($ipadd, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
61 6
        $isIPv6 = filter_var($ipadd, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);
62 6
        return ($isIPv4 || $isIPv6) ? true : false;
63
    }
64
}
65