Passed
Branch dev-2.0.0 (810e5d)
by Daniel
09:00
created

AbstractWhoisClient::getResponse()   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
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
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 port for creating the socket.
20
     *
21
     * @var int
22
     */
23
    protected $port = 43;
24
25
    /**
26
     * The timeout duration used for WhoIs server lookups.
27
     *
28
     * @var int
29
     */
30
    protected $timeout = 10;
31
32
    /**
33
     * The carriage return line feed character combo.
34
     *
35
     * @var string
36
     */
37
    protected $clrf = "\r\n";
38
39
    /**
40
     * The input domain provided by the user.
41
     *
42
     * @var SocketClient|null
43
     */
44
    protected $connection;
45
46
    /**
47
     * Perform a Whois lookup.
48
     *
49
     * Performs a Whois request using the given input for lookup and the Whois
50
     * server values.
51
     *
52
     * @param string $lookupValue The domain or IP being looked up.
53
     * @param string $whoisServer The whois server being queried.
54
     *
55
     * @return string               The raw text results of the query response.
56
     * @throws Exceptions\SocketClientException
57
     */
58 3
    public function makeRequest(string $lookupValue, string $whoisServer): string
59
    {
60 3
        $this->createConnection($whoisServer);
61 3
        $this->writeRequest($lookupValue);
62 3
        return $this->getResponseAndClose();
63
    }
64
65
    /**
66
     * Creates a socket connection to the whois server and activates it.
67
     *
68
     * @param string $whoisServer The whois server domain or IP being queried.
69
     *
70
     * @throws Exceptions\SocketClientException
71
     */
72 9
    final public function createConnection(string $whoisServer): void
73
    {
74
        // Form a TCP socket connection to the whois server.
75 9
        $this->connection = new SocketClient('tcp://' . $whoisServer . ':' . $this->port, $this->timeout);
76 9
        $this->connection->connect();
77 9
    }
78
79
    /**
80
     * Writes a whois request to the active socket connection.
81
     *
82
     * @param string $lookupValue The cache item to save.
83
     *
84
     * @return int|bool Either an int of the return code, or false on some errors.
85
     * @throws Exceptions\SocketClientException
86
     */
87 9
    final public function writeRequest(string $lookupValue)
88
    {
89
        // Send the domain name requested for whois lookup.
90 9
        return $this->connection->writeString($lookupValue . $this->clrf);
0 ignored issues
show
Bug introduced by
The method writeString() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

90
        return $this->connection->/** @scrutinizer ignore-call */ writeString($lookupValue . $this->clrf);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
91
    }
92
93
    /**
94
     * A method for retrieving a raw Whois response.
95
     *
96
     * @return string
97
     * @throws Exceptions\SocketClientException
98
     */
99 9
    final public function getResponse(): string
100
    {
101
        // Read the full output of the whois lookup.
102 9
        return $this->connection->readAll();
103
    }
104
105
    /**
106
     * A method for retrieving a raw Whois response.
107
     *
108
     * This method must retrieve the response from the active socket and then
109
     * close the socket. If the connection is not properly closed servers could
110
     * drop/ignore rapid subsequent requests.
111
     *
112
     * @return string   The raw results of the query response.
113
     * @throws Exceptions\SocketClientException
114
     */
115 9
    final public function getResponseAndClose(): string
116
    {
117
        // Read the full output of the whois lookup.
118 9
        $response = $this->getResponse();
119
        // Disconnect the connections after use in order to prevent observed
120
        // network & performance issues. Not doing this caused mild throttling.
121 9
        $this->connection->disconnect();
122 9
        return $response;
123
    }
124
125
    // Fluent option methods
126
    /**
127
     * Fluent method to set the port for non-standard requests.
128
     *
129
     * @param  int    $port The port to use for the request.
130
     * @return self         The very same class.
131
     */
132
    final public function withPort(int $port): self
133
    {
134
        $this->port = $port;
135
        return $this;
136
    }
137
138
    /**
139
     * Fluent method to set the timeout for the current request.
140
     *
141
     * @param  int    $timeout  The timeout duration in seconds.
142
     * @return self             The very same class.
143
     */
144
    final public function withTimeout(int $timeout): self
145
    {
146
        $this->timeout = $timeout;
147
        return $this;
148
    }
149
}
150