IpModel   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Test Coverage

Coverage 5.56%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 35
c 1
b 0
f 0
dl 0
loc 74
ccs 2
cts 36
cp 0.0556
rs 10
wmc 12

4 Methods

Rating   Name   Duplication   Size   Complexity  
A ipValidateJson() 0 15 5
A local() 0 3 1
A ipValidate() 0 15 5
A ipStack() 0 33 1
1
<?php
2
3
namespace Anax\Controller;
4
5
class IpModel
6
{
7
    public function ipValidate($ipAddress) : array
8
    {
9
        $version = null;
10
        if (filter_var($ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) || filter_var($ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
11
            $check = $ipAddress . " is OK";
12
            $version = strpos($ipAddress, ".") === false ? 6 : 4;
13
            $hostname = gethostbyaddr($ipAddress);
14
            if ($hostname == $ipAddress) {
15
                $hostname = "Not found";
16
            }
17
        } else {
18
            $check = $ipAddress . " is NOT OK ip address";
19
            $hostname = "Not found";
20
        }
21
        return [$check, $hostname, $version];
22
    }
23
24 2
    public function local() : string
25
    {
26 2
        return file_get_contents("http://ipecho.net/plain");
27
    }
28
29
    public function ipValidateJson($ipAddress) : array
30
    {
31
        $version = null;
32
        if (filter_var($ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) || filter_var($ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
33
            $check = $ipAddress . " is OK";
34
            $version = strpos($ipAddress, ".") === false ? "ipv6" : "ipv4";
35
            $hostname = gethostbyaddr($ipAddress);
36
            if ($hostname == $ipAddress) {
37
                $hostname = "Not found";
38
            }
39
        } else {
40
            $check = $ipAddress . " is NOT OK ip address";
41
            $hostname = "Not found";
42
        }
43
        return ["ip" => $check, "domain" => $hostname, "type" => $version];
44
    }
45
46
    public function ipStack($ipAddress)
47
    {
48
        // $path = ANAX_INSTALL_PATH . "/config/api.txt";
49
        $keys = require(ANAX_INSTALL_PATH . "/config/api.php");
50
        // $accessKey = file_get_contents($path[0]);
51
52
        $accessKey = $keys[0];
53
        // $handle = fopen(ANAX_INSTALL_PATH . "/config/api.txt", "r");
54
        // if ($handle) {
55
        //         $accessKey = fgets($handle);
56
        //         // echo $line;
57
        //     }
58
        //     fclose($handle);
59
        // Initialize CURL:
60
        $chr = curl_init('http://api.ipstack.com/'.$ipAddress.'?access_key='.trim($accessKey).'');
61
        curl_setopt($chr, CURLOPT_RETURNTRANSFER, true);
0 ignored issues
show
Bug introduced by
It seems like $chr 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

61
        curl_setopt(/** @scrutinizer ignore-type */ $chr, CURLOPT_RETURNTRANSFER, true);
Loading history...
62
63
        // Store the data:
64
        $json = curl_exec($chr);
0 ignored issues
show
Bug introduced by
It seems like $chr 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

64
        $json = curl_exec(/** @scrutinizer ignore-type */ $chr);
Loading history...
65
        curl_close($chr);
0 ignored issues
show
Bug introduced by
It seems like $chr 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

65
        curl_close(/** @scrutinizer ignore-type */ $chr);
Loading history...
66
        $controll = $this->ipValidateJson($ipAddress);
67
68
69
        // Decode JSON response:
70
        $apiResult = json_decode($json, true, JSON_UNESCAPED_UNICODE);
71
        $apiResult["ip check"] = $controll["ip"];
72
        $apiResult["domain"] = $controll["domain"];
73
        // if (trim($accessKey) === $k) {
74
        //     return "lika";
75
        // } else {
76
        //     return var_dump(trim());
77
        // }
78
        return $apiResult;
79
    }
80
}
81