Completed
Branch dev-2.0.0 (e891eb)
by Daniel
05:19
created

AbstractWhoisClient::makeWhoisRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
ccs 4
cts 4
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\WhoisClientInterface;
6
7
/**
8
 * The Whois Client Class.
9
 *
10
 * @author mallardduck <[email protected]>
11
 *
12
 * @copyright lucidinternets.com 2018
13
 *
14
 * @version 0.4.0
15
 */
16
abstract class AbstractWhoisClient implements WhoisClientInterface
17
{
18
19
    /**
20
     * The carriage return line feed character comobo.
21
     * @var string
22
     */
23
    protected $clrf = "\r\n";
24
25
    /**
26
     * The timeout duration used for WhoIs server lookups.
27
     * @var int
28
     */
29
    const TIMEOUT = 10;
30
31
    /**
32
     * The input domain provided by the user.
33
     * @var SocketClient
34
     */
35
    protected $connection;
36
37
    /**
38
     * Perform a Whois lookup.
39
     *
40
     * Performs a Whois request using the given input for lookup and the Whois
41
     * server values.
42
     *
43
     * @param  string $lookupValue  The domain or IP being looked up.
44
     * @param  string $whoisServer  The whois server being queried.
45
     *
46
     * @return string               The raw text results of the query response.
47
     */
48 20
    public function makeRequest(string $lookupValue, string $whoisServer) : string
49
    {
50 20
        $this->createConnection($whoisServer);
51 20
        $this->writeRequest($lookupValue);
52 20
        return $this->getResponseAndClose();
53
    }
54
55
    /**
56
     * Creates a socket connection to the whois server and activates it.
57
     *
58
     * @param string $whoisServer The whois server domain or IP being queried.
59
     */
60 22
    final public function createConnection(string $whoisServer) : void
61
    {
62
        // Form a TCP socket connection to the whois server.
63 22
        $this->connection = new SocketClient('tcp://'.$whoisServer.':43', self::TIMEOUT);
64 22
        $this->connection->connect();
65 22
    }
66
67
    /**
68
     * Writes a whois request to the active socket connection.
69
     *
70
     * @param string $lookupValue The cache item to save.
71
     *
72
     * @return int|bool Either an int of the return code, or false on some errors.
73
     */
74 22
    final public function writeRequest(string $lookupValue)
75
    {
76
        // Send the domain name requested for whois lookup.
77 22
        return $this->connection->writeString($lookupValue.$this->clrf);
78
    }
79
80
    /**
81
     * A method for retrieving a raw Whois response.
82
     *
83
     * This method must retrieve the reponse from the active socket and then
84
     * close the socket. If the connection is not properly closed servers could
85
     * drop/ignore rapid subsequent requests.
86
     *
87
     * @return string   The raw results of the query response.
88
     */
89 22
    final public function getResponseAndClose() : string
90
    {
91
        // Read the full output of the whois lookup.
92 22
        $response = $this->connection->readAll();
93
        // Disconnect the connections after use in order to prevent observed
94
        // network & performance issues. Not doing this caused mild trottling.
95 22
        $this->connection->disconnect();
96 22
        return $response;
97
    }
98
}
99