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

Client::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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