Completed
Push — master ( 6be770...a840d8 )
by Daniel
02:09
created

SocketClient::isConnected()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace MallardDuck\Whois;
4
5
use MallardDuck\Whois\Exceptions\SocketClientException;
6
7
/**
8
 * A simple socket stream client.
9
 *
10
 * @author mallardduck <[email protected]>
11
 *
12
 * @copyright lucidinternets.com 2020
13
 *
14
 * @version ?.?.?
15
 */
16
class SocketClient
17
{
18
    protected $socketUri = null;
19
    protected $socket = null;
20
    protected $timeout = 30;
21
    protected $connected = false;
22
23 38
    public function __construct(string $socketUri, int $timeout = 30)
24
    {
25 38
        $this->setSocketUri($socketUri);
26 38
        $this->setTimeout($timeout);
27 38
    }
28
29 38
    protected function setSocketUri(string $socketUri): string
30
    {
31 38
        $current = $this->socketUri;
32 38
        $this->socketUri = $socketUri;
33
34 38
        return $current ?? '';
35
    }
36
37 38
    protected function setTimeout(int $timeout): int
38
    {
39 38
        $current = $this->timeout;
40 38
        $this->timeout = $timeout;
41
42 38
        return $current;
43
    }
44
45 34
    public function connect(): self
46
    {
47 34
        $fp = stream_socket_client($this->socketUri, $errno, $errstr, $this->timeout);
48 34
        if (!is_resource($fp) && false === $fp) {
49
            $message = sprintf("Stream Connection Failed: %s", $errstr);
50
            throw new SocketClientException($message, $errno);
51
        }
52
53 34
        $this->socket = $fp;
54 34
        $this->connected = true;
55 34
        return $this;
56
    }
57
58 2
    public function isConnected(): bool
59
    {
60 2
        return $this->connected;
61
    }
62
63 34
    public function writeString(string $string)
64
    {
65 34
        if (!$this->connected) {
66 2
            $message = sprintf("The calling method %s requires the socket to be connected", __FUNCTION__);
67 2
            throw new SocketClientException($message);
68
        }
69 32
        return stream_socket_sendto($this->socket, $string);
70
    }
71
72 32
    public function readAll(): string
73
    {
74 32
        if (!$this->connected) {
75 2
            $message = sprintf("The calling method %s requires the socket to be connected", __FUNCTION__);
76 2
            throw new SocketClientException($message);
77
        }
78 30
        return stream_get_contents($this->socket);
79
    }
80
81 38
    public function disconnect(): self
82
    {
83 38
        if (!is_null($this->socket)) {
84 34
            stream_socket_shutdown($this->socket, STREAM_SHUT_RDWR);
85 34
            fclose($this->socket);
86
        }
87 38
        $this->socket = null;
88 38
        $this->connected = false;
89
90 38
        return $this;
91
    }
92
93 18
    public function __destruct()
94
    {
95 18
        $this->disconnect();
96 18
    }
97
}
98