IpValidator::setKeys()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
nc 1
nop 1
dl 0
loc 3
c 1
b 0
f 0
cc 1
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
/**
4
 * Class IpValidator
5
 * Validate ip and return result.
6
*/
7
8
namespace Anax\Models;
9
10
use Anax\Config\access;
0 ignored issues
show
Bug introduced by
The type Anax\Config\access 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...
11
12
class IpValidator
13
{
14
    private $keys;
15
16
    public function setKeys($config)
17
    {
18
        $this->keys = $config;
19
    }
20
21
    /**
22
     * Curl from url
23
     */
24
    public function curl($url)
25
    {
26
          $curl = curl_init($url);
27
          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

27
          curl_setopt(/** @scrutinizer ignore-type */ $curl, CURLOPT_RETURNTRANSFER, true);
Loading history...
28
          $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

28
          $data = curl_exec(/** @scrutinizer ignore-type */ $curl);
Loading history...
29
          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

29
          curl_close(/** @scrutinizer ignore-type */ $curl);
Loading history...
30
31
          return $data;
32
    }
33
34
    /**
35
     * Validate IP-adress
36
     */
37
    public function validateIp($ipAdress)
38
    {
39
        $accessToken = $this->keys["ipValidator"];
40
        $stackUrl = 'http://api.ipstack.com/' . $ipAdress . '?access_key=' . $accessToken . '';
41
42
        $host = "";
43
        $stack = "";
44
        $type = "";
45
        $valid = "";
46
47
        if (filter_var($ipAdress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
48
            $host = gethostbyaddr($ipAdress);
49
            $stack = json_decode($this->curl($stackUrl));
50
            $type = "IPv4";
51
            $valid = true;
52
        } elseif (filter_var($ipAdress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
53
            $host = gethostbyaddr($ipAdress);
54
            $stack = json_decode($this->curl($stackUrl));
55
            $type = "IPv6";
56
            $valid = true;
57
        } else {
58
            $valid = false;
59
        }
60
61
        $data = [
62
            "ip" => $ipAdress,
63
            "valid" => $valid,
64
            "type" => $type,
65
            "host" => $host,
66
            "stack" => $stack
67
        ];
68
        return $data;
69
    }
70
71
    /**
72
     * Get current IP-adress
73
     */
74
    public function getYourIp($request)
75
    {
76
        if (!empty($request->getServer('HTTP_CLIENT_IP'))) {
77
            $ipAdress = $server('HTTP_CLIENT_IP');
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $server seems to be never defined.
Loading history...
78
        } elseif (!empty($request->getServer('HTTP_X_FORWARDED_FOR'))) {
79
            $ipAdress = $request->getServer('HTTP_X_FORWARDED_FOR');
80
        } else {
81
            $ipAdress = $request->getServer('REMOTE_ADDR');
82
        }
83
84
        return $ipAdress;
85
    }
86
}
87