Passed
Push — master ( 29b457...c1dca6 )
by Daniel
02:15
created

AbstractClient   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 121
ccs 29
cts 29
cp 1
rs 10
c 0
b 0
f 0
wmc 8

5 Methods

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