Passed
Push — 2.1 ( cc4763...230ed5 )
by Greg
07:17
created

NetworkService   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 38
rs 10
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A findIpRangesForAsn() 0 29 5
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2025 webtrees development team
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Services;
21
22
use Throwable;
23
24
use function fclose;
25
use function fsockopen;
26
use function fwrite;
27
use function is_resource;
28
use function preg_match_all;
29
use function sprintf;
30
use function stream_get_contents;
31
use function stream_set_timeout;
32
33
class NetworkService
34
{
35
    private const WHOIS_HOSTS           = ['whois.radb.net', 'whois.ripe.net'];
36
    private const WHOIS_QUERY_FORMAT    = "-i origin %s\r\n";
37
    private const WHOIS_TIMEOUT_SECONDS = 5;
38
39
    /**
40
     * @return list<string>
0 ignored issues
show
Bug introduced by
The type Fisharebest\Webtrees\Services\list 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...
41
     */
42
    public function findIpRangesForAsn(string $asn): array
43
    {
44
        $query = sprintf(self::WHOIS_QUERY_FORMAT, $asn);
45
46
        foreach (self::WHOIS_HOSTS as $host) {
47
            try {
48
                $stream = fsockopen($host, 43, $error_code, $error_message, self::WHOIS_TIMEOUT_SECONDS);
49
50
                stream_set_timeout($stream, self::WHOIS_TIMEOUT_SECONDS);
51
52
                fwrite($stream, $query);
53
54
                $text = stream_get_contents($stream);
55
            } catch (Throwable $ex) {
56
                continue;
57
            } finally {
58
                if (is_resource($stream)) {
59
                    fclose($stream);
60
                }
61
            }
62
63
            preg_match_all('/\nroute6?:[ \t]*([0-9a-f.:]+\/[0-9]+)/i', $text, $matches);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $text seems to be defined later in this foreach loop on line 54. Are you sure it is defined here?
Loading history...
64
65
            if ($matches[1] !== []) {
66
                return $matches[1];
67
            }
68
        }
69
70
        return [];
71
    }
72
}
73