Passed
Push — master ( 7a4139...584eab )
by Daniel
01:56
created

Client   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A lookup() 0 15 2
1
<?php
2
namespace MallardDuck\Whois;
3
4
use Hoa\Socket\Client as SocketClient;
5
use MallardDuck\Whois\WhoisServerList\Locator;
6
use MallardDuck\Whois\Exceptions\MissingArgException;
7
8
/**
9
 * The Whois Client Class.
10
 *
11
 * @author mallardduck <[email protected]>
12
 * @copyright lucidinternets.com 2018
13
 * @version 1.0.0
14
 */
15
class Client
16
{
17
18
    /**
19
     * The TLD Whois locator class.
20
     * @var Locator
21
     */
22
    private $tldLocator;
23
24
    /**
25
     * The carriage return line feed character comobo.
26
     * @var string
27
     */
28
    private $clrf = "\r\n";
29
30
    /**
31
     * Construct the Whois Client Class.
32
     */
33 9
    public function __construct()
34
    {
35 9
        $this->tldLocator = new Locator;
36 9
    }
37
38
    /**
39
     * Performs a Whois look up on the domain provided.
40
     * @param  string $domain The domain being looked up via whois.
41
     * @return string         The output of the Whois look up.
42
     */
43 6
    public function lookup($domain = '')
44
    {
45 6
        if (empty($domain)) {
46 3
            throw new MissingArgException("Must provide a domain name when using lookup method.");
47
        }
48
        // Get the domains whois server.
49 3
        $whoisServer = $this->tldLocator->getWhoisServer($domain);
50
        // Form a socket connection to the whois server.
51 3
        $client = new SocketClient('tcp://' . $whoisServer . ':43');
52 3
        $client->connect();
53
        // Send the domain name requested for whois lookup.
54 3
        $client->writeLine($domain . $this->clrf);
55
56
        // Read and return the full output of the whois lookup.
57 3
        return $client->readAll();
58
    }
59
}
60