Passed
Push — master ( 4457ff...29b457 )
by Daniel
02:12
created

AbstractClient::parseWhoisDomain()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 11
nc 5
nop 1
dl 0
loc 22
ccs 14
cts 14
cp 1
crap 5
rs 8.6737
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
     * [Short description of the method]
61
     *
62
     * @param string $domain          [Description]
63
     *
64
     * @return string
65
     */
66 72
    protected function getSearchableHostname($domain)
67
    {
68
        // Attempt to parse the domains Host component and get the registrable parts.
69 72
        $host = new Host($domain);
70
        // Get the method by which is supported to maintain PHP 7 and 5.6 compatibility.
71 66
        $method = (method_exists($host, 'getRegistrableDomain')) ? 'getRegistrableDomain' : 'getRegisterableDomain';
72
73 66
        return $host->$method();
74
    }
75
76
    /**
77
     * Takes the user provided domain and parses then encodes just the registerable domain.
78
     * @param  string $domain The user provided domain.
79
     * @return string         Just the registrable part of a domain encoded for IDNA.
80
     */
81 75
    protected function parseWhoisDomain($domain)
82
    {
83 75
        if (empty($domain)) {
84 3
            throw new MissingArgException("Must provide a domain name when using lookup method.");
85
        }
86 72
        $this->inputDomain = $domain;
87
88
        // Check domain encoding
89 72
        $encoding = mb_detect_encoding($domain);
90
91 72
        $processedDomain = $this->getSearchableHostname($domain);
92
        // Check how the host component was parsed
93 66
        if (strlen($processedDomain) === 0 && strlen($domain) >= 0) {
94 3
            $processedDomain = $domain;
95 1
        }
96
97
        // Punycode the domain if it's Unicode
98 66
        if ("UTF-8" === $encoding) {
99 15
            $processedDomain = $this->punycode->encode($processedDomain);
100 5
        }
101 66
        $this->parsedDomain = $processedDomain;
102 66
        return $this;
103
    }
104
105
    /**
106
     * A unicode safe method for making whois requests.
107
     *
108
     * The main difference with this method is the benefit of
109
     *
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 makeWhoisRequest($domain, $whoisServer)
115
    {
116 33
        $this->parseWhoisDomain($domain);
117
        // Form a socket connection to the whois server.
118 33
        return $this->makeWhoisRawRequest($this->parsedDomain, $whoisServer);
119
    }
120
121
    /**
122
     * A function for making a raw Whois request.
123
     * @param  string $domain      The domain or IP being looked up.
124
     * @param  string $whoisServer The whois server being queried.
125
     * @return string              The raw results of the query response.
126
     */
127 33
    public function makeWhoisRawRequest($domain, $whoisServer)
128
    {
129
        // Form a tcp socket connection to the whois server.
130 33
        $client = new SocketClient('tcp://' . $whoisServer . ':43', 10);
131 33
        $client->connect();
132
        // Send the domain name requested for whois lookup.
133 33
        $client->writeString($domain . $this->clrf);
134
        // Read the full output of the whois lookup.
135 33
        $response = $client->readAll();
136
        // Disconnect the connections to prevent network/performance issues.
137
        // Yes, it's necessary. Without disconnecting I discovered errores when
138
        // I began adding tests to the library.
139 33
        $client->disconnect();
140
141 33
        return $response;
142
    }
143
}
144