AbstractWhoisClient::getResponseAndClose()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 8
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
3
namespace MallardDuck\Whois;
4
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 1.0.0
15
 */
16
abstract class AbstractWhoisClient implements WhoisClientInterface
17
{
18
    /**
19
     * The carriage return line feed character combo.
20
     * @var string
21
     */
22
    protected $clrf = "\r\n";
23
24
    /**
25
     * The timeout duration used for WhoIs server lookups.
26
     * @var int
27
     */
28
    public const TIMEOUT = 10;
29
30
    /**
31
     * The input domain provided by the user.
32
     * @var SocketClient
33
     */
34
    protected $connection;
35
36
    /**
37
     * Perform a Whois lookup.
38
     *
39
     * Performs a Whois request using the given input for lookup and the Whois
40
     * server values.
41
     *
42
     * @param string $lookupValue The domain or IP being looked up.
43
     * @param string $whoisServer The whois server being queried.
44
     *
45
     * @return string               The raw text results of the query response.
46
     * @throws Exceptions\SocketClientException
47
     */
48 24
    public function makeWhoisRequest(string $lookupValue, string $whoisServer): string
49
    {
50 24
        $this->createConnection($whoisServer);
51 24
        $this->makeRequest($lookupValue);
52 24
        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
     * @throws Exceptions\SocketClientException
61
     */
62 28
    final public function createConnection(string $whoisServer): void
63
    {
64
        // Form a TCP socket connection to the whois server.
65 28
        $this->connection = new SocketClient('tcp://' . $whoisServer . ':43', self::TIMEOUT);
66 28
        $this->connection->connect();
67 28
    }
68
69
    /**
70
     * Makes a whois request
71
     *
72
     * @param string $lookupValue The cache item to save.
73
     *
74
     * @return bool True if all not-yet-saved items were successfully saved or
75
     * there were none. False otherwise.
76
     * @throws Exceptions\SocketClientException
77
     */
78 28
    final public function makeRequest(string $lookupValue): bool
79
    {
80
        // Send the domain name requested for whois lookup.
81 28
        return $this->connection->writeString($lookupValue . $this->clrf);
82
    }
83
84
    /**
85
     * A function for making a raw Whois request.
86
     *
87
     * @return string   The raw results of the query response.
88
     * @throws Exceptions\SocketClientException
89
     */
90 28
    final public function getResponseAndClose(): string
91
    {
92
        // Read the full output of the whois lookup.
93 28
        $response = $this->connection->readAll();
94
        // Disconnect the connections after use in order to prevent observed
95
        // network & performance issues. Not doing this caused mild throttling.
96 28
        $this->connection->disconnect();
97 28
        return $response;
98
    }
99
}
100