Completed
Push — master ( 626a8c...38bd5f )
by Daniel
03:05
created

AbstractWhoisClient::makeRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
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 1
dl 0
loc 4
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\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 input domain provided by the user.
27
     * @var SocketClient
28
     */
29
    protected $connection;
30
31
    /**
32
     * Perform a Whois lookup.
33
     *
34
     * Performs a Whois request using the given input for lookup and the Whois
35
     * server values.
36
     *
37
     * @param  string $lookupValue  The domain or IP being looked up.
38
     * @param  string $whoisServer  The whois server being queried.
39
     *
40
     * @return string               The raw text results of the query response.
41
     */
42 24
    public function makeWhoisRequest(string $lookupValue, string $whoisServer)
43
    {
44 24
        $this->createConnection($whoisServer);
45 24
        $this->makeRequest($lookupValue);
46 24
        $response = $this->getResponseAndClose();
47
48 24
        return $response;
49
    }
50
51
    /**
52
     * Creates the connection to the whois server.
53
     *
54
     * @param string $whoisServer The whois server being queried.
55
     */
56 24
    final public function createConnection(string $whoisServer)
57
    {
58
        // Form a tcp socket connection to the whois server.
59 24
        $this->connection = new SocketClient('tcp://'.$whoisServer.':43', 10);
60 24
        $this->connection->connect();
61 24
    }
62
63
    /**
64
     * Makes a whois request
65
     *
66
     * @param string $lookupValue The cache item to save.
67
     *
68
     * @return bool True if all not-yet-saved items were successfully saved or
69
     * there were none. False otherwise.
70
     */
71 24
    final public function makeRequest(string $lookupValue)
72
    {
73
        // Send the domain name requested for whois lookup.
74 24
        return $this->connection->writeString($lookupValue.$this->clrf);
75
    }
76
77
    /**
78
     * A function for making a raw Whois request.
79
     *
80
     * @return string   The raw results of the query response.
81
     */
82 24
    final public function getResponseAndClose()
83
    {
84
        // Read the full output of the whois lookup.
85 24
        $response = $this->connection->readAll();
86
        // Disconnect the connections to prevent network/performance issues.
87 24
        $this->connection->disconnect();
88 24
        return $response;
89
    }
90
}
91