ASNCollector::collect()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
cc 3
eloc 14
c 4
b 1
f 0
nc 3
nop 0
dl 0
loc 23
rs 9.7998
1
<?php
2
3
namespace Startwind\Inventorio\Collector\Hosting\HostingCompany;
4
5
use Startwind\Inventorio\Collector\Collector;
6
7
class ASNCollector implements Collector
8
{
9
    public function getIdentifier(): string
10
    {
11
        return 'AutonomousSystem';
12
    }
13
14
    public function collect(): array
15
    {
16
        // @todo use already collected ip address
17
        $ip = file_get_contents('https://api.ipify.org');
18
        $data = json_decode(file_get_contents("http://ip-api.com/json/" . $ip), true);
19
20
        $asnArray = explode(' ', $data['as']);
21
22
        $asn = substr($asnArray[0], 2);
23
        $as = $data['as'];
24
25
        $short = $as;
26
27
        if (str_contains(strtolower($short), 'hetzner')) {
28
            $short = 'hetzner';
29
        } else if (str_contains(strtolower($short), 'velia')) {
30
            $short = 'velia';
31
        }
32
33
        return [
34
            'as' => $as,
35
            'asn' => $asn,
36
            'short' => $short
37
        ];
38
    }
39
}
40