Completed
Push — dev-2.0.0 ( 51660a...55cd5f )
by Daniel
01:42
created

AbstractWhoisClient::withPort()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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

83
        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...
84
    }
85
86
    /**
87
     * A method for retrieving a raw Whois response.
88
     *
89
     * This method must retrieve the reponse from the active socket and then
90
     * close the socket. If the connection is not properly closed servers could
91
     * drop/ignore rapid subsequent requests.
92
     *
93
     * @return string   The raw results of the query response.
94
     */
95 24
    final public function getResponseAndClose() : string
96
    {
97
        // Read the full output of the whois lookup.
98 24
        $response = $this->connection->readAll();
99
        // Disconnect the connections after use in order to prevent observed
100
        // network & performance issues. Not doing this caused mild trottling.
101 24
        $this->connection->disconnect();
102 24
        return $response;
103
    }
104
105
    // Fluent option methods
106
    /**
107
     * Fluent method to set the port for non-standard requests.
108
     *
109
     * @param  int    $port The port to use for the request.
110
     * @return self         The very same class.
111
     */
112
    final public function withPort(int $port) : self
113
    {
114
        $input->port = $port;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $input seems to be never defined.
Loading history...
115
        return $this;
116
    }
117
118
    /**
119
     * Fluent method to set the timeout for the current request.
120
     *
121
     * @param  int    $timeout  The timeout duration in seconds.
122
     * @return self             The very same class.
123
     */
124
    final public function withTimeout(int $timeout) : self
125
    {
126
        $input->timeout = $timeout;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $input seems to be never defined.
Loading history...
127
        return $this;
128
    }
129
}
130