Completed
Push — master ( 584eab...218d7a )
by Daniel
02:26
created

Client   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 81
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getRegistrableDomain() 0 7 3
A lookup() 0 20 2
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 Client
18
{
19
20
    /**
21
     * The TLD Whois locator class.
22
     * @var Locator
23
     */
24
    private $tldLocator;
25
26
    /**
27
     * The Unicode for IDNA.
28
     * @var TrueBV\Punycode
0 ignored issues
show
Bug introduced by
The type MallardDuck\Whois\TrueBV\Punycode was not found. Did you mean TrueBV\Punycode? If so, make sure to prefix the type with \.
Loading history...
29
     */
30
    private $punycode;
31
32
    /**
33 9
     * The carriage return line feed character comobo.
34
     * @var string
35 9
     */
36 9
    private $clrf = "\r\n";
37
38
    /**
39
     * The input domain provided by the user.
40
     * @var string
41
     */
42
    public $inputDomain;
43 6
44
    /**
45 6
     * The encoded domain after parsing with Punycode.
46 3
     * @var string
47
     */
48
    public $encodedDomain;
49 3
50
    /**
51 3
     * Construct the Whois Client Class.
52 3
     */
53
    public function __construct()
54 3
    {
55
        $this->punycode = new Punycode();
0 ignored issues
show
Documentation Bug introduced by
It seems like new TrueBV\Punycode() of type TrueBV\Punycode is incompatible with the declared type MallardDuck\Whois\TrueBV\Punycode of property $punycode.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
56
        $this->tldLocator = new Locator;
57 3
    }
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
    private function getRegistrableDomain($domain)
65
    {
66
        $host = (new Host($domain))->getRegistrableDomain();
67
        if ( strlen($host) === 0 && strlen($host) >= 0) {
68
            $host = $domain;
69
        }
70
        return $this->punycode->encode($host);
71
    }
72
73
    /**
74
     * Performs a Whois look up on the domain provided.
75
     * @param  string $domain The domain being looked up via whois.
76
     * @return string         The output of the Whois look up.
77
     */
78
    public function lookup($domain = '')
79
    {
80
        if (empty($domain)) {
81
            throw new MissingArgException("Must provide a domain name when using lookup method.");
82
        }
83
        $this->inputDomain = $domain;
84
85
        $this->encodedDomain = $this->getRegistrableDomain($domain);
86
        // Get the domains whois server.
87
        $whoisServer = $this->tldLocator->getWhoisServer($this->encodedDomain);
88
        // Form a socket connection to the whois server.
89
        $client = new SocketClient('tcp://' . $whoisServer . ':43', 10);
90
        $client->connect();
91
        // Send the domain name requested for whois lookup.
92
        $client->writeString($this->encodedDomain . $this->clrf);
93
        // Read and return the full output of the whois lookup.
94
        $response = $client->readAll();
95
        $client->disconnect();
96
97
        return $response;
98
    }
99
}
100