Test Failed
Push — master ( ed95ce...c4bcac )
by Daniel
02:07
created

AbstractClient   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 95
rs 10
c 0
b 0
f 0
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A makeWhoisRawRequest() 0 12 1
A makeWhoisRequest() 0 5 1
B parseWhoisDomain() 0 27 6
A __construct() 0 4 1
1
<?php
2
namespace MallardDuck\Whois;
3
4
use TrueBV\Punycode;
5
use League\Uri\Components\Host;
6
use League\Uri\Components\Exception;
7
use Hoa\Socket\Client as SocketClient;
8
use MallardDuck\Whois\WhoisServerList\Locator;
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
    public function __construct()
54
    {
55
        $this->punycode = new Punycode();
56
        $this->tldLocator = new Locator;
57
    }
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
    protected function parseWhoisDomain($domain)
65
    {
66
        $this->inputDomain = $domain;
67
68
        // Check domain encoding
69
        $encoding = mb_detect_encoding( $domain );
70
71
        // Attempt to parse the domains Host component and get the registrable parts.
72
        try {
73
            $host = new Host($domain);
74
            // Get the method by which is supported to maintain PHP 7 and 5.6 compatibility.
75
            $method = (method_exists($host, 'getRegistrableDomain')) ? 'getRegistrableDomain' : 'getRegisterableDomain';
76
            $processedDomain = $host->$method();
77
            // Check how the host component was parsed
78
            if ( strlen($processedDomain) === 0 && strlen($host) >= 0) {
79
                $processedDomain = $domain;
80
            }
81
        } catch (Exception $e) {
82
            $processedDomain = $domain;
83
        }
84
85
        // Punycode the domain if it's Unicode
86
        if ("UTF-8" === $encoding) {
87
            $processedDomain = $this->punycode->encode($processedDomain);
88
        }
89
        $this->parsedDomain = $processedDomain;
90
        return $this;
91
    }
92
93
    public function makeWhoisRequest($domain, $whoisServer)
94
    {
95
        $this->parseWhoisDomain($domain);
96
        // Form a socket connection to the whois server.
97
        return $this->makeWhoisRawRequest($this->parsedDomain, $whoisServer);
98
    }
99
100
    public function makeWhoisRawRequest($domain, $whoisServer)
101
    {
102
        // Form a socket connection to the whois server.
103
        $client = new SocketClient('tcp://' . $whoisServer . ':43', 10);
104
        $client->connect();
105
        // Send the domain name requested for whois lookup.
106
        $client->writeString($domain . $this->clrf);
107
        // Read and return the full output of the whois lookup.
108
        $response = $client->readAll();
109
        $client->disconnect();
110
111
        return $response;
112
    }
113
}
114