Passed
Push — master ( 9bad7b...2a1621 )
by Lawrence
01:41
created

Whois   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 14
dl 0
loc 97
ccs 34
cts 34
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A filterServers() 0 10 2
A __construct() 0 3 1
A allServers() 0 3 1
A loadServers() 0 9 3
A servers() 0 13 4
A check() 0 18 3
1
<?php
2
declare(strict_types=1);
3
4
/*
5
 +-----------------------------------------------------------------------------+
6
 | PHPPackage - Domain Checker
7
 +-----------------------------------------------------------------------------+
8
 | Copyright (c)2018 (http://github.com/phppackage/domaincheck)
9
 +-----------------------------------------------------------------------------+
10
 | This source file is subject to MIT License
11
 | that is bundled with this package in the file LICENSE.
12
 |
13
 | If you did not receive a copy of the license and are unable to
14
 | obtain it through the world-wide-web, please send an email
15
 | to [email protected] so we can send you a copy immediately.
16
 +-----------------------------------------------------------------------------+
17
 | Authors:
18
 |   Lawrence Cherone <[email protected]>
19
 +-----------------------------------------------------------------------------+
20
 */
21
22
namespace PHPPackage\DomainCheck;
23
24
class Whois
25
{
26
    /**
27
     * @var
28
     */
29
    private $tlds = [];
30
31
    /**
32
     *
33
     */
34 3
    public function __construct(array $tlds = [])
35
    {
36 3
        $this->tlds = $tlds;
37 3
    }
38
39
    /**
40
     *
41
     */
42 4
    private function filterServers(array $servers, array $tlds = []): array
43
    {
44 4
        $tlds = array_merge($this->tlds, $tlds);
45
46 4
        if (empty($tlds)) {
47 1
            return $servers;
48
        }
49
50 3
        return array_values(array_filter($servers, function ($value) use ($tlds) {
51 3
            return in_array($value['tld'], $tlds);
52 3
        }));
53
    }
54
    
55
    /**
56
     *
57
     */
58 6
    private function loadServers(string $servers_file = 'whois-servers.json')
59
    {
60 6
        $path = __DIR__.'/'.$servers_file;
61
62 6
        if (!file_exists($path) || !is_readable($path)) {
63 1
            throw new \RuntimeException('whois-servers.json does not exist or is not readable');
64
        }
65
        
66 5
        return json_decode(file_get_contents($path), true);
67
    }
68
69
    /**
70
     *
71
     */
72 9
    public function servers(array $tlds = [], bool $all = false, string $servers_file = 'whois-servers.json'): array
73
    {
74 9
        $json = $this->loadServers($servers_file);
75
76 8
        if (empty($json) || !is_array($json)) {
77 2
            throw new \RuntimeException('invalid whois-servers.json file');
78
        }
79
80 6
        if (!$all) {
81 4
            return $this->filterServers($json, $tlds);
82
        }
83
        
84 2
        return $json;
85
    }
86
    
87
    /**
88
     *
89
     */
90 1
    public function allServers(string $servers_file = 'whois-servers.json'): array
91
    {
92 1
        return $this->servers([], true, $servers_file);
93
    }
94
95
    /**
96
     * Socket connection to whois server.
97
     *
98
     * @param string $domain
99
     * @param string $server
100
     * @param string $findText
101
     * @return bool
102
     */
103 5
    public function check($domain, $server, $pattern): bool
104
    {
105 5
        $socket = fsockopen($server, 43);
106
107 5
        if ($socket === false) {
108 1
            return false;
109
        }
110
111 4
        fputs($socket, $domain."\r\n");
112
113 4
        $response = null;
114 4
        while (!feof($socket)) {
115 4
            $response .= fgets($socket, 512);
116
        }
117
118 4
        fclose($socket);
119
120 4
        return (stripos($response, $pattern) !== false);
121
    }
122
}
123