Completed
Push — master ( 626a8c...38bd5f )
by Daniel
03:05
created

Client::getSearchableHostname()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
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 MallardDuck\Whois\WhoisServerList\AbstractLocator;
7
use MallardDuck\Whois\WhoisServerList\DomainLocator;
8
use MallardDuck\Whois\Exceptions\MissingArgException;
9
10
/**
11
 * The Whois Client Class.
12
 *
13
 * @author mallardduck <[email protected]>
14
 *
15
 * @copyright lucidinternets.com 2018
16
 *
17
 * @version 0.4.0
18
 */
19
class Client extends AbstractWhoisClient
20
{
21
22
    /**
23
     * The TLD Whois locator class.
24
     * @var AbstractLocator
25
     */
26
    protected $whoisLocator;
27
28
    /**
29
     * The Unicode for IDNA.
30
     * @var \TrueBV\Punycode
31
     */
32
    protected $punycode;
33
34
    /**
35
     * The input domain provided by the user.
36
     * @var string
37
     */
38
    public $inputDomain;
39
40
    /**
41
     * The parsed domain after validating and encoding.
42
     * @var string
43
     */
44
    public $parsedDomain;
45
46
    /**
47
     * Construct the Whois Client Class.
48
     */
49 62
    public function __construct()
50
    {
51 62
        $this->punycode = new Punycode();
52 62
        $this->whoisLocator = new DomainLocator();
53 62
    }
54
55
    /**
56
     * A unicode safe method for making whois requests.
57
     *
58
     * The main difference with this method is the benefit of punycode domains.
59
     *
60
     * @param  string $domain      The domain or IP being looked up.
61
     * @param  string $whoisServer The whois server being queried.
62
     *
63
     * @return string              The raw results of the query response.
64
     */
65 2
    public function makeSafeWhoisRequest($domain, $whoisServer)
66
    {
67 2
        $this->parseWhoisDomain($domain);
68
        // Form a socket connection to the whois server.
69 2
        return $this->makeWhoisRequest($this->parsedDomain, $whoisServer);
70
    }
71
72
    /**
73
     * Uses the League Uri Hosts component to get the search able hostname in PHP 5.6 and 7.
74
     *
75
     * @param string $domain The domain or IP being looked up.
76
     *
77
     * @return string
78
     */
79 52
    protected function getSearchableHostname($domain)
80
    {
81
        // Attempt to parse the domains Host component and get the registrable parts.
82 52
        $host = new Host($domain);
83 48
        return $host->getRegistrableDomain();
84
    }
85
86
    /**
87
     * Takes the user provided domain and parses then encodes just the registerable domain.
88
     * @param  string $domain The user provided domain.
89
     *
90
     * @return string Returns the parsed domain.
91
     */
92 54
    protected function parseWhoisDomain($domain)
93
    {
94 54
        if (empty($domain)) {
95 2
            throw new MissingArgException("Must provide a domain name when using lookup method.");
96
        }
97 52
        $this->inputDomain = $domain;
98
99
        // Check domain encoding
100 52
        $encoding = mb_detect_encoding($domain);
101
102 52
        $processedDomain = $this->getSearchableHostname($domain);
103
104
        // Punycode the domain if it's Unicode
105 48
        if ("UTF-8" === $encoding) {
106 10
            $processedDomain = $this->punycode->encode($processedDomain);
107
        }
108 48
        $this->parsedDomain = $processedDomain;
109
110 48
        return $processedDomain;
111
    }
112
113
    /**
114
     * Performs a Whois look up on the domain provided.
115
     * @param  string $domain The domain being looked up via whois.
116
     *
117
     * @return string         The output of the Whois look up.
118
     */
119 40
    public function lookup($domain = '')
120
    {
121 40
        if (empty($domain)) {
122 4
            throw new MissingArgException("Must provide a domain name when using lookup method.");
123
        }
124 36
        $this->parseWhoisDomain($domain);
125
126
        // Get the domains whois server.
127 34
        $whoisServer = $this->whoisLocator->getWhoisServer($this->parsedDomain);
128
129
        // Get the full output of the whois lookup.
130 22
        $response = $this->makeWhoisRequest($this->parsedDomain, $whoisServer);
131
132 22
        return $response;
133
    }
134
}
135