Total Complexity | 6 |
Total Lines | 49 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
15 | class Data |
||
16 | { |
||
17 | private $_ip; |
||
18 | private $_url; |
||
19 | |||
20 | 11 | public function __construct(string $url) |
|
21 | { |
||
22 | 11 | $this->_url = $url; |
|
23 | 11 | $this->_ip = $this->getIp($this->_url); |
|
24 | 11 | } |
|
25 | |||
26 | /** |
||
27 | * Get Shodan API URL with key. |
||
28 | * |
||
29 | * @return string |
||
30 | */ |
||
31 | 4 | public function shodanApiUrl() |
|
35 | } |
||
36 | |||
37 | /** |
||
38 | * Get IPv4 address from a URL. |
||
39 | * |
||
40 | * @param string $url |
||
41 | * |
||
42 | * @return string|null |
||
43 | */ |
||
44 | 11 | public function getIp(string $url) |
|
45 | { |
||
46 | 11 | $domain = parse_url($url); |
|
47 | 11 | if (empty($domain['host'])) { |
|
48 | 1 | return; |
|
49 | } |
||
50 | |||
51 | 11 | $ip = gethostbyname($domain['host']); |
|
52 | |||
53 | 11 | return filter_var($ip, FILTER_VALIDATE_IP) ? $ip : null; |
|
54 | } |
||
55 | |||
56 | /** |
||
57 | * Returns URL set when initialising Data object. |
||
58 | * |
||
59 | * @return string |
||
60 | */ |
||
61 | 3 | public function getUrl() |
|
64 | } |
||
65 | } |
||
66 |