Passed
Push — master ( 73d0c8...623cb3 )
by Daniel
02:42
created

AbstractClient::makeWhoisRawRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 2
dl 0
loc 15
ccs 7
cts 7
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace MallardDuck\Whois;
3
4
use TrueBV\Punycode;
5
use League\Uri\Components\Host;
6
use Hoa\Socket\Client as SocketClient;
7
use MallardDuck\Whois\WhoisServerList\Locator;
8
use MallardDuck\Whois\Exceptions\MissingArgException;
9
10
/**
11
 * The Whois Client Class.
12
 *
13
 * @author mallardduck <[email protected]>
14
 * @copyright lucidinternets.com 2018
15
 * @version 1.0.0
16
 */
17
class AbstractClient
18
{
19
20
    /**
21
     * The TLD Whois locator class.
22
     * @var Locator
23
     */
24
    protected $tldLocator;
25
26
    /**
27
     * The Unicode for IDNA.
28
     * @var \TrueBV\Punycode
29
     */
30
    protected $punycode;
31
32
    /**
33
     * The carriage return line feed character comobo.
34
     * @var string
35
     */
36
    protected $clrf = "\r\n";
37
38
    /**
39
     * The input domain provided by the user.
40
     * @var string
41
     */
42
    public $inputDomain;
43
44
    /**
45
     * The parsed domain after validating and encoding.
46
     * @var string
47
     */
48
    public $parsedDomain;
49
50
    /**
51
     * Construct the Whois Client Class.
52
     */
53 90
    public function __construct()
54
    {
55 90
        $this->punycode = new Punycode();
56 90
        $this->tldLocator = new Locator;
57 90
    }
58
59
    /**
60
     * Takes the user provided domain and parses then encodes just the registerable domain.
61
     * @param  string $domain The user provided domain.
62
     * @return string         Just the registrable part of a domain encoded for IDNA.
63
     */
64 75
    protected function parseWhoisDomain($domain)
65
    {
66 75
        if (empty($domain)) {
67 3
            throw new MissingArgException("Must provide a domain name when using lookup method.");
68
        }
69 72
        $this->inputDomain = $domain;
70
71
        // Check domain encoding
72 72
        $encoding = mb_detect_encoding($domain);
73
74
        // Attempt to parse the domains Host component and get the registrable parts.
75 72
        $host = new Host($domain);
76
        // Get the method by which is supported to maintain PHP 7 and 5.6 compatibility.
77 66
        $method = (method_exists($host, 'getRegistrableDomain')) ? 'getRegistrableDomain' : 'getRegisterableDomain';
78 66
        $processedDomain = $host->$method();
79
        // Check how the host component was parsed
80 66
        if (strlen($processedDomain) === 0 && strlen($domain) >= 0) {
81 3
            $processedDomain = $domain;
82 1
        }
83
84
        // Punycode the domain if it's Unicode
85 66
        if ("UTF-8" === $encoding) {
86 15
            $processedDomain = $this->punycode->encode($processedDomain);
87 5
        }
88 66
        $this->parsedDomain = $processedDomain;
89 66
        return $this;
90
    }
91
92
    /**
93
     * A unicode safe method for making whois requests.
94
     *
95
     * The main difference with this method is the benefit of
96
     *
97
     * @param  string $domain      The domain or IP being looked up.
98
     * @param  string $whoisServer The whois server being queried.
99
     * @return string              The raw results of the query response.
100
     */
101 33
    public function makeWhoisRequest($domain, $whoisServer)
102
    {
103 33
        $this->parseWhoisDomain($domain);
104
        // Form a socket connection to the whois server.
105 33
        return $this->makeWhoisRawRequest($this->parsedDomain, $whoisServer);
106
    }
107
108
    /**
109
     * A function for making a raw Whois request.
110
     * @param  string $domain      The domain or IP being looked up.
111
     * @param  string $whoisServer The whois server being queried.
112
     * @return string              The raw results of the query response.
113
     */
114 33
    public function makeWhoisRawRequest($domain, $whoisServer)
115
    {
116
        // Form a tcp socket connection to the whois server.
117 33
        $client = new SocketClient('tcp://' . $whoisServer . ':43', 10);
118 33
        $client->connect();
119
        // Send the domain name requested for whois lookup.
120 33
        $client->writeString($domain . $this->clrf);
121
        // Read the full output of the whois lookup.
122 33
        $response = $client->readAll();
123
        // Disconnect the connections to prevent network/performance issues.
124
        // Yes, it's necessary. Without disconnecting I discovered errores when
125
        // I began adding tests to the library.
126 33
        $client->disconnect();
127
128 33
        return $response;
129
    }
130
}
131